Search code examples
ruby-on-railsmacositunesplaylist

Ruby on Rails: Get mediaplayer information (iTunes, TRAKTOR, Cog; current song + playlist)


I want to write a small web app which does this things:

  • deliver a web page to display the current song and some meta data
  • provide a web api which can be queried for the next song to allow the page to update without page load

No big deal, the only part I can't estimate is if it's possible to access the current song of iTunes, TRAKTOR and if possible Cog from a ruby on rails application.

It would be especially nice if I can access the playlist, too to display the following song, too.


Solution

  • I would use applescript via the command-line. I don't have Traktor Pro or Coq, but you can look at what objects are available via the AppleScript Dictionary viewer in OS X's Script Editor. For iTunes, the command would be:

    osascript -e 'tell application "iTunes" to get name of current track'
    osascript -e 'tell application "iTunes" to get name of every track of current playlist'
    

    You would execute the command from your rails app (which would need to be running as your user account) like this:

    def show
        @current_track = `osascript -e 'tell application "iTunes" to get name of current track'`
    end
    

    ...

    <h1>The current track is: <%= @current_track -%></h1>