Display iTunes Cover & Currently Playing Track

Weird it doesn't do that for me (on High Sierra) if it's wrapped in

if application "Spotify" is running then

end if

is included. Spotify seems to stay closed.

It's never worked that way for me, even on High Sierra :thinking: :crazy_face: maybe I was doing something wrong?

Hmm.. I did a bit of messing around with the script execution interval and it seems like it does seem to have an effect on this issue, but only sometimes. Generally an interval of >3 seconds seems to do the trick and lets Spotify close permanently, but even then it sometimes relaunches. The script I'm running does use
if application "Spotify" is running then
end if
but this doesn't seem to help when intervals are lower.

I'm wondering if this may be because I just updated to 2.513 from 2.422 today, and prior to being on 2.513 this relaunching never happened.

@GoldenChaos thanks for the suggestion and force quitting does indeed work, but I feel like this only works as a temporary fix

the BTT version shouldn't matter as the Apple Script execution is handled by the system.
Maybe try this method of checking whether an app is running:

tell application "System Events"
	set num to count (every process whose name is "iTunes")
end tell
set maxSize to 20

if num > 0 then
 // here the script code that should be executed if iTunes is running
end if

Logically putting the script in the code you recommended above should resolve the problem, but weirdly Spotify seems to be stubborn and still relaunches. I honestly can't figure out why this is, I wonder if this is a bug on my end or something related to this stack overflow thread. At the moment I have changed the widget so that it is actually just a one line bashscript that runs the pre-compiled .scpt file:

osascript ~/path to script/script.scpt

Even with this workaround the relaunching phenomenon occurs, but generally works better than just having the widget as an applescript. What confuses me the most is the inconsistent behavior, sometimes it relaunches sometimes it doesn't. For now I think I'll just let this be, as it's ultimately really not a big deal, but I appreciate all the help anyways.

@GoldenChaos I haven't faced issues with Spotify, although it for some reason has converted Spotify to Spotify (Old) in my script? Then again the Spotify app has changed to Spotify (Old) for me too. Not sure what's happened. I rarely use Spotify though so hard to comment on.

I did come across an issue with iTunes if you use Apple Music. As the artwork is streamed it creates some issues searching for it. I was able to fix by checking if an artwork is available first though. Full iTunes code can be seen here:

if application "iTunes" is running then
	tell application "iTunes" to tell current track
		if exists (every artwork) then
			tell artwork 1
				set srcBytes to raw data
				-- figure out the proper file extension
				if format is «class PNG » then
					set ext to ".png"
				else
					set ext to ".jpg"
				end if
			end tell
			set fileName to ((((path to application support folder from user domain) as text) & "BetterTouchTool:" as text) & "itunes_cover" & ext)
			-- write to file
			set outFile to open for access file fileName with write permission
			-- truncate the file
			set eof outFile to 0
			-- write the image bytes to the file
			write srcBytes to outFile
			close access outFile
		else
			set fileName to ((((path to application support folder from user domain) as text) & "BetterTouchTool:" as text) & "itunes.png")
		end if
	end tell
	set maxSize to 25
	tell application "iTunes"
		set playState to (player state as text)
		if playState is equal to "playing" then
			set trackName to name of current track
			set artistName to artist of current track
			set albumName to album of current track
			if length of trackName is greater than maxSize then
				set trackName to text 1 thru (maxSize - 3) of trackName & "..."
			end if
			if length of artistName is greater than maxSize then
				set artistName to text 1 thru (maxSize - 3) of artistName & "..."
			end if
			set trackInfo to trackName & " | " & artistName
		else
			set trackInfo to ""
		end if
		return "{\"text\":\"" & trackInfo & "\", \"icon_path\":\"" & (POSIX path of fileName as text) & "\"}"
	end tell
end if
1 Like

This isn't getting the artwork from Apple Music tracks, though? Right now, for me, this displays whatever the last album art displayed was before streaming began. Would be great to be able to get the actual artwork.

I'd image there's a way to get the artwork url? It's possible through Spotify. I'll look into it further.

Apparently not possible: https://stackoverflow.com/posts/37473465/revisions

Maybe one day!

Possibly the Discogs API could be used to retrieve cover art in that case: https://www.discogs.com/developers/ (sorry I don't have a lot of time for BTT until end of next week, our wedding party is coming up this weekend :-))

3 Likes

No stress! You're allowed to take some time for yourself, you do plenty for us :grinning:

Congratulations! :champagne: Enjoy your weekend, and I hope the wedding was/goes amazing!

Is it possible to do the same for Spotify? Display artwork of playing track.

