Display iTunes Cover & Currently Playing Track

touch-bar

#23

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.


#24

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!


#25

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 :-))


#26

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!


#27

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


#28

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

#29

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.


#30

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

Update: forgot I could do this simply with btt actions


#31

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


#32

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


#33

Awesome, thanks @liamb!


#34

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


#35

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

a Reminders.app script
#36

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.