# Fixing audiobook track titles (again)

I decided to relisten to the audiobook of Harry Potter and the Deathly Hallows, so I did the usual iTunesy stuff and synced the first few chapters to both my iPhone and my old second-generation iPod nano. When the syncing was done and I looked at both devices to make sure everything had worked, I realized I needed to make some changes to the track titles.

You see, I’m still working my way through Sean Wilentz’s Dylan book and the track titles of the two books have enough similarities to be confusing. Having two books on the iPhone is no big deal, as its user interface does a good job in keeping them separate.

In fact, the track titles as they appear in iTunes don’t even show up on the iPhone.

But it’s a different story on the nano. The nano puts all the book tracks together in the Audiobook category. Because these books were ripped from CDs, there are several tracks for each chapter, and they have names like “Chapter 01A - The Dark Lord Ascending.”1 On the nano’s small screen, the “Chapter 01A” part takes up most of the width, making it harder than it should be to distinguish between the two books. What I needed was a set of track names that had both the book name and the chapter/track info in the first several characters. Time to write a script.

I suppose I could have written it in AppleScript, but in my mind “AppleScript” and “search and replace” are phrases that aren’t tres bien ensemble. So I wrote the script in Python using the appscript library. Here it is:

python:
1:  #!/usr/bin/python
2:
3:  import appscript
4:
5:  selected = appscript.app('iTunes').selection.get()
6:
7:  for t in selected:
8:    oldname = t.name.get()
9:    newname = oldname.replace('Chapter', 'HP7 - Ch', 1)
10:    t.name.set(newname)


I selected all the Deathly Hallows tracks and ran the script to apply the changes. I then made the obvious edits and ran it on all the other Potter books and on the Dylan book, too.

(Why does Line 9 use a third argument of 1 in the replace method? That’s a bit of defensive programming I thought of after changing the Deathly Hallows track titles. If some of the tracks had had names like

Chapter 52C - The Final Chapter

they would have turned into

HP 7 - Ch 52C - The Final HP7 - Ch

which was not what I was looking for. The third argument limits the replacement to the first “Chapter” found in the title.)

The script made short work of an otherwise long and tedious process, and now I have tracks that can be distinguished at a glance.

1. The track names in the Dylan audiobook were more or less in this form when I ripped the disks. The Potter track names, on the other hand, were originally a mess, and I had to wrangle them into this form using an AppleScript described in this ripe old post. Such are the vagaries of the Gracenote database