Partager via


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

{

[STAThread]

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
end
tell

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

  • Anonymous
    May 09, 2004
    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 :-)

    Phil.
  • Anonymous
    May 09, 2004
    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.
  • Anonymous
    May 10, 2004
    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...
  • Anonymous
    May 10, 2004
    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.
  • Anonymous
    May 18, 2004
    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.
  • Anonymous
    May 19, 2004
    The comment has been removed
  • Anonymous
    May 20, 2004
    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
  • Anonymous
    May 23, 2004
    The comment has been removed
  • Anonymous
    May 23, 2004
    The comment has been removed
  • Anonymous
    June 18, 2004
    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!
  • Anonymous
    June 19, 2004
    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.
  • Anonymous
    June 21, 2004
    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?
  • Anonymous
    June 22, 2004
    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.
  • Anonymous
    June 22, 2004
    Thanks!
    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?
  • Anonymous
    July 13, 2004
    It is old news that Apple released the iTunesCOMSdk. A few things are emerging that leverage the SDK: iTunes Art...
  • Anonymous
    August 05, 2004
    The comment has been removed
  • Anonymous
    October 20, 2004
    Automating/scripting iTunes on Windows OoooooO...(jaw dropped on the floor, eyes dazed in confusion and simultaneous delight)....
  • Anonymous
    December 29, 2004
    Here's some cool code I hacked together from the iTunes Windows COM SDK docs and some other sites - Dan...
  • Anonymous
    April 23, 2006
    http://maximized.com/freeware/scriptsforitunes/documentation.htm http://www.codeproject.com/csharp/iTunesTray.asp...
  • Anonymous
    April 23, 2006
    Finally the Orroz has come back to the blogsphere. Here is some good news, you can access iTunes from...
  • Anonymous
    June 24, 2006
    During TechEd 2006, my buddy Jean-Luc David (JL),&amp;nbsp;let me borrow his iPod Shuffle.&amp;nbsp; Oh my gosh!!&amp;nbsp;...
  • Anonymous
    June 15, 2009
    PingBack from http://mydebtconsolidator.info/story.php?id=21671