Using Java to Expand iTunes Functionalityby David Miller
fans have had hooks into iTunes
for several years, which makes the information in your music library
accessible to other scriptable applications. And while AppleScript can
be glued together with other components in OS
X (via the
/usr/bin/osascript command in the terminal
do shell script command), attempting to provide
your music information to non-scriptable applications or resources (example:
a web site powered by PHP
and MySQL) has traditionally
been done in hack-and-scratch ways.
MyTunes fills that void by providing an easy-to-use and extensible framework that allows use of your iTunes library via Java. But it's also a case study in integrating open standards with Apple's iLife applications to provide extra functionality. This article contains an introduction to MyTunes, in addition to the basic concepts of how and why it works the way it does.
What's in a Name?
MyTunes has been around for several years in a previous form; it was originally an AppleScript that created an XML-ish file from the iTunes library, which was then parsed by a Perl script to load into a MySQL database (hence the name: MySQL + iTunes = MyTunes). However, iTunes version 4+ creates a file called iTunes Music Library.xml in your music library folder, so the first half of the old version was no longer necessary. And because the Perl script had to be rewritten to parse the new file that is automatically maintained by iTunes, the package was overhauled to:
- Make it extensible, so that I can easily change it in the future should the need arise.
- Make it easier to use, so that other people can use it and customize the tools to their needs.
Contained in the iTunes Music Library.xml file is just about every piece of data about your music collection that you could possibly want:
- Application Data: version, library location.
- Tracks: name, artist, album, length, rating, comments, etc.
- Playlists: name, whether or not the playlist is "smart", and all tracks that belong to it.
To see what other information is included, open it up in your favorite text editor and take a peek.
A little bit later on, we'll walk through an example of how the bigger and better MyTunes accomplishes the task previously completed by its predecessor, and in doing so, provides a reusable framework to avoid rewriting the meat and potatoes; instead, we'll be spending our time using the information, rather than retrieving the information. But before we start playing around with MyTunes, we'll need to install the necessary components that comprise the package.
Downloading & Installing MyTunes
As with any other time that you're dealing with XML, the first thing that you'll need is an XML parser. MyTunes uses the Apache Software Foundation's Xerces parser, which is freely available here. You'll also need Apache's XML-RPC library to take advantage of the new "remote control" features available (but more on that later).
The latest version of MyTunes is available here
(included in the distribution is the currently incomplete javadoc
documentation -- feel free to refer to the appropriate page when we're
working our way through the examples). Be sure to add these files to
CLASSPATH (refer to this article
to set up the
CLASSPATH environment variable), or place
all of the JAR files in the /Library/Java/Extensions/ folder,
as all classes located here are automatically made available to your
One of the major goals of the new version of MyTunes is to make it easy for other people (besides myself) to use. One step in accomplishing this goal is to use a configuration file that holds all necessary parameters; all classes in the package that have parameterized variables will look for the configuration file located at ~/.mytunes.xml. A sample file is located here.
Once you've downloaded the sample to your desktop, place it in the proper location with the following command:
mv ~/Desktop/mytunes.xml ~/.mytunes.xml
You'll notice that it won't show up when you view your home directory
in the Finder. This is because every file that has a "
as the first character in its name is hidden. In order to view and edit,
you'll have to use the Terminal in conjunction with a text editor such
You should see something similar to the following.
As you can see from the above image, there are several groups of properties:
librarytells us where our iTunes Music Library.xml file is located.
remote.*parameters are for the remote control features (via XML-RPC).
mysqlimport.*parameters are for the
Chordthat we'll begin creating.
Once you have the necessary components installed and have set the proper values in your configuration file, open a Terminal window and enter the command:
You should see something similar to the following:
If you receive a
ClassNotFoundException, then one (or more)
of the JAR files aren't located in /Library/Java/Extensions/
folder or another location specified in your
One other problem that you might run into is a thrown
this will arise when you are not connected to the Internet when using
MyTunesLibrary. This situation stems from the fact that
iTunes Music Library.xml's DTD
(the guide saying how a particular XML document is constructed) is declared
PUBLIC, and as such, it is located on Apple's servers.
Thus, the XML parser attempts to load the document from this location,
and throws a
java.io.IOException when the file can't be
Looking at the sample output, we can see that I currently have 1839
tracks and 20 playlists in my iTunes library. However, this is the only
MyTunesLibrary provides on its own; while
MyTunesLibrary parses the information in the library (track
names, albums, the playlists that songs belong to, etc.), the class
doesn't actually do anything with the data. This separation of logic
allows us to reuse the class in different applications without rewriting
the bulk of the code (which mainly consists of parsing the XML file
to extract the desired information).
With that in mind, in order to use the information made available to
us, we have to create a class that has appropriate methods invoked by
MyTunesLibrary as callbacks. For anyone who has worked
with SAX before,
ContentHandler interface should spring immediately
to mind; with SAX we receive notifications when we've reached XML elements,
text, or other items. While we're indirectly interested in this information,
we want to abstract the XML to a higher level than tags and attributes.
While MyTunes' callback structure is similar to SAX, the XML file is actually parsed with DOM. The reasoning behind this choice is mainly because of the non-semantic markup in iTunes Music Library.xml; the entire library is described by approximately 10 tags, which makes keeping track of our location in the file while parsing problematic. Because we are using DOM, there is a chance that you could run into memory issues if you have a large music library. In the event that you do run into memory errors, try increasing the memory allocated to your JVM to fix the problem.
Pages: 1, 2