MacDevCenter    
 Published on MacDevCenter (http://www.macdevcenter.com/)
 See this if you're having trouble printing code examples


Cleaning iTunes

by brian d foy
01/17/2003

Over the holidays, I was away from any sort of network, so I started to clean up things on my laptop. During this process, I decided to take a look at the 20GB of MP3s residing on my hard disk to see how badly the Gracenote CD Database (CDDB) had classified them. From there, I could decide how much effort I'd have to expend to clean things up.

What is the CDDB, and How Does It Work?

The CDDB works well for "popular" music -- that stuff that gets put into "Rock," "Alternative," and so on. It works less well for compilations, like "Soundtracks," since the phrase "Various Artists" ends up in the artist portion of the ID. Apparently, it works poorly for show tunes -- people seem to really care about the performers, so they get really specific with the artist portion of the ID, to the extent that every song on the CD has a different artist string even though it is the same cast. People almost always butcher western art music ("Classical" to most people) so that any information may show up anywhere -- track name in artist, performer in CD name, different languages in descriptions for different tracks, and then the ones that are really bad that I just type in myself.

Part of the reason for this is the history of MP3 ID3 tags. The first version, ID3v1, had a limited number of fields that had short character limits. A later version, ID3v2, had more fields and did not have the length restrictions of ID3v1. For instance, ID3v1 does not have a "Composer" field, and ID3v2 does. Complete details, as well as non-technical overviews, on both versions and future directions are on the ID3 Web site. iTunes can use either version, and can convert ID3v1 tags to ID3v2 tags.

The CDDB was originally a community project where the public submitted the CD data and the CDDB made it available to everyone else. They incorporated as CDDB, Inc. in 1995, and eventually renamed themselves Gracenote as they diversified their business. Their submission guidelines hint at the many different forms in which people have submitted data. Some of the things I want to clean up are bad data, and some are matters of Gracenote policy I disagree with, like inserting "(Disk N)" at the end of album names for multi-CD sets. In my iTunes Music Library, I get to have things my way.

Tidy Up Those Descriptions in iTunes

Related Reading

Mac OS X Hacks
100 Industrial-Strength Tips & Tricks
By Rael Dornfest, Kevin Hemenway

Some of the CD descriptions are easy to fix, and I can use some scripts from Doug's AppleScripts. The "Remove n Characters From Front" script removes the same number of characters from the names of all of the tracks in the current selection, so I can make a name like "01 First Track" into "First Track" (and the "Number Tracks of Playlist" will go the other way). If the artist and track names are reversed, the "Swap Track & Artist" script works nicely.

I started to work on a little Perl script to do this stuff for me, but since I use a Mac, I get to use iTunes. As I tried a few things in iTunes, I discovered that I could get the job done faster and easier with very little programming. Most of the changes relate to special cases anyway, so automating the task is not that helpful. Sometimes jumping into programming is the wrong way to go at first.

If I want to change the information for one track, I use the "Get Info" command from the "File" menu, which displays a dialog with three tabs. Under the "Tags" tab I update the appropriate information.

Screen shot.
Get Info dialog "Tags" tab

If I change the tag in iTunes, it updates the iTunes database and playlists, updates the ID tag in the actual MP3 file, and moves the file to a new artist directory if my music is in the iTunes Music Library. This is all very cool. I wanted to reorder the directory structure by artist anyway, and now I do extra work to do that once I fix things. I also use the MP3-Info Contextual Menu Item plug-in to ensure that the changes I make in iTunes show up in the MP3 file, and sometimes to perform simple edits.

Screen shot.
MP3-Info Contextual Menu items.

I can also select several tracks from a playlist and affect them in parallel. I make a contiguous selection by holding down the Shift key, selecting the first item, then selecting the last item, or a non-contiguous item by holding down the Apple key and selecting only the items I want. With multiple items selected, I choose the "Get Info" menu item again and get a similar dialog, but this time, the dialog does not have tabs and only shows the information that all of the tracks have in common; for instance, the same artist or album name. Any changes I make affect all of the tracks. Again, very cool if the change is simple.

Screen shot.
Get Info dialog for multiple items

And Those Changes That Aren't Simple ...

Often the change is not simple. While trying to fix the ID tags for Glenn Gould's English Suites, I needed to take part of the old title string and combine it with part of the old artist string. The title had the name of the major work, while the artist had the name of the movement in that piece (instead of, say, "Glenn Gould" or "Johann Sebastian Bach"). Each track had a unique artist string, and some tracks had the names of the major work, while others were left blank (ending up with "Track 02" and so on) meaning that they were the same as the previous track.

Screen shot.
English Suites with CDDB default values.

This looks like a tough programming problem, but it really is not that bad if I break it into little problems. Too often I see programmers (oh, that includes me too) try to solve several problems at once, which makes the solutions very complex, and complex solutions make for complicated maintenance. The Unix paradigm uses small tools in pipelines to accomplish tasks, and often I start making small tools to get the job done.

The first problem is getting the track names in order. If I can make the "Track N" strings match the major work name, I have solved one problem, and the next problem, combining that name with the movement name, is much easier.

A Little AppleScript to the Rescue

I wrote a little AppleScript to grab the selected tracks in the playlist, grab the "name" of the first track in the selection, and use that for the name of the remaining tracks. I need to be careful here -- depending on which way I have the playlist ordered (ascending or descending in a certain column), the first track may be different than what I see as the first track in the window.

This is a quick script to prove to myself that this idea will work. I do not want to get too deep into a problem before I find out if it is not going to work.

tell application "iTunes"
    set myWindow to browser window 1
    set mySelection to selection of myWindow
    set myNewName to name of first item in (get the selection of browser window 1)
    repeat with myTrack in (get the selection of browser window 1)
        set name of myTrack to myNewName
    end repeat
end tell

I used this little script for a couple of CDs, and messed up a couple of times because the right name did not end up in myNewName. I added a dialog to allow me to choose the correct track name from all of the selected tracks, and another dialog to confirm the selection and give me a chance to edit it.

tell application "iTunes"
    set myNames to name of selection of browser window 1
    set myNewName to choose from list myNames with prompt "Which name?" multiple selections allowed 0
    if myNewName is false then --nothing selected, just stop
        return
    end if
    display dialog "Use this name?" default answer myNewName buttons {"Cancel", "Ok"} default button 1 with icon 1 giving up after 5
    if the button returned of the result is "Cancel" then
        return
    end if
    
    repeat with myTrack in (get the selection of browser window 1)
        set name of myTrack to myNewName
    end repeat
end tell

After I use this script, the track names should be half-right. I still need to combine them with the string in the artist portion. I do this a bit differently, so I do not need to select tracks, since I want to change all tracks in the playlist at the same time -- I easily create temporary playlists by dragging a group of selected tracks into the playlist window. Once I have those tracks isolated in their own playlist, I have an easier time working with them, even if I de-select them. I tell iTunes to cycle through all of the tracks, join the name and artist strings with a ", ", then set the name to the combined string. Once the string in the artist section is safely in the track name, I set the artist string to the real artist, which for me means the performer (iTunes has a separate column for Composer -- see the "Edit -> View Options..." menu item to see how you can change the columns in iTunes).


Screen shot.
iTunes View Options dialog.


tell application "iTunes"
    set myPlaylist to playlist "To Do"
    repeat with i from 1 to count of tracks of myPlaylist
        set myTrack to track i of myPlaylist
        set myArtist to artist of myTrack
        set myName to name of myTrack
        set myNewName to myName & ", " & myArtist
        set name of myTrack to myArtist
        set artist of myTrack to "Glenn Gould"
    end repeat
end tell

Once I have the basic idea of the script to combine the artist and track names, I fill out the script with dialogs to select the playlist and the artist name so I do not have to change the script for every group of changes that I want to make.

Screen shot.
AppleScript dialogs for iTunes script.


tell application "iTunes"
    set allPlaylists to name of user playlists
    set myPlaylist to choose from list allPlaylists with prompt
"Which playlist?" multiple selections allowed 0
    if myPlaylist is false then --nothing selected, just stop
        return
    end if
    
    display dialog "What is the new artist name?" default answer "Glenn Gould" buttons {"Cancel", "Ok"} default button 1 with icon 1 giving up after 5
    if the button returned of the result is "Cancel" then
        return
    end if
    
    repeat with i from 1 to count of tracks of myPlaylist
        set myTrack to track i of myPlaylist
        set myArtist to artist of myTrack
        set myName to name of myTrack
        set myNewName to myName & ", " & myArtist
        set name of myTrack to myArtist
        set artist of myTrack to "Glenn Gould"
    end repeat
end tell

Once I get the scripts just how I like them, I add them to the iTunes Scripts directory (/Users/user-name/Library/iTunes/Scripts), which I had to create myself since it did not exist. When iTunes recognizes the directory, an AppleScript icon shows up to the left of the Help menu, and the names of the scripts in the directory show up in the Scripts menu, ready for use.

Screen shot.
iTunes menu bar with AppleScript icon.

Wrapping Up

In a short time, I created two scripts that clean up quite a bit of classical music information in my iTunes Music Library. AppleScript gives me easy access to almost everything that iTunes knows, and lets me automate repetitive tasks. Other problems that I need to fix are special cases or different situations, so I create other scripts for them, first trying very small scripts to make sure the idea works, then adapting the small script for general use. Very quickly I have a powerful set of tools to fix most of the problems I find in my MP3 files without having to do a lot of typing. Now I can spend time listening to my music rather than categorizing it.

brian d foy is a Perl trainer for Stonehenge Consulting Services and is the publisher of The Perl Review.


Return to the Mac DevCenter.

Copyright © 2009 O'Reilly Media, Inc.