Returning useful data from iTunes artwork

Hi all

I’m writing an application in RealBasic, using AppleScript to communicate with iTunes. I want to get the artwork from the currently selected track. Currently I’m doing that using the following:

tell application "iTunes" to write (get raw data of artwork 1 of current track) to file starting at 0

This works, and my RealBasic app can then open the file from where AppleScript put it. This is messy though, and I’d like to do something like this:

tell application "iTunes"
return (get raw data of artwork 1 of current track)
end tell

and then use the returned value as the data for the image. The problem is that the data returned from this is not usable image data. It contains no header showing the format of the image, but starts instead with “«data”, then a newline, then “tdta” then loads of what appear to be hex characters. Saving this out to a file and opening it up with a hex editor does not reveal anything like an image file. Returning “data of artwork 1 of current track” rather than “raw data” has a similar effect, but with “«data” followed by newline, then “PICT” then the hex characters. This seemed more promising, but the data were still unusable as an image.

This is the only usage?

do shell script "touch ~/Desktop/artwork.png"
set thefile to ((path to desktop as string) & "artwork.png") as alias
set thefilehandle to open for access thefile with write permission
	tell application "iTunes" to write (get raw data of artwork 1 of current track) to thefilehandle starting at 0
end try
close access thefilehandle
open location "file://" & POSIX path of thefile

Here is an example app using Objective-C and the pasteboard. It might be useful.

Download here

@Dylan Weber
Thank you for your reply, but that seems to be the same method I’m already using. The main difference is that your script opens the file rather than leaving it there for my RealBasic app to find. It’s the creation of a file that I’m hoping to avoid.

@Craig Williams
That Obj-C example may well be useful. I’ve had a look, and it appears that the Applescript part retrieves the album art and puts it in the clipboard. The Obj-C part then does the following to “paste” the contents of the clipboard into the NSImageWell:

NSPasteboard *pboard = [NSPasteboard generalPasteboard]; NSData *pictData = [pboard dataForType:NSTIFFPboardType]; NSImage *img = [[NSImage alloc] initWithData]; [iv setImage:img]; [img release];
I’ll try this in RealBasic since it has clipboard classes, but I’m not sure whether this will yield results any different to when I attempted to return the image data to a variable.


Ok, the following works.

set imageData to my getImageDataFromiTunes()
if not imageData = "" then
    set the clipboard to imageData
end if

on getImageDataFromiTunes()
    tell application "iTunes"
            set sel to item 1 of (get selection)
        on error
            return "No track selected"
        end try
        if exists artworks of sel then
            return (get data of artwork 1 of sel)
            return "Selected track has no artwork"
        end if
    end tell
end getImageDataFromiTunes

And then in RealBasic:

GetArtworkClip //Calls the Applescript dim c as new Clipboard ImageWell1.Image = c.Picture
Only thing I need to work out now is how to pipe the contents of the RealBasic ImageWell through a web server - but that’s an issue for the RealBasic forum :wink:

Cheers guys!