Home > Articles > Home & Office Computing > Mac OS X

Scripting Mac OS X

📄 Contents

  1. How iTunes Stores Songs
  2. Listing Songs
  3. Wrapping Up
  • Print
  • + Share This
Use the power of the Terminal to explore your iTunes library! This article by Dave Taylor won't make you a shell scripting maven. Instead, it shows you how to learn more about your music library and iTunes by creating a Unix shell script.
Like this article? We recommend

Since the very dawn of the Macintosh, programming has been the domain of high-power software developers who learn complex and powerful languages such as C++ or Java, or take the time needed to learn developer workbenches, GUI-based app development tools, or similar. If you've thought about scripting at all, it's doubtless been tightly intertwined with AppleScript, a straightforward but Mac-only pseudo-English scripting language with extensive OS support.

But there's another way that you can push your Mac around a bit and change its behavior—a simple way that involves surprisingly little programming or effort on your part, other than learning some of the rudiments of working with the Terminal application on your Mac OS X system. It's called shell scripting, and if you can type in a few commands on a command line and get the results you seek, you're probably ready to jump right into scripting today!

Before we travel down that river, though, make sure you keep your arms and legs inside the vessel at all times, and that you launch the Terminal application by first finding it in Applications -> Utilities. Try typing ls -l at the command line to see how it feels to not use your mouse to interact with your Mac. Not too bad, is it?

How iTunes Stores Songs

The purpose of this article isn't to lead you through all the steps needed to become a shell scripting maven; it's to look at how you learn more about your music library and iTunes by creating a Unix shell script. So let's get started already.

Like many different Mac OS X applications, iTunes stores all its preferences in an XML-formatted preferences file that we'll have to read through to figure out where the iTunes music library is stored on the computer. Fortunately, this XML file is always in the same spot on the computer—where $HOME is your home directory on your Mac system (it's automatically expanded to your home directory by the shell, however, so don't worry about having to define it):

$HOME/Music/iTunes/iTunes Music Library.xml

Let's have a peek:

$ grep '>Music Folder<' $HOME/Music/iTunes/iTunes\ Music\ Library.xml 
    <key>Music Folder</key><string>file://localhost/Users/taylor/

I already know that the specific value I seek is "Music Folder", so I use the grep utility to show me just the line that matches that pattern in the configuration file. You can see, though, that it's all pretty ugly XML and formal URL format, so there's a little bit of additional work involved in identifying the location of the iTunes library, which can be accomplished in a typical Unix fashion with a few commands strung together to pull things apart and put them back together the way I want:

ituneconfig="$ithome/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

Now, let's take a small sidetrack and turn these few commands into an actual shell script. Fortunately, this is easily done by prepending the following line and then including all the commands in a simple text file:


How do you create this file? You can use something like Microsoft Word if you really want to (just make sure to save it as ascii only with line breaks rather than a .doc file), but your best bet is really to use one of the editors accessible from within Terminal: pico or vi.


There are good tutorial introductions to both editors on the Net: Google tutorial introduction and the name of the program, and you'll be set.

I suggest that you call the newly created shell script file itunesinfo.sh and save it in your home directory (which is the directory you start out in when you launch Terminal each time). After that's done, there's one more step needed, and that's to make the script executable so that the shell will be happy running it. That's easy to do with the chmod command:

chmod +x itunesinfo.sh

Assuming that all is well, you should now be able to type in the name of the script and have it tell you where your iTunes library is located:

$ ./itunesinfo.sh
Your music library is at /Users/taylor/Music/iTunes/iTunes Music/

That's not too exciting, so let's see what we can do with basic Unix commands now that we have a shell script that knows how to find the iTunes music library.

  • + Share This
  • 🔖 Save To Your Account