Automating/scripting iTunes on Windows

Apple has posted an SDK for accessing iTunes using COM on Windows (it's funny, I noticed they left .DS_Store files in the zip archive!). It includes sample javascripts for doing things like creating playlists. You can actually write these scripts without downloading the SDK. What the sdk gives you (in addition to these samples) is the headers you'll need to access this from C with straight COM, and documentation of the object model.

An example of iterating through the tracks through javascript looks like:

var   iTunesApp = WScript.CreateObject("iTunes.Application");

var   mainLibrary = iTunesApp.LibraryPlaylist;

var   tracks = mainLibrary.Tracks;

var   i;


for (i = 1; i <= numTracks; i++)


      var   currTrack = tracks.Item(i);

      WScript.Echo(currTrack.Name + " by " + currTrack.Artist);


You don't really need it though - the type library is built into iTunes.exe. For example, you can from you C# app, you can add a reference to iTunes by right clicking the references section, choosing Add Reference..., going to the COM tab, and selecting iTunes 1.0 Type Library. You can then double-click iTunesLib under references to see the object model iTunes exposes, but you are much better off with the documentation in the sdk. In C#, the same example looks like:

namespace iTunesSample


using System;

using iTunesLib;


class TrackLister



static void Main(string[] args)


iTunesApp app = new iTunesAppClass();

IITTrackCollection tracks = app.LibraryPlaylist.Tracks;

for(int i = 0; i < tracks.Count; i++)


IITTrack track = tracks[i+1];

Console.WriteLine("{0} by {1}", track.Name, track.Artist);





You can’t just use foreach because Tracks doesn’t expose an iEnumerator. It would be great if iTunes directly exposed managed interfaces. In that case, you could write something like:

iTunesApp app = new iTunesAppClass();

foreach(Track currTrack in app.LibraryPlayList.Tracks)


Console.WriteLine("{0} by {1}", track.Name, track.Artist);


In comparison, on the Mac OS, iTunes is scripted/automated using AppleScript. This is an English-like scripting language. Here’s what the similar code would look like:

tell application "iTunes"
    repeat with currTrack in every track in the current playlist
        log name of currTrack & " by " & artist of currTrack
   end repeat

Very simple! However, accessing the same data from C/C++/Objective C is much more difficult (maybe Cocoa has some nice wrappers I don’t know about…).


Now if only the play/stop/prev track/next track buttons on my Inspiron 8600 were programmable, I could hook them up to control iTunes. Has anyone hacked them?


Comments (30)

  1. That’s really cool, can’t seem to reference the iTunesLib DLL in VS.NET 2005 however, maybe there’s a special way to reference COM in 2005 I’m not aware of yet 🙂


  2. Dan Crevier says:

    Did you go to the COM tab in the Add reference dialog? You could also click browse to go to it manually. You might also want to make sure you are using the latest version of iTunes.

  3. RunOns says:

    Dan Crevier posted some sample C# code for controlling the Windows version of iTunes. Now I can finally write the app I’ve always wanted: a way to control my music playing anywhere in my apartment from any other room in…

  4. Jay T. says:

    I have an Inspiron 8500 (which is the same machine as the 8600 except for the processor – I have P4 Mobile 2.2GHz) and my play/stop/prev track/next track buttons work perfectly in iTunes, I didn’t even have to change any configurations. Just make sure the iTunes window has the focus (foreground) and they should work… However, It seems the volume buttons are linked differently and only allow controlling the volume in Windows.

  5. ajc says:

    Thanks Dan! I have posted on my blog showing how to not only use the iTunes COM object from C#, but also how to bind event handlers for iTunes events. You may find it useful. I use it to update the Current Listening links on my blog, by binding to the PlayTrack event.

  6. Matt says:

    I tried this using through an ASP.NET web app, and I was getting ‘COMException: Server Execution Error’ from this line, which is the first call. Any ideas?


    private void Page_Load(object sender, System.EventArgs e){

    iTunesLib.IiTunes iTunesApp = new iTunesAppClass();



  7. Gonzonia says:

    I relased TitleTunes 0.0.1 yesterday…small app that hides the iTunes main browser window and locks some basic controls to the top of the screen. Written in Vb.Net

  8. Greg says:

    Can anyone tell me how to set this thing up in 2003 for C++ I just cant get it to work

  9. Greg says:

    Can anyone tell me how to set this thing up in 2003 for C++ I just cant get it to work

  10. To hook your multimedia keys (doesn’t matter if it’s an Dell Inspiron or something else), just download the iTouch software from Logitech. This little helper supports the following mediaplayers:

    • Windows CD Player

    • Windows Media Player up to version 9.x

    • MUSICMATCH(tm) versions 4.x up to 8.1

    • RealOne Player

    • Creative PlayCenter versions 1.5, 2.x and 3.0

    • WinAmp 2.x and WinAmp 3

    • Sonic CinePlayer versions 1.0 and 1.5

    • PowerDVD 3.0, 4.0 and 5.0

    • Intervideo WinDVD 2.x, 4.0 and 5.1

    • Quick Time 6.0 and 6.4

    • iTunes 4.6

    iTouch also works if your mediaplayer application hasn’t got the focus and you don’t need a Logitech Keyboard for this to work. I myself have an Inspiron 8600 and it works like a charme!

  11. Dan Crevier says:

    Unfortunately, it doesn’t work with Napster 🙁 I’m pretty disappointed with the Napster application. I guess I should just stick to listening to my music in WMP.

  12. Can't Bind Event says:

    I can’t seem to bind the OnPlayerPlayingTrackChangedEvent. It doesn’t give me an error when I do this:

    iTunesLib.iTunesApp App = new iTunesLib.iTunesAppClass();

    App.OnPlayerPlayingTrackChangedEvent +=new iTunesLib._IiTunesEvents_OnPlayerPlayingTrackChangedEventEventHandler(App_OnPlayerPlayingTrackChangedEvent);

    But the event is never fired! Any ideas?

  13. daniel02216 says:

    The OnPlayerPlayingTrackChangedEvent is only fired for linked CD tracks and editing the name of the currently playing track. It has a misleading name, but you are looking for OnPlayerPlayEvent, which is called every time a new track is played, along with OnPlayerStopEvent.

  14. Declan says:


    I’m having a different problem now. I’m attempting to write a frontend for iTunes but anytime iTunes is not open or you open the Preferences dialog in iTunes it throughs an exception. I know about the OnCOMCallsDisabledEvent, but that only helps if iTunes is running before the program is fired up. Is there not a way to check if iTunes is ready to give/receive instruction?

  15. manicwave says:

    It is old news that Apple released the iTunesCOMSdk. A few things are emerging that leverage the SDK: iTunes Art…

  16. Anonymous says:

    Little Yellow Frog &raquo; iTunes COM SDK and .NET

  17. Automating/scripting iTunes on Windows OoooooO…(jaw dropped on the floor, eyes dazed in confusion and simultaneous delight)….

  18. Here’s some cool code I hacked together from the iTunes Windows COM SDK docs and some other sites – Dan…

  19. Jorriss.Net says:

    Finally the Orroz has come back to the blogsphere. Here is some good news, you can access iTunes from…

  20. During TechEd 2006, my buddy Jean-Luc David (JL),&amp;nbsp;let me borrow his iPod Shuffle.&amp;nbsp; Oh my gosh!!&amp;nbsp;…

Skip to main content