Now playing widget on menu bar

How can I have a now playing widget for Spotify on the menu bar?
Thanks

If you have one for the Touch Bar, just copy-paste the Apple-/Javascript in a menu bar widget. They work the same way.

1 Like

that worked, but the name only shown when it is highlighted (when I click and hold it)

Refresh rate is good?

Would you mind pasting the script below?

I put refresh rate to 0.1 seconds just for the testing, will change to 0.5 or 1 later.
script:

tell application "BetterTouchTool"
if is_app_running "Spotify" then
	tell application "Spotify"
		try
			if player state is playing then
				set sName to (get name of current track)
				set sArtist to (get artist of current track)
				
				if (length of sName) > 25 then
					set sName to text 1 thru 22 of sName & "..."
				else
					set sName to (get name of current track)
				end if
				if (length of sArtist) > 25 then
					set sArtist to text 1 thru 22 of sArtist & "..."
				else
					set sArtist to (get artist of current track)
				end if
				if sArtist is not "" then
					set nowPlaying to sName & "  —  " & sArtist
				else
					set nowPlaying to sName
				end if
				
				--fix JSON-Breaking Double Quotes
				set aString to nowPlaying
				set astid to AppleScript's text item delimiters
				set AppleScript's text item delimiters to quote
				set aString to text items of aString
				set AppleScript's text item delimiters to "\\" & quote
				set aString to aString as text
				set AppleScript's text item delimiters to astid
				-- Quotes have been vanquished
				set nowPlaying to aString
				
				set artworkURL to artwork url of current track
				
				set fileName to ((((path to application support folder from user domain) as text) & "BetterTouchTool:" as text) & "spotify_cover.png")
				
				set sNamePlist to ""
				set sArtistPlist to ""
				set plistfile_path to ((((path to application support folder from user domain) as text) & "BetterTouchTool:" as text) & "spotify_playing.plist")
				
				set returnText to "{\"text\":\"" & nowPlaying & "\",\"font_color\": \"255,255,255,255\", \"icon_path\":\"" & (POSIX path of fileName as text) & "\" }"
				
				-- read the plist file
				tell application "System Events"
					try
						if file plistfile_path exists then
							tell property list file plistfile_path
								tell contents
									set sNamePlist to value of property list item "sName"
									set sArtistPlist to value of property list item "sArtist"
								end tell
							end tell
						end if
						if (sName is sNamePlist) and (sArtist is sArtistPlist) then
							--return "Found it!"
							tell application "BetterTouchTool" to set_string_variable "ShowNotPlayingMsg1" to "false"
							return returnText
							
						end if
					on error
						tell application "BetterTouchTool" to set_string_variable "ShowNotPlayingMsg1" to "true"
						return "{\"text\":\" Spotify \",\"font_color\": \"255,255,255,255\"}"
					end try
				end tell
				
				if (sName is not sNamePlist) or (sArtist is not sArtistPlist) then
					
					tell application "System Events"
						try
							-- create an empty property list dictionary item
							set the parent_dictionary to make new property list item with properties {kind:record}
							
							-- create new property list file using the empty dictionary list item as contents
							set this_plistfile to ¬
								make new property list file with properties {contents:parent_dictionary, name:plistfile_path}
							make new property list item at end of property list items of contents of this_plistfile ¬
								with properties {kind:string, name:"sName", value:sName}
							make new property list item at end of property list items of contents of this_plistfile ¬
								with properties {kind:string, name:"sArtist", value:sArtist}
						on error
							tell application "BetterTouchTool" to set_string_variable "ShowNotPlayingMsg1" to "true"
							return "{\"text\":\" Spotify \",\"font_color\": \"255,255,255,255\"}"
						end try
					end tell
					
					do shell script "curl " & artworkURL & " -o ~/Library/Application\\ Support/BetterTouchTool/spotify_cover.png"
					
					tell application "BetterTouchTool" to set_string_variable "ShowNotPlayingMsg1" to "false"
					return returnText
					
				end if
			else
				tell application "BetterTouchTool" to set_string_variable "ShowNotPlayingMsg1" to "false"
				return "{\"text\":\" Spotify \",\"font_color\": \"255,255,255,255\"}"
			end if
		on error
			tell application "BetterTouchTool" to set_string_variable "ShowNotPlayingMsg1" to "false"
			return "{\"text\":\" Spotify \",\"font_color\": \"255,255,255,255\"}"
		end try
	end tell
