Applescript: jpg to base64, in the case of album artwork

Well, to be honest I tried to make this post work in Catalina:

As just changing "iTunes" to "Music" did not work out I tried to rewrite the script, which was harder than expected :sweat_smile:
Now, as I got you the link I also checked on the original post from @Andreas_Hegenberg in this thread and figured out it worked (in my defense, last time I tried it under Mojave it did not ). So my problem is somehow solved by this :man_shrugging:t4:

Unfortunately I'm as unfamiliar with this as you are. I kinda was focused on base64 because that's the way I displayed custom icons for now in my widgets.
Anyhow, here is the working code from the other thread:

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

if num > 0 then
	
	tell application "Music" to tell artwork 1 of current track
		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
	
	
	tell application "Music"
		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
else
	set trackInfo to "Music close"
	return trackInfo
end if

It gets the title, artist and album too, but that's easy to erase from the code. :slightly_smiling_face:

1 Like