iTunes music video import script

New hack: "Munge Videos.scpt". Well, it's not actually that new, but I haven't published it before, and it has grown a lot lately.

It's an iTunes AppleScript to process newly-imported music videos. Among other things, it: marks the track as being a "Music Video"; sets the "Comments" field to the resolution in pixels of the video; sets the "Volume Adjustment" based on the video's overall audio volume; copies the year, genre, rating, and capitalization from older versions of this same track; and adds this new track to all of the playlists of an old track that it appears to be intended to replace.

It's pretty hairy, and it's a really good reason to hate AppleScript. I'd appreciate code critiques from anyone who thinks they actually understand this bagbiting language.

PS: If anyone can tell me how to add a track to a playlist at a particular position, e.g., "add track B after track A in playlist X", that would be swell.

Previously, previously, previously, previously.

Tags: , , , ,

7 Responses:

  1. bode says:

    Thank you JWZ for expanding my vocabulary!

    "The original loading of these terms was almost undoubtedly obscene, possibly referring to a douche bag or the scrotum (we have reports of “Bite the douche bag!” being used as a taunt at MIT 1970-1976, and we have another report that “Bite the bag!” was in common use at least as early as 1965), but in their current usage they have become almost completely sanitized."

  2. pavel_lishin says:

    I just now realized that (apparently?) you can't edit applescript scripts with a plain ol' text editor.

    This pretty much ensures that you'll never receive a code critique from me, at least.

    • jwz says:

      You can do #!/usr/bin/osascript and it will work from the command line, but you can't put that file in the iTunes "Scripts" directory.

      • pavel_lishin says:

        Oh, I was just talking about reading the actual source - which you can apparently do with /usr/bin/osadecompile.

        (And having read some of it, oh my god, it looks like the programming languages I used to read about in books I pulled out of the public library dumpster.)

  3. dunham says:

    I usually to use the python "appscript" module for this kind of stuff, because I can't really figure out how to do anything non-trivial in AppleScript.

  4. Rob Mayoff says:

    Use the "move" command on every track in the playlist to the end of the playlist. Use copy at the appropriate time to insert the new track. Here's my test case. Looks like the blog is going to hide the indentation.


    tell application "iTunes"

    -- The test setup. This adds Plainsong to my Cure playlist, immediately after Just Like Heaven. If Plainsong is already in the playlist, it gets added again.
    set thePlaylist to user playlist "Cure"
    set plSize to (thePlaylist's every file track as list)'s length
    set newTrack to playlist 1's first file track whose name is "Plainsong"
    set refIndex to ((thePlaylist's first file track whose name starts with "Just Like Heaven")'s index)

    -- The "algorithm".
    repeat with i from 1 to refIndex
    move thePlaylist's file track 1 to thePlaylist's end
    end repeat
    copy newTrack to thePlaylist's end
    repeat with i from refIndex + 1 to plSize
    move thePlaylist's file track 1 to thePlaylist's end
    end repeat
    end tell

    • sherm says:

      I assume that completes in a matter of, I don't know, decades if one has something like a jwz-illion items in the playlist?