November 05, 2004
Driving iTunes from Python on Windows

Inspired by David Janes' pytunes, I've been having a play with driving iTunes with Python.

The first thing that I've discovered is that running EnsureDispatch over iTunes utterly screws up pytunes. The lesson here is to always use EnsureDispatch. If you don't, your code might break as soon as somebody else's code does. This applies to COM scripting with Python in general, I think.

It's all pretty easy. If you're familiar with COM scripting with Python, then you already know almost everything you'll need to know. Grab the iTunes COM for Windows SDK, and off you go! Fun, if utterly useless, is driving iTunes from a Python interpreter:

>>> import win32com.client
>>> iTunes = win32com.client.gencache.EnsureDispatch("iTunes.Application")
>>> iTunes.LibrarySource.Playlists.ItemByName('Party Shuffle').PlayFirstTrack()
>>> iTunes.Pause()
>>> iTunes.Play()
>>> iTunes.PreviousTrack()
>>> iTunes.NextTrack()
>>> iTunes.CurrentTrack.Name
u'Would Not Come'
>>> iTunes.SoundVolume
>>> iTunes.SoundVolume = 25
>>> iTunes.SoundVolume
>>> iTunes.SoundVolume = 50
>>> iTunes.Windows.Item(1).Minimized = True

Perhaps slightly more usefully, you can also edit your tracks' metadata. For example, here's a mickey-mouse script that I used to clean up some dodgy quote marks in my library -

Update: This one is actually marginally useful - find tracks that have no images, or more than one, with Anyone know where I can find cover art for "River Station" by Jimmy Giuffre & André Jaume? Google doesn't know it exists. Perhaps it doesn't...

Update: See also See also More Pointless iTunes fun with Python.

Posted to Python by Simon Brunning at November 05, 2004 02:05 PM

Great stuff. Its a lot easier than I assumed it would be.

Yay COM?

Posted by: Geoff Howland on November 7, 2004 11:05 PM

Yup - COM can be good, though it's only ever as good as the exposed object model. In the case of iTunes, it's very good.

Posted by: Simon Brunning on November 8, 2004 04:55 PM

I've tried duplicating your steps in the interpreter, but I don't get very far. When I try the EnsureDispatch call, I get a TypeError: "This COM object cannot automate the makepy process. Please run makepy manually ..."

I get the same type of errors running the pytunes script. I'm using iTunes 4.7 -- is this the version you were using as well?

Posted by: Brandon Corfman on November 23, 2004 03:10 PM

I was using iTunes 4.6 when I first ran EnsureDispatch, but I'm on 4.7 now. I'll try deleting the makepy generated stuff, and try again...

Posted by: Simon Brunning on November 23, 2004 03:40 PM

Nope, still works fine for me.

Did you try running makepy manually? What happened?

Have you had trouble with any other COM libraries?

Posted by: Simon Brunning on November 23, 2004 04:08 PM

Thanks for looking at it, Simon. I did a recent upgrade from 4.6 to 4.7, and apparently the iTunes COM objects didn't register properly the first time. I had to uninstall and reinstall 4.7 again for it to start working.

Now, if only I could capture events from iTunes properly ...

Posted by: Brandon Corfman on November 25, 2004 09:43 PM

Thanks! I am using iTunes with Airport Express and was looking for a way to control playback from a Linux box. Your post + Pyro gave me the answer.

Posted by: Dmitriy Agafonov on March 8, 2005 05:00 AM

Out of interest, does this script still work under Python 2.4.1 / iTunes 4.7?

For me, the EnsureDispatch that was working under 2.4 now fails with a SyntaxError on importing the generated file. There's a very similar bug already listed in the pywin32 tracker which fingers it as a Python 2.4.1 issue...

So, back down to Dispatch for me for now. What were the issues that arose from mixing Dispatch and EnsureDispatch?

Posted by: James Kew on April 6, 2005 08:47 PM

Another seemingly pointless app, is the one I just put together to allow remote control of iTunes via the web. But with an airport express I've been using this all the time.

Posted by: Tim Desjardins on December 22, 2005 04:58 PM
Post a comment

Email Address:



Remember info?