Search code examples
applescriptmp3itunes

Folder image to iTunes artwork in applescript : not working anymore


Anyone of you guys could point me to why this applescript (which was working on snow leopard) does not work anymore on 10.7 Lion.

It intend to copy the file named "folder.jpg" from each album folder into ID3 tags of selected mp3 in iTunes.

property tempfile : ((path to temporary items as string) & "itunespicturefile_temporaire.pict")
global vPictFolder


tell application "iTunes"
    set v_TrackSelection to selection
    if v_TrackSelection is not {} then
        repeat with vTrack in v_TrackSelection
            set vPictFolder to my (ParentFromPath(location of vTrack as string)) as text
            set thisPict to my getpictData("folder.jpg")
            if thisPict is not "" then
                delete artworks of vTrack
                set data of artwork 1 of vTrack to (thisPict)
            end if
        end repeat
    else
        display dialog ("select tracks first !")
    end if
end tell

on getpictData(vAlbumpictName)
    tell application "Finder" to tell file vAlbumpictName of folder vPictFolder to if exists then
        set t_file to it as string
    else
        display dialog vPictFolder & vAlbumpictName
        return ""
    end if
    do shell script "/opt/local/bin/convert " & quoted form of POSIX path of t_file & " " & quoted form of POSIX path of tempfile
    display dialog "ok"
    return read (tempfile as alias) from 513 as picture
end getpictData

on ParentFromPath(thePath)
    set wantPath to true
    set thePath to (thePath as text)
    set saveDelim to AppleScript's text item delimiters
    set AppleScript's text item delimiters to {":"}
    set pathAsList to text items of thePath
    if the last character of thePath is ":" then
        set idx to (the number of text items in thePath) - 2
    else
        set idx to -2
    end if
    if wantPath then
        set folderName to ((text items 1 through idx of pathAsList) as text) & ":"
    else
        set folderName to item idx of pathAsList
    end if
    set AppleScript's text item delimiters to saveDelim
    return folderName
end ParentFromPath

Solution

  • Because Picture format (".pict") is not supported on Lion and newer OS. You can use the term picture to read a image file (JPEG, PNG, ...), the format will not be in the Picture format , but the original format of the file.

    tell application "iTunes"
        set v_TrackSelection to selection
        if v_TrackSelection is not {} then
            repeat with vTrack in v_TrackSelection
                set vPictFolder to my (ParentFromPath(location of vTrack as string)) as text
                set thisPict to my getpictData(vPictFolder & "folder.jpg")
                if thisPict is not "" then
                    delete artworks of vTrack
                    set data of artwork 1 of vTrack to (thisPict)
                end if
            end repeat
        else
            display dialog ("select tracks first !")
        end if
    end tell
    
    on getpictData(tFile)
        try
            return (read (tFile as alias) as picture)
        end try
        display dialog tFile
        return "" -- not exists
    end getpictData