or learn more

Controlling iTunes with Scala

Sep 22, 2010

A while back I came across an interesting snippet of Clojure code1 controlling iTunes. I played around with it and eventually morphed it into the code posted on my @learnclojure account. This morning I came across a similarly themed post about Scala’s interaction with Applescript and it inspired me to create the following:

[sourcecode lang=”scala” gist=”591683″]object iTunes { val mgr = new ScriptEngineManager val engine = mgr.getEngineByName(“AppleScript”)

def apply(cmd:String) = { this.engine.eval(“tell application \”iTunes\” to ” + cmd) this }

def ?() = this(“pause”)

def !() = this(“play”)

def ->|() = this(“next track”)

def |<-() = this(“previous track”) }

iTunes ? iTunes ! iTunes ->| iTunes |<- [/sourcecode]

They can also be composed as iTunes.->|().?. Nothing ground-breaking of course, but the next step is to create a complete iTunes DSL with command compositions allowing things like fast-forward, rewind, jumping to time segments, moving around by time increments, etc. Hopefully someone else has/will done/do this for me.


  1. I have looked deeply, but cannot find the original author. If you know then please relay. 

One Comment, Comment or Ping

  1. ;; But the original lisp example does not play the same cuteness card!

    (defn ? “Pause” [itunes] …) (defn ! “Play” [itunes] …) (defn ->| “Next track” [itunes] …) (defn |<- “Prev track” [itunes] …)

    (-> itunes ? ! ->|)

Reply to “Controlling iTunes with Scala”