Improved Spotify Widget

I just happened to purchase BetterTouchBar and I am thoroughly enthused by the seemingly limitless customisation options. I realise the Spotify widget seems to be extremely popular around here, and so I would like to share mine with you all. I used a combination of my own code, and a few lines from Vas3k and GoldenChaos. Thank you so much for sharing your codes.
Anyway, here is my Spotify widget. Please let me know how I might improve this nifty little thing!

https://share.folivora.ai/sharedPreset/b8c8fe34-fe1e-4ed5-97f1-f216c2b32327

Tap - Toggles play or pause
Long press (playing) - Plays the next song
Long press (paused) - Opens Spotify

If "Artist - Song Title" exceeds 30 characters, it will only display the song title
If the song title itself exceeds 30 characters, it will display the first 27 characters and leave a "..." at the end.
It also displays the cover art of the current song, or the Spotify application icon when the player is paused.

Edit: In order to activate the trigger that plays the next song, create a new trigger and set its action to run an AppleScript. Copy and paste this into the editor and you should be good to go :slight_smile:

if application "Spotify" is running then
tell application "Spotify"
if player state is playing then
tell application "Spotify" to next track
else
tell application "Spotify" to activate
end if
end tell
end if

" Edit: In order to activate the trigger that plays the next song, create a new trigger and set its action to run an AppleScript. Copy and paste this into the editor and you should be good to go) "

Can you give me a little more info on how to do this, I can't make it happen

Sebastian

@SebastianOugterOlsen, I believe what needs to be done for that is to create a "named trigger" in the other tab of BTT preferences.

Trigger is "named trigger", predefined action is "Run Apple Script" (I think either version should work). Enter that text in the original post as the AppleScript. The name of this named trigger should be "Long Press Spotify" to match up with this shared widget settings.

Let me know if this works for you. (and @ChildOfRobertson, if I'm missing/mistaking something, please let me know.)

Hey,

I have made some improvements to your script as it didn't seem to work when playing music offline...
I also reworked the icon update as I noticed that with some artworks it was having trouble updating the icon (i noticed they took a bit longer to be added to the btt folder and therefore it would be take the artwork from the previous song) but now it works 100% of the time :slight_smile:

on name_and_cover(trackName, iPath)
	tell application "BetterTouchTool"
		if iPath contains "offline" then
			update_touch_bar_widget "61E02114-40B8-4D7E-94D0-6F0110B7D846" text trackName
		else
			update_touch_bar_widget "61E02114-40B8-4D7E-94D0-6F0110B7D846" text trackName icon_path iPath
		end if
	end tell
end name_and_cover

set iconPath to "~/Library/Application Support/BetterTouchTool/spotify_cover.png"
if application "Spotify" is running then
	try
		tell application "Spotify"
			try
				set artworkURL to artwork url of current track
				do shell script "curl " & artworkURL & " -o ~/Library/Application\\ Support/BetterTouchTool/spotify_cover.png"
			on error number 6
				set iconPath to "offline"
			end try
			
			if player state is playing then
				set trackTitle to ((get name of current track) & " - " & (get artist of current track))
				if length of trackTitle is less than 27 then
					set displayTitle to trackTitle
					my name_and_cover(displayTitle, iconPath)
				else
					set displayTitle to text 1 thru 27 of trackTitle & "..."
					my name_and_cover(displayTitle, iconPath)
				end if
			else
				my name_and_cover("(Paused)", iconPath)
			end if
		end tell
	end try
else
	return ""
end if

Hey! Just logged in again after ages. I tried out your code and it works brilliantly! Thank you so much :slight_smile:

I was able to combine this preset with the lyric display python code mentioned here:
https://community.folivora.ai/t/touchbar-synced-lyric/10498
Here is the github link for the lyric preset portion linked above: https://github.com/ChenghaoMou/touchbar-lyric

It took a little of me fumbling around to get the python portion working but I'm really happy with how it turned out. At the end is me showing off the gesture from BetterTouchTool to change my volume by twisting a "knob" on my touchpad. Sorry for the low video quality and shaky cam.

on name_and_cover(trackName, iPath)
	tell application "BetterTouchTool"
		if iPath contains "offline" then
			update_touch_bar_widget "582B6183-8FDA-464A-81BC-A222636D5E19" text trackName
		else
			update_touch_bar_widget "582B6183-8FDA-464A-81BC-A222636D5E19" text trackName icon_path iPath
		end if
	end tell
end name_and_cover

set iconPath to "~/Library/Application Support/BetterTouchTool/spotify_cover.png"
if application "Spotify" is running then
	try
		tell application "Spotify"
			try
				set artworkURL to artwork url of current track
				do shell script "curl " & artworkURL & " -o ~/Library/Application\\ Support/BetterTouchTool/spotify_cover.png"
			on error number 6
				set iconPath to "offline"
			end try
			
			if player state is playing then
				set trackTitle to ((get name of current track) & " - " & (get artist of current track))
				if length of trackTitle is less than 27 then
				set lyrics to do shell script "/usr/local/bin/python3.7 -m touchbar_lyric"
					set displayTitle to trackTitle
					
					set displayTitle to displayTitle & "
" & lyrics
my name_and_cover(displayTitle, iconPath)
					
				else
					set lyrics to do shell script "/usr/local/bin/python3.7 -m touchbar_lyric"
					set displayTitle to text 1 thru 27 of trackTitle & "..."
					set displayTitle to displayTitle & "
" & lyrics
					my name_and_cover(displayTitle, iconPath)
				end if
			else
				my name_and_cover("||", "")
			end if
			
		end tell
	end try
else
	return ""
end if

Hi everyone! And thanks ChildOfRobertson. code works perfectly.
In what way should i transform the script if i want it to go to the next song instead of pausing it? I was not able to make it work with the long press. More golbally, I don't get what part of the script concerns what happens when thouchbar button is 'touched'. Thanks a lot for your help!

I managed to make it work. But i would like to do the opposite. Change track with a short push and pause with a long. Not that it's very important but it will also allow me to understand the part I'm missing in the script. Thanks a lot!!

Hey there! This is really great code, I'm glad you managed to make it work. I seldom listen to songs with lyrics but that's a really wonderful feature :smiley:

Hey, apologies for the late reply! I'm glad you managed to make it work for you. I believe you have to add a named trigger for that to work, but I'm pretty you you've figured that one out too :slight_smile: cheers!

hey, im kinda new to programming and btt as a whole, but this preset (which as been my favorite), suddenly stopped working.

did a update to spotify or something ruin it? anyone experiencing the same thing?

Spotify seems to have broken their applescript support again.

Ah dang. I really hate how the touch bar media controls affects all kind of media.

Hoping there's a possible fix soon