applescriptitunesartwork

Getting artwork from current track in Applescript


I'm trying to make a script to pull the artwork of the currently playing track and write it to a file. I've checked out a few guides but none of them seem to work, any tips?

tell application "iTunes"
write artwork 1 to "path:to:desktop" of type("JPEG")
end tell

To be honest I've no idea what I'm doing. Anyone feeling helpful?

Thanks


Solution

  • You can write raw data of artwork 1 of current track to a file:

    -- get the raw bytes of the artwork into a var
    tell application "iTunes" 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 desktop) as text) & "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
    

    You might also use http://dougscripts.com/itunes/scripts/ss.php?sp=savealbumart, but it doesn't include source code.