Display iTunes Cover & Currently Playing Track

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