Improved Spotify Widget

touch-bar

#1

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


#3

" 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


#4

@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.)


#5

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