Controlling iTunes with Python ... Cross Platform

By · Published · python, mac, apple

So it's been awhile since I've written. In that time, my girlfriend has moved in here with me in Huntsville and, as always, dealnews has kept me very busy. However, it has not prevented me from occasionally trying my hand at something new.

A week or so ago I decided that I was going to learn Python. However, as part of my nature, I simply can't "learn" a language without having a purpose. For instance, I have never been able to simply read a book on programming - I needed a reason. So I've been giving myself reasons to do little tasks here and there in Python. One of them came to me just today.

I have recently moved all of my development at dealnews from the PC to a Macbook. I've never been an OS-bigot - always use the right tool for the job, and the Mac - which in many ways is just Unix with pretty make-up - is the perfect platform. However, I still use many of the peripherals I purchased for my PC, including my Microsoft Natural Egronomic Keyboard that I adore. At home, I still use a PC (until I can afford a new Mac Pro), albeit with the same keyboard.

One of the things I really love about the keyboard is that it has various buttons that are just ... buttons. They can be mapped to do anything you want them to. There are five multi-function buttons at the top that can be mapped to run programs. So I'm sitting here thinking, "self" (because that is what I call myself), "why not write a little program to run on the click of that button and go to the next or previous track in iTunes, so that changing the music doesn't involve any more effort out of my busy programming day than hitting an additional keystroke". But, it must work both at home and at work, meaning that it must run in Windows and Mac.

Enter Python

I knew from previous experimenting in .NET that iTunes exposes a COM object on Windows. With that in mind, I quickly found this page that described almost exactly what I wanted to do in Windows. So that left the Macintosh. After an hour or so of digging on Apple's website, I found this page that described how to access the COM on the Mac - and wouldn't you know, the functions are slightly different.

After that, it was pretty easy:

import sys from optparse import OptionParser

platform = sys.platform
if platform == "win32":
    import win32com.client
    iTunes = win32com.client.gencache.EnsureDispatch("iTunes.Application")

if platform == "darwin":
    from Foundation import *
    from ScriptingBridge import *
    iTunes = SBApplication.applicationWithBundleIdentifier_("com.apple.iTunes")

def previousTrack():
    if platform == "win32":
        iTunes.PreviousTrack()
    if platform == "darwin":
        iTunes.previousTrack()

def nextTrack():
    if platform == "win32":
        iTunes.NextTrack()
    if platform == "darwin":
        iTunes.nextTrack()

def main():
    parser = OptionParser()
    parser.add_option("-n", "--next-track", action="store_true", dest="next")
    parser.add_option("-p", "--prev-track", action="store_true", dest="prev")
    (options, args) = parser.parse_args()
    if options.next == True:
        nextTrack()
    if options.prev == True:
        previousTrack()

if __name__ == "__main__":
    main()

So yeah. It's kind of code monkeyed together, but not bad for someone who's only been doing Python for a week in the evenings. Passing either a -n or -p to the script causes it to command iTunes to go forward or back. Of note, to work on Windows, it does need the COM components from the Python for Windows extensions.

I'm gonna expand this script some more in the future, but for now it does what I need.

( Comments )

Did something I wrote help you out?

That's great! I don't earn any money from this site - I run no ads, sell no products and participate in no affiliate programs. I do this solely because it's fun; I enjoy writing and sharing what I learn.

All the same, if you found this article helpful and want to show your appreciation, here's my Amazon.com wishlist.


Related Posts

petfeedd 0.2 released, with Docker support!

Rob's Raspberry Pi Powered Pet Feeders


comments powered by Disqus