Have I told you lately that I hate you?
January 31, 2011 at 8:37 PM by Dr. Drang
One of the reasons I dislike AppleScript is the “English-like” syntax that often comes around to bite you in the butt because it’s ambiguous. The bigger problem, though, is the wide variability in supported commands from application to application. This isn’t really AppleScript’s fault, but it’s hard to develop trust in a language when a construct like current item
works in four scripts, but throws an error in the fifth. I’ve never felt on solid ground when programming in AppleScript.
The lone exception has been AppleScripting in iTunes. For some reason, iTunes has been the one application in which I could generally trust my instincts and write AppleScript without obsessively checking and rechecking its Dictionary. Until today.
Over the weekend, I got a tweet from Pete Dickson (@petedickson), asking for some help with a script he’d seen in an old post. It was an early version of one of my BBC Radio 2 scripts, so I directed him to the up-to-date version and made a small suggestion as to where the old script might be failing him. Turned out I was completely off base. Pete found his own solution and in doing so pointed out an error in my code.
The error came in this bit of AppleScript,
24: tell application "iTunes"
25: set theFile to item 1 of theArgs
26: set theTrack to (add theFile)
27: delay 60
28: set bookmarkable of theTrack to true
29: set shufflable of theTrack to false
30:
31: add (get location of theTrack) to playlist "Radio shows"
32: set lyrics of theTrack to songlist
33: set data of artwork 1 of theTrack to theArt
34:
35: end tell
which
- imports a recording from Audio Hijack Pro (Line 26);
- sets the “Remember playback position” option (Line 28);
- sets the “Skip when shuffling” option (Line 29);
- puts it in a playlist (Line 31);
- adds lyrics (Line 32); and
- adds artwork (Line 33).
The problem was that Lines 28 and 29 weren’t doing their job for Pete. And when I looked back at some recent recordings I’d made, I found that those options hadn’t been set for me, either—I just hadn’t noticed it.
Now, there’s no question that code was working when I wrote it and that it continued to work for over a year afterward. Some recent update of iTunes broke it.
As I said, Pete figured out a fix, which is this:
24: tell application "iTunes"
25: set theFile to item 1 of theArgs
26: set theTrack to (add theFile)
27: add (get location of theTrack) to playlist "Radio shows"
28: delay 60
29: set bookmarkable of theTrack to true
30: set shufflable of theTrack to false
31:
32: set lyrics of theTrack to songlist
33: set data of artwork 1 of theTrack to theArt
34:
35: end tell
Can you see the difference? What was Line 31, the add to playlist
line, has been moved ahead of the set bookmarkable
and set shufflable
lines. That’s it. There are no new commands; there’s no new syntax. The only difference between what used to work and what now works is the time at which the track is put into the playlist.
I hate AppleScript.