AppleScript Delenda Est

ziggy on 2004-06-30T04:12:42

A few months ago someone mentioned an interesting sounding course on CD, How to Listen to and Understand Great Music. Through the magic of demographic profiling, I was getting this company's catalogs for a while, but ignoring them. Then I got an iPod, a job with a 1 hour commute (each way), and a lot of time to kill. So I bit the bullet and ordered a few courses.

Most of the courses are 30min long, 2 per CD, broken into 6 tracks each. This isn't the kind of thing that you'd expect to find in CDDB (but surprisingly, there were some hits). iTunes lets me set properties for an entire 36- or 48- lecture title, but I also want to name each track something meaningful like Lecture 1a or Lecture 12f.

For whatever reason, I thought it would be easiest to do this from inside iTunes. But with 200-odd tracks to edit, I don't want to do this by hand. And that means hacking some AppleScript together.

I've used AppleScript on and off over the years (mostly off), and every time I pick it up, I dread it. I've programmed in enough languages over the years that I can muddle through if I have some sample code to examine, good diagnostics when I make syntax or semantic errors, and a good language reference nearby.

With AppleScript, two of the three legs of that stool are generally missing, which makes the task ever so much more frustrating. But with enough help from the syntax highlighting and repeated runtime errors, I generally muddle through (or walk away, or find another solution entirely).

Herewith is my current script for bulk renaming of tracks. It probably won't be very useful for you, but it's another sample bit of AppleScript that google will be able to find for the next poor soul immersed in the frustration that is AppleScript.

tell application "iTunes"
    set p to the name of user playlists
    set selected to choose from list p
    if selected is false then
        return
    end if
    
    set reply to display dialog ¬
        "Segments per Lecture" default answer ¬
        "6" buttons {"OK", "Cancel"} ¬
        default button "OK"
    if button returned of reply is "Cancel" then
        return
    end if
    
    set segments to the text returned of reply
    set myplaylist to playlist named selected
    set trackcount to the count of tracks ¬
        of myplaylist
    set lectures to trackcount div segments
    
    set msg to selected & return & return ¬
        as string
    set msg to msg & ¬
        "Convert " & trackcount & ¬
        " tracks into " & lectures & " " & ¬
        segments & "-part lectures?"
    
    set reply to display dialog msg ¬
        buttons {"OK", "Cancel"} ¬
        default button "Cancel"
    
    set a to {}
    
    if button returned of reply is "OK" then
        repeat with lecture from 1 to lectures
            set base to (lecture - 1) * segments
            
            set the name of track (base + 1) ¬
                of myplaylist ¬
                to "Lecture " & lecture & "a"
            set the name of track (base + 2) ¬
                of myplaylist ¬
                to "Lecture " & lecture & "b"
            set the name of track (base + 3) ¬
                of myplaylist ¬
                to "Lecture " & lecture & "c"
            set the name of track (base + 4) ¬
                of myplaylist ¬
                to "Lecture " & lecture & "d"
            set the name of track (base + 5) ¬
                of myplaylist ¬
                to "Lecture " & lecture & "e"
            set the name of track (base + 6) ¬
                of myplaylist ¬
                to "Lecture " & lecture & "f"
        end repeat
    end if
end tell
(If Apple can get Automator to do that with a clickety-clickety-BAM!, I'll be really impressed.)


Applescript

merlyn on 2004-06-30T10:42:21

Every time I think I understand enough applescript to write some code, I write the code, and then it barfs. So I try something slightly different, and it still doesn't work. So I stare at the docs, and try again. Nope.

Eventually, after enough genetic processing (keeping the bits that work, and recombining them with other parts that work, and then randomly mutating the result), I can get something to work. And then I'm puzzled by what I'm staring at, because I would have never guessed that combination would do it.

This cycle has repeated for nearly everything I've done with AS. I can't believe people can actually be productive with this language, unless they spend at least an hour a day using it.

At least it's an example of a language that appears to be easier than Perl, but is actually harder! {grin}

Now I know

delegatrix on 2004-06-30T12:46:12

Is that what you've been up in the office?

Re:Now I know

ziggy on 2004-06-30T13:12:31

Off and on, yes. Mostly an hour here and an hour there when I'm importing lectures to iTunes. I had something less satisfying worked out that required editing the script each time it was run, and I was tired of that.

(And, no, this kind of thing shouldn't take an hour. Which is one reason why it is so frustrating.)

Re:Now I know

waltman on 2004-07-01T01:45:41

And how long would it have taken you to type in the data by hand? :)

Re:Now I know

ziggy on 2004-07-01T02:09:42

Less time, probably. But it feels more tedious and it's certainly more error prone.

A course with 48 lectures has 288 tracks to rename from "Track 01", "Track 02", etc. to something else. Even if it is faster to do (once) by hand, I'd rather have a program do it.

Of course, I can generate all of the names in about a minute with a oneliner, a macro or whatnot. The problem remains how to apply the new names to the tracks, and AppleScript (and iTunes) do not make this as easy as it should be.

So...

perrin on 2005-02-26T18:05:03

How was the lecture? I was thinking of ordering some stuff from them too.

Re:So...

ziggy on 2005-02-27T04:40:31

I've listened to a few of their courses, and they've been generally good. My problem is that I get interested in a topic, so I'll order a pair of courses in that area (Ancient Greece + Ancient Rome). Invariably, one professor is more engaging than another, like in a Dominus vs. Damian sort of way. I haven't listened to anything so bad that I've sent it in for a refund.

If you think something is interesting, then order it (while it's on sale) by all means. Everything they sell has a lifetime satisfaction guarantee.