Some very minor points:
-
If you prefer the
system attribute
command to thepath to
command, it's better in this instance to usesystem attribute "HOME"
rather thansystem attribute "USER"
. The latter, of course, returns the user's login name, however, not only does this definitely have to match the name of the user's home directory, but the home directory doesn't necessarily have to reside in the/Users
folder. Therefore, reading the value from the"HOME"
environment variable will be more reliable, and also give you a full path, i.e./Users/%USER%/
in most cases, but/var/macOS/
in my case. -
"/Users/" & (system attribute ...) & "/Library/..."
are three strings that are concatenated together to form a string. Therefore, there's no need to coerce it totext
as well. The line simplifies to:set fileName to (system attribute "HOME") & "/Library/Application Support/BetterTouchTool/itunes_cover" & ext
-
No need to
tell application "Finder"
to do anything in this script, thankfully. Finder is awful, and will just slow everything down. That line can simply reduce to:set cover_file to the POSIX path of the filename
-
open for access
andclose access
have a few specific uses, the most of important of which are in locking a file if the read or write operation is going to take a long time; or to obtain a reference to a file handle to use throughout various parts of a script or in different scripts. However, for the majority of cases, we can forgo their involvement, and they actually end up causing more hassle if one doesn't implement effective error-catching so as to ensure one doesn't leave a file handle open in the event of an unexpected early termination of the script beforeclose access
has been invoked. Much simpler just towrite srcBytes to outFile
without theopen for access
call, and then there's no need toclose access
.Below, you'll see that I do actually make a call to these two commands, but in immediate succession to one another. This is a quick and simple way to create a file if one doesn't already exist.
-
Lastly,
POSIX path of fileName as text
, again, is a redundant coercion, asPOSIX path of
returns atext
object. Coercions are pretty expensive operations, so doing them needlessly slows things down.
I re-worked the script slightly, which I hope benefits the OP. It should be quicker in theory but, of course, it's such a small script, one isn't going to notice in practice. It also converts whatever format the raw data is in to jpeg
format, to reduce the file size. For example, a 433KB png
file squeezes into a 72KB jpeg
, which will be less work (data) for both Better Touch Tool, but, more valuably, the touch bar, to process and render.
use application id "com.apple.Music"
use scripting additions
---------------------------------------------------------------------
property json : a reference to [¬
"{\"text\": \"", my trackInfo, "\",", ¬
" \"font_color\": \"255,255,255,255\",", ¬
" \"icon_path\": \"", my imgPath, "\"}"]
property BTTdir : POSIX path of (path to ¬
application support folder from ¬
user domain) & "BetterTouchTool"
property filename : "itunes_cover.jpg"
property artwork : a reference to the current track's artworks
property raw data : a reference to raw data of my artwork
property track : a reference to the current track's name
property artist : a reference to the current track's artist
---------------------------------------------------------------------
set [imgPath, trackInfo] to ["", ""]
if not (my raw data exists) then return the json's contents as text
set trackInfo to my artist & " — " & my track
set [X] to my raw data
# set BTTdir to system attribute "TMPDIR" -- for debugging
set imgPath to BTTdir & "/" & filename
close access (open for access imgPath)
set eof of imgPath to 0
write X to imgPath
set the clipboard to (read imgPath as "TIFF")
set eof of imgPath to 0
write (the clipboard as "JPEG") to imgPath
set the clipboard to
return the json's contents as text
----------------------------------------------------------------‹END›