Yup!

if application "Spotify" is running then
	tell application "Spotify"
		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
			set nowPlaying to sName & "  —  " & sArtist
			set artworkURL to artwork url of current track
			do shell script "curl " & artworkURL & " -o ~/Library/Application\\ Support/BetterTouchTool/spotify_cover.png"
			set fileName to ((((path to application support folder from user domain) as text) & "BetterTouchTool:" as text) & "spotify_cover.png")
			return "{\"text\":\"" & nowPlaying & "\", \"icon_path\":\"" & (POSIX path of fileName as text) & "\" }"
			return nowPlaying
		else
			return "Paused"
		end if
	end tell
else
	return ""
end if

Hi there, I have tried this and 3 other ways to get an iTunes now playing widget to work in Mojave dev beta 6 - is it the case that AppleScript is not functioning correctly in the beta yet? I just cannot get any widget to appear at all.

Has anybody tried combining this with a play/pause function? That would be epic!

Update: forgot I could do this simply with btt actions

Does anybody have a fix for Apple Music? the entire buttons disappears as the script fails to run when playing from non-downloaded music

1 Like

Hi @Philip_Aarseth!
Scroll up a little ways, I found a way to work with this:

1 Like

Awesome, thanks @liamb!

I have a problem with spotify, the cover art is flickering on touch bar. Is it me only?

This script is rather inefficient regenerating the artwork and fetching the data upon each check. Personally I use an (old) script/app I wrote that polls when the track changes (and also uses non-embedded artwork).

However here's an updated script for iTunes that avoids much of the overhead by simply checking if the track actually changed before going off and doing stuff—so not unduly expensive on a frequency of say 8 seconds. Recommended with action of 'Play/Pause', icon size of 30, 0 radius, black background, and font size of 12 because it displays album, track and artist on two lines. (Does not change on pause.)

A similar approach should be possible with other apps, but if they don't provide an ID or URL you can just check if the album name changed. Could reuse the same BTT variable across apps although better as string if so.

if application "iTunes" is running then
tell application "BetterTouchTool" to set priorTrackId to get_number_variable "trackId"
tell application "iTunes"
	if player state is playing and id of current track is not priorTrackId then
		tell current track
			if exists (every artwork) then
				tell artwork 1
					set srcBytes to raw data
					-- figure out the proper file extension
					if format is «class PNG » then
						set ext to ".png"
					else
						set ext to ".jpg"
					end if
				end tell
				set fileName to ((((path to application support folder from user domain) as text) & "BetterTouchTool:" as text) & "itunes_cover" & ext)
				-- write to file
				set outFile to open for access file fileName with write permission
				-- truncate the file
				set eof outFile to 0
				-- write the image bytes to the file
				write srcBytes to outFile
				close access outFile
			else
				set fileName to (((path to application "iTunes") as text) & "Contents:Resources:iTunes.icns")
			end if
			set maxSize to 60
			set artistName to artist
			set albumName to album
			set trackName to name
			-- must escape double quotes such as in «12" mix»
			set delimiters to AppleScript's text item delimiters
			set AppleScript's text item delimiters to "\""
			set trackName to every text item of trackName
			set albumName to every text item of albumName
			set artistName to every text item of artistName
			set AppleScript's text item delimiters to "\\\""
			set trackName to trackName as string
			set albumName to albumName as string
			set artistName to artistName as string
			set AppleScript's text item delimiters to delimiters
			if length of trackName is greater than (maxSize / 2) then
				set trackName to text 1 thru (maxSize / 2) of trackName & "…"
			end if
			if length of artistName is greater than (maxSize / 2) then
				set artistName to text 1 thru (maxSize / 2) of artistName & "…"
			end if
			set trackRating to "  "
			repeat (rating / 20) times
				set trackRating to trackRating & "⭑"
			end repeat
			if length of (albumName & trackRating) is greater than maxSize then
				set albumName to text 1 thru (maxSize - (length of trackRating)) of albumName & "…"
			end if
			set trackInfo to albumName & trackRating & "\\n‘" & trackName & "’ by " & artistName
			set trackId to id
			tell application "BetterTouchTool" to set_number_variable "trackId" to trackId
		end tell
		return "{\"text\":\"" & trackInfo & "\", \"icon_path\":\"" & (POSIX path of fileName as text) & "\"}"
	end if
end tell
end if
1 Like

I've got this same issue. Works great for music I have put into my Library, but if I am listening to non-downloaded/non-library music, the icon disappears.

Edit: I am an idiot and did not read replied posts. :slight_smile: . Nevermind.