applescriptitunesartworkitunesartwork

Delete multiple artworks in single track programmatically


I'm writing an AppleScript to manage my album artwork. I am trying to write some code that will delete all artworks contained in the selected tracks (iTunes tracks can contain up to 30 artwork objects). This would be the equivalent of selecting the tracks in iTunes, opening the 'Get Info' window, and checking the (blank) artwork box.

As an example, code that SHOULD work:

tell application "iTunes" to set t to track 1 of playlist "Example Playlist"
tell t to delete every artwork

Note that the syntax for deleting only the first artwork would be: tell t to delete artwork 1

Unfortunately, AppleScript is not cooperating. If the track has 2 artworks, only the first is deleted; if the track has 3 artworks, the first 2 are deleted.

It appears to be an issue with the artwork indices changing during execution of the delete command (i.e. for a track with 2 artworks, it successfully deletes artwork 1, but when it tries to delete artwork 2, it cannot find it because it has been renamed to artwork 1 when the original artwork 1 was deleted). Trying to coerce the every artwork specifier into a list (so that it can be reversed) doesn't work, as delete doesn't seem to work on a user-defined list.

Does anyone know what I'm doing wrong? I know that I could use a repeat loop to iterate through all the artworks in the track as delete each one individually, but this seems to go against the attempted simplicity of AppleScript. Am I somehow misusing the every statement?


Solution

  • Try:

    tell application "iTunes"
        set t to first item of (get selection)
        delete t's artworks
    end tell
    

    OR

    tell application "iTunes"
        set t to first item of (get selection)
        set tArtwork to t's artworks
        repeat with i from (count tArtwork) to 1 by -1
            delete tArtwork's item i
        end repeat
    end tell