# Simple AppleScript tuneup for iTunes

My iTunes library has many songs that I ripped back in my Linux days, when I got track information from freeDB, a service that was started up when Gracenote changed the licence on the previously-free CDDB database. As with Gracenote--but to a greater extent than with Gracenote--some of freeDB’s entries are a little funky.

One set of entries that’s been bothering me for years is Creedence Clearwater Revival’s Chronicle album of greatest hits. The name field for every track on that album is prefixed with “CCR - ”. Thus,

CCR - Susie Q
CCR - I Put A Spell On You
CCR - Proud Mary

and so on. Eventually I got tired of seeing this and wrote the following AppleScript to delete the six-character prefix.

1:  tell application "iTunes"
2:    set sel to selection
3:    repeat with theTrack in sel
4:      set theName to name of theTrack
5:      set len to length of theName
6:      set name of theTrack to (characters 7 thru len of theName as text)
7:    end repeat
8:  end tell


It probably took a bit longer to write the script than it would have to change the 20 or so tracks by hand, but it was more interesting. As you can see from Line 2, the script assumes that all the tracks that need to be changed are selected before the script is run.

The first time through, I thought Lines 5 and 6 could be the single line

set name of theTrack to (characters 7 thru last of theName as text)


but apparently the “last” specifier can’t be used that way. Hence the “len” variable in Line 5 to get the index of the last character. (Had I written this in Python and appscript, I could have done that with a single line because Python has a simple way of indexing the end of the string without knowing the string length. But by the time I realized “last” wouldn’t work the way I wanted, I didn’t feel like rewriting in Python.)

The “as text” stuck on the end of Line 6 is needed because without it “characters 7 thru len of theName” will return a list of characters instead of a string.

This is very much in the Unix tradition of one-shot, throwaway scripts. It solves a specific problem by automating a tedious process and will never be used again.