As the Now playing widget struggles to get the album artwork if you're streaming a song which is not in your library and I did not find a way to get the now playing information from the mini player (which would be awesome because I could also control the music currently plying on the HomePod), I wrote the following script to download the album artwork as jpg:
-- get the raw bytes of the artwork into a var
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
-- get the filename to ~/Desktop/cover.ext
set fileName to ((((path to application support folder from user domain) as text) & "BetterTouchTool:" as text) & "music_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
return fileName
Now the thing is, in order to display it in the Touch Bar I need it to get converted into base64. Any idea how this could be achieved?
-- get the raw bytes of the artwork into a var
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 ".png"
end if
end tell
-- get the filename to ~/Desktop/cover.ext
set fileName to ((((path to application support folder from user domain) as text) & "BetterTouchTool:" as text) & "music_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
return "{\"text\":\"fileName\", \"icon_path\":\"MY_HARDDRIVE_HERE/Users/Caliguvara/Library/Application Support/BetterTouchTool/music_cover.png\"}"
If this doesn't help I can try to reproduce this later today! Maybe using the BTT_PRESET_PATH instead of the hard coded path would also be a good idea.
I’m not familiar with what the image format requirements are for touchbar widgets, but this won’t be converting JPEG data into PNG data; it merely gives the filename an extension that isn’t appropriate to any file that ends up containing a JPEG image. It’s similar to selecting a .jpg file in Finder and renaming it so that it ends with a .png file extension.
Fair enough. I hoped it would work nevertheless From what I saw in the community, your scripting and coding knowledge is far beyond any of my capacities, do you have any hint how I could get the current artwork in Music and get it into the Touch Bar?
I wasn’t implying your method would definitely not work. It is something I would probably try myself because it’s takes one minor edit and half a second to run before ruling something out as being too ridiculous to put past Apple.
But I assume you have tried it, to no avail, so it’s useful to know why it wouldn’t have been one to bank on.
If you’re able to fill in a vital piece of information for me:
...then I am more than happy to give you an idea of the approach I would take. Your initial idea mentioned something about converting JPEG image data into base 64–why ? What are all the potential types of data you’re aware of that would be successfully read and displayed as an image for a widget ? Is this an Apple-defined protocol, or is it intrinsic to BTT ?
I think you need an extra backslash in "Application\ Support", so that it becomes "Application\\ Support".
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
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
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.
Is there a way to access the Music Mini Player btw? This would make it possible to control Music playing on an TV or the HomePod too, and be definitively a nice feature