There's so little on the net that is complete, so I thought this snippet of knowledge would be useful to any would-be Applescript coders:
set jpegFilename to "/path/to/some/artwork.jpg" set jpegFile to (POSIX file jpegFilename) tell application "Image Events" set myImage to open (jpegFile as file) -- the below line needs HFS syntax pathname, eg: MyComputer:Users:liquidx: -- save myImage as PICT in (file ":path:to:some:temporary.pict") save myImage as PICT in (POSIX file "/path/to/some/temporary.pict") close myImage end tell tell application "iTunes" set myTrack to current track set artworkCount to count of artwork of myTrack -- the below line needs HFS syntax pathname, eg: MyComputer:Users:liquidx: -- set myArt to read (file ":path:to:some:temporary.pict") from 513 as picture set myArt to read (POSIX file "/path/to/some/temporary.pict" ) from 513 as picture if artworkCount > 0 then set data of artwork (artworkCount + 1) of current track to myArt else set data of artwork 1 of current track to myArt end if end tell tell application "Image Events" -- delete (file ":path:to:some:temporary.pict") delete (POSIX file "/path/to/some/temporary.pict") end tell
Note that I have commented out the HFS file specification lines. I had troubles ensuring that the "POSIX file" command working reliably, and that Script Editor would munge everything into HFS path spec format. Note that it is important that certain things happen outside of "tell application 'Image Events'" and some outside. So if you have some path problems, try using the HFS path specification.
It might seem weird at first, but that is all you really need. I'm not sure how to force a particular format on to the artwork, but the file itself isn't stored in PICT format (because otherwise your file would grow around 1 or 2M.) I think internall iTunes will convert the PICT data into JPEG. You can verify that with doing:
tell application "iTunes" return format of artwork 1 of current track
Also note that in Applescript, for some reason, you do not need to initialise arrays. You just directly assign to the first element of artwork and it will create the array for your behind the scenes. It is a big leap of faith when coming from a structured programing language!
You can reply to me about this on Twitter: Tweet to @liquidx