Controlling iTunes with Python ... Cross Platform

This is an old post!

This post is over 2 years old. Solutions referenced in this article may no longer be valid. Please consider this when utilizing any information referenced here.

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 (0)

Interested in why you can't leave comments on my blog? Read the article about why comments are uniquely terrible and need to die. If you are still interested in commenting on this article, feel free to reach out to me directly and/or share it on social media.

Contact Me
Share It
What I Use
Since it’s been a good six years since I did one of these, here’s what I am using in the year 2022 as far as tech and tech-adjacent things.
Read More
What I Use
Since it’s been awhile since I wrote a post about what I use in regards to software, hardware, etc. Perhaps it’s time that I did that again. So here’s a list of what I’m using in 2016:
Read More
Apple
So last month I had the pleasure of attending 360iDev in Denver, Colorado. Overall, this was a very good conference. As always, I learned so much from my fellow developers.
Read More