A Cocoa source project using NSAppleScript to access iTunes artwork by track Persistent ID
iTunes 7 introduced a new album artwork format which stores image data on the user's hard disk at an obfuscated path like ~/Music/iTunes/Album Artwork/Download/7981A995206365E0/08/06/00/7981A995206365E0-2BBBE65B9B38C068.itc. It is impossible for developers to access this stored data in a way that links it to the associated track. In addition, this art is stored on disk in the opaque .itc format, which is not recognised my most image editors, although the data inside is either png or jpg.
During the development of AudioCodex, we have long wished to have access to iTunes' album artwork. MachineCodex Software previously released ArtDeCodex which, in an early attempt to circumvent the restrictions outlined above, decodes the png or jpg data inside an itc file. This tool was ultimately useless for AudioCodex however, as it doesn't solve the problem of how to get the artwork for a particular track, and not just some random track's artwork.
Prompted by an email from Eddie, an AudioCodex user, I decided this week to finally put together a solution to this missing functionality. I was aware of only one way that our desired feature could be possible - by using AppleScript to query iTunes directly for the artwork for a particular track. AppleScript access requires the target app to be running, meaning that we would have to launch iTunes to get at the artwork.
For us, this was a little bit undesirable, as up until this point we have been able to integrate iTunes data into AudioCodex without ever having to launch iTunes. Some people don't enjoy having iTunes running, although with the advent of the iPhone, it is perhaps not as unusual to find iTunes in use on these people's Macs. On the other hand, many people don't have a problem with iTunes, and we want to accommodate both kinds of users with AudioCodex. Most of all, iTunes has excellent album art functionality, which AudioCodex was the poorer for being unable to access.
Writing Cocoa to access AppleScript is much easier than it used to be thanks to Leopard's ScriptingBridge Framework, which is a boon for developers. However, we want AudioCodex to continue being compatible with Tiger for as long as possible, which meant using the older and more obscure NSAppleScript Framework. Thankfully, we were able to find an open source project known as Clutter (now defunct) which uses NSAppleScript to access iTunes artwork.
Access to the Clutter source code made this a very simple feature to implement, amounting to just a few hours work. I have included one entire class, ScriptingController, from the Clutter project, as well as a miscellany of methods. Thanks go to Sprote Rsrch for this code. (If you are associated with Sprote Rsrch, please put them in touch with us.)
Like many developers, when we want to test out new features for a large application like AudioCodex, we write a test app to prove the principle. For this project I wrote a small app, iTunes Artwork, which demonstrates all the necessary Cocoa and AppleScript code. As it is based on an open source project, we felt it was in the spirit to release this app into the public domain, under the MIT License. The Code from Clutter is attributed in the source.
Our example app features some neat functionality. For example, if iTunes isn't running, it will be launched. The image info is stored as NSData. The image is looked up by the track's Persistent ID. It can also get the artwork for the selected track in iTunes.
All this is accomplished by two AppleScripts and a few dozen lines of Objective-C.
The next version of AudioCodex, 1.5 due in a few weeks, will include iTunes album artwork, but for now, here is the Cocoa source code to iTunes Artwork for developers who might be interested in exploring AppleScript from within Cocoa. We hope you find it useful.