else
	tell application "BetterTouchTool" to set_string_variable "ShowNotPlayingMsg1" to "true"
	return ""
end if

end tell

also, this only works for Spotify now playing. is there something I can use that you have that will work for any now playing?

bump..

BTT can provide the now playing information to you. However for performance reasons this is only enabled if you either have a now playing widget configured on the Touch Bar or you have a conditional activation group that uses the now playing variables.

If you set up a conditional activation group like this:

You will be able to query the BTT variables using Apple Script (get_string_variable, see http://docs.folivora.ai/docs/1102_apple_script.html)

For example BTTNowPlayingInfoTitle or BTTNowPlayingInfoAlbum

im a bit confused as to what you're saying. I do have a now playing widget on my touchbar (im using Aquatouch).

what do I do from here?

For example you can get the currently playing song using this Apple Script (which will work for any app that supports now playing)

tell application "BetterTouchTool" to get_string_variable "BTTNowPlayingInfoTitle"

it didn't work...

also, I think im starting to understand. the string variable 'BTTNowPlayingInfoTitle' is taken from that list in the Scripting BTT picture u sent, right? so am I right in saying, this code will only give the title. How do I make it format like this:
Thumbnail Title — Artist

lol, im an idiot. It was Command-D'd. it works now, and im gonna experiment to try get my format working.

this is what I have so far:

How do I get rid of the {} and the ""?

I noticed there's no string variable for the icon? or can I just not see it anywhere?


now I have this, edited from another post on this forum.
I also wanting to have a Play icon while paused, and a Pause icon while playing in place of the words 'Playing' and 'Paused' I have both of these icons in my finder. how do I add this?

code:

if application "BetterTouchTool" is running then

tell application "BetterTouchTool"

set statued to get_number_variable "BTTCurrentlyPlaying"

if statued is 1.0 then

set currentInfo to {"Music", get_string_variable "BTTNowPlayingInfoTitle", get_string_variable "BTTNowPlayingInfoArtist", get_string_variable "BTTNowPlayingInfoAlbum", "Playing"}

else

set currentInfo to {"Music", get_string_variable "BTTNowPlayingInfoTitle", get_string_variable "BTTNowPlayingInfoArtist", get_string_variable "BTTNowPlayingInfoAlbum", "Paused"}

end if

end tell

end if

bump.

bump again

tell application "BetterTouchTool"
	set statued to get_number_variable "BTTCurrentlyPlaying"
	if statued is 1.0 then
		set InfoTitle to get_string_variable "BTTNowPlayingInfoTitle"
		set InfoArtist to get_string_variable "BTTNowPlayingInfoArtist"
		return InfoTitle & " – " & InfoArtist
	else
		return "Play Music"
	end if
end tell

Set as Button icon the Play icon, as alternate Button icon the Pause icon. And set "Play Music" as Alternate Color Regex.

Edit. Play Music will be displayed if no music is currently playing. Change it to "Paused" in the script (3rd line from the bottom) and as regex if you prefer to see this.

1 Like

that works! unfortunately the play and pause icons are very close to the title. Can I make it further away? maybe with a seperate icon?

also, for some reason it doesn't show on some apps, randomly. also, when a YouTube vid is playing, it says 'missing value' at the end

tell application "BetterTouchTool"
	set statued to get_number_variable "BTTCurrentlyPlaying"
	if statued is 1.0 then
		set InfoTitle to get_string_variable "BTTNowPlayingInfoTitle"
		set InfoArtist to get_string_variable "BTTNowPlayingInfoArtist"
		if InfoArtist is not missing value then
			return "   " & InfoTitle & " – " & InfoArtist
		else
			return "   " & InfoTitle
		end if
	else
		return "   " & "Play Music"
	end if
end tell

Try this. Add more or less spaces in the first two Return Lines between the first " ". This will add some space in front of the output, behind the icon. If your icon is at the end, use the following script:

tell application "BetterTouchTool"
	set statued to get_number_variable "BTTCurrentlyPlaying"
	if statued is 1.0 then
		set InfoTitle to get_string_variable "BTTNowPlayingInfoTitle"
		set InfoArtist to get_string_variable "BTTNowPlayingInfoArtist"
		if InfoArtist is not missing value then
			return InfoTitle & " – " & InfoArtist & "  "
		else
			return "   " & InfoTitle & "  "
		end if
	else
		return "Play Music" & "  "
	end if
end tell

The "Missing Value" is not in any of these two scripts (due to the extra if is missing value condition I added). This should satisfy you now.

1 Like