How to Producing Summary Listings of iTunes Libraries using script

If you've used the excellent Mac OS X application iTunes for any length of time, you're sure to have a massive playlist of CDs that you've scanned, downloaded, swapped, or what-have-you. Unfortunately, for all its wonderful capabilities, iTunes doesn't have an easy way to export a list of your music in a succinct and easy-to-read format. Fortunately, it's not hard to write a script to offer this functionality.

The Code
#!/bin/sh

# itunelist - Lists your iTunes library in a succinct and attractive
# manner, suitable for sharing with others, or for
# synchronizing (with diff) iTune libraries on different
# computers and laptops.

itunehome="$HOME/Music/iTunes"
ituneconfig="$itunehome/iTunes Music Library.xml"

musiclib="/$(grep '>Music Folder<' "$ituneconfig" | cut -d/ -f5- | \
cut -d\< -f1 | sed 's/%20/ /g')"

echo "Your music library is at $musiclib"

if [ ! -d "$musiclib" ] ; then
echo "$0: Confused: Music library $musiclib isn't a directory?" >&2
exit 1
fi

exec find "$musiclib" -type d -mindepth 2 -maxdepth 2 \! -name '.*' -print |
sed "s|$musiclib/||"

How It Works
Like many modern computer applications, iTunes expects its music library to be in a standard location — in this case ~/Music/iTunes Music Library/iTunes Library/ — but allows you to move it elsewhere if desired. The script needs to be able to ascertain the different location, and that's done by extracting the Music Folder field value from the iTunes preferences file. That's what this pipe accomplishes:

musiclib="/$(grep '>Music Folder<' "$ituneconfig" | cut -d/ -f5- | \
cut -d\< -f1 | sed 's/%20/ /g')"

The preferences file ($ituneconfig) is an XML data file, so it's necessary to do some chopping to identify the exact Music Folder field value. Here's what the Music Folder value in my own iTunes config file looks like:

file://localhost/Volumes/110GB/iTunes%20Library/

The Music Folder value is actually stored as a fully qualified URL, interestingly enough, so we need to chop off the file://localhost/ prefix, which is the job of the first cut command. Finally, because many directories in Mac OS X include spaces, and because the Music Folder field is saved as a URL, all spaces in that field are mapped to %20 sequences and have to be restored to spaces by the sed invocation before proceeding.

With the Music Folder name determined, it's now easy to generate music lists on two Macintosh systems (or even an iPod!) and then use the diff command to compare them, making it a breeze to see which albums are unique to one or the other system and perhaps to sync them up.

Running the Script
There are no command arguments or flags to this script.

The Results
$ itunelist | head
Your music library is at /Volumes/110GB/iTunes Library/
Acoustic Alchemy/Blue Chip
Acoustic Alchemy/Red Dust & Spanish Lace
Acoustic Alchemy/Reference Point
Adrian Legg/Mrs. Crowe's Blue Waltz
Al Jarreau/Heaven And Earth
Alan Parsons Project/Best Of The Alan Parsons Project
Alan Parsons Project/Eve
Alan Parsons Project/Eye In The Sky
Alan Parsons Project/I Robot

Hacking the Script
All right, this isn't about hacking the script per se, but because the iTunes library directory is saved as a fully qualified URL, it would be most interesting to experiment with having a web-accessible iTunes directory and then using the URL of that directory as the Music Folder value in the XML file....