Control iTunes Under Windows

Control iTunes Under Windows

Use a simple open source library to monitor and control Windows iTunes from your Swing application.

Windows doesn't have a standard scripting API like Apple Events, but it does have another object model that iTunes supports. Using an open source library, this hack will show you how to script iTunes just as easily on Windows as you can on the Mac.

Working with COM

The Component Object Model (COM) is a standard way for Windows components to expose functionality that other programs can call at runtime. com4j ( is an open source library that creates connections from Java programs to COM objects. com4j has two parts: a command-line program to create the Java interfaces that your program will call, and a native library that binds your program to the COM object at runtime.

com4j uses class annotations to do its magic, so you can only use it with Java 5.0 or greater.

To get started, download the com4j package at With the com4j stubber and the iTunes executable in your current directory, you can generate the interfaces like this:

java -jar tlbimp.jar -o jtunes -p test.jtunes iTunes.exe

This command will load the iTunes executable and look for COM definitions. Once they are located, tlbimp will generate a bunch of Java interfaces in the test.jtunes package and put the .java files into the jtunes directory. If you look at the generated Java interfaces, you will see a whole slew of methods and objects for playing, querying tracks, and dealing with virtually every other feature of iTunes. com4j will also pull out any embedded documentation and insert the documentation as JavaDoc comments in the generated interfaces.

This process is pretty quick, so you may find it useful to call it from Ant as part of your compile process.

Once you have the interface stubs, you can create a program to control iTunes quite easily. You can use the same program that you did when controlling iTunes on the Mac [Hack #82]. Just replace the action listener with the class in Figure.

A listener to control iTunes on Windows
	import test.jtunes.*;
	public class WinItunes {
		public void actionPerformed(ActionEvent evt) {
			try {

				IiTunes itunes;
				itunes = ClassFactory.createiTunesApp();
			} catch (Excepti(Exception ex) {
				System.out.println("exception : " + ex.getMessage());

Compile this class along with the interfaces in the test.jtunes package. You will also need the com4j.jar in your CLASSPATH and com4j.dll files in your PATH. When you run the program, the com4j library will connect to iTuneslaunching it if necessaryand execute the playPause() method.

com4j only allows you to call methods from the same thread that you used to create the COM proxy. Typically, you want to update your Swing components with iTunes information, which you can do safely from the Swing event thread only. This means that you should create the COM proxy from the event thread as well (using the ClassFactory method). Unfortunately, this may cause your application to block for a few seconds while iTunes loads (if it's not already running). To avoid this delay, you probably want to do all of your iTunes communication through a custom queue, or use the new concurrency utilities available in Java 5.0. The com4j developers are working on a solution to this problem, so it may be solved by the time you read this.

Get Track Information

As with Apple Events on the Mac [Hack #82], the COM interface gives you a way to query the currently playing track. You can call iTunes.currentTrack() to get an IITTrack object. This object has methods to query just about anything you could possible want to know about a track, including the artist, album, playing time, encoding method, and even the import date. Each method on the IITTrack object returns information as Strings or Java primitives, so it's pretty easy to access anything you want and then stuff it into your Swing interface. The following code shows how to get the track number, count, name, album, and artist:

	IITTrack track;
	track = itunes.currentTrack();
	int track_number = track.trackNumber();
	int track_count = track.trackCount();

	String track_name =;
	String album_name = track.album();
	String artist_name = track.artist();

com4j is a great open source project that unleashes the power of Java code integrated with native applications. The iTunes COM interface provides hooks for virtually everything that iTunes can do. These two things mean you could write a program to sort songs, create new playlists, or even export track listings to your own application that prints CD labels. You can find fscom/sdk/itunescomsdk.html, so see what other cool things you can come up with.

     Python   SQL   Java   php   Perl 
     game development   web development   internet   *nix   graphics   hardware 
     telecommunications   C++ 
     Flash   Active Directory   Windows