How to automatically add artwork to iTunes song using AppleScript?

878 views Asked by At

I made an automator workflow which can add songs to my iTunes library and set their info by the Artist name, Album name, and Song Name. However, the tracks are coverless when I import them so I was looking for ways to automatically add artwork. One of the ways was to use the iTunes "Get Album Artwork." The problem with this is that it often didn't give very good artwork if it even provided it at all.

I decided to download artwork for songs I didn't have artwork for which are stored in my "Downloads" folder. The input I pass into my script is the .jpg file which I want to add to the song as artwork. The problem is that whenever I run this script, it says that it can't make the alias file into a file type. An error message is as follows:

Can’t make alias "Macintosh HD:Users:Nilay:Downloads:Avicii - True - Hey Brother.jpg" into type file.

There is no reason it shouldn't be able to find the file because I find the file using the Automator "Filter Finder Items" so I know it's passing in the correct file.

I tried to pass in the text of the file rather than the actual file but that still resulted in the same message. The first few comments are a few ways I have tried to get around this error message but alas, none worked.

I was wondering if anyone knew how to solve this error message or another way which I could add custom artwork to the song without manually having to go to iTunes -> Select Song -> Get Info -> Artwork -> Add Artwork. It would also be feasible if someone could explain how to automate this action using AppleScript or Workflow. Thank you!!!

on run {input, parameters}

    --set jpegFilename to input
    --set jpegFilename to "/path/to/some/artwork.jpg"
    --set jpegFile to (POSIX file jpegFilename)
    --set jpegFile to (jpegFilename as POSIX file)
    --tell application "System Events" to set jpegFile to (input as POSIX file)
    set jpegFile to input

    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 jpegFile)
        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 jpegFile) 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 jpegFile)
    end tell

end run
1

There are 1 answers

2
vadian On

input is a list of alias specifiers, first of all you need a repeat loop

repeat with jpegFile in input

or get the first item of the list

set jpegFile to item 1 of input

Then open command in Image Events expect an alias specifier anyway therefore any coercion is not needed.

set myImage to open jpegFile