Updated 2006-09-11 22:00:56

Itunes is the music management software from Apple [1]. It can be used to manage music files, podcasts, and now videos. On windows, there is also a COM interface. The documentation for this is available via the itunes sdk [2], as well as samples in javascript.

<REMARK-JK:Oct2005 > On Mac OS X, you can control iTunes via AppleScript using the Tclapplescript extension, or you can even send raw apple events using the TclAE extension. You can pretty much control everything in this way, playing specific songs, managing playlists, and so on.

For an example of this, implementing an iTunes controller inside Alpha, see http://mat.uab.es/~kock/alpha-tcl/iTunesController.tcl.gz (Recall that Alpha is an extremely powerful editor, written mostly in Tcl -- it's really paradise for Tcl scripters!)

The Windows example below (the initial content of this page, by an anonymous author) implements a global hotkey. To do this in OSX, you should probably have a look at the hotkey example provided by DAS using ffidl. <END-OF-REMARK-BY-JK>

The examples below really only make sense on Windows.

The setup: with geoshell [3], hotkeys can be (fairly) easily created to do things like control the volume of the machine, mute, etc. These hotkeys are global hotkeys since they do not depend on any particular application to have focus. itunes for windows has some hotkeys to handle volume, pause, etc, but they are not global. I prefer to keep itunes minimised and peeking out from the system tray, so giving focus to itunes can be problematic at times. So, I wrote a series of tcl scripts to control itunes via COM.

(for those without geoshell, a short cut of these tcl scripts can be used to create a hotkey. twapi [4] can be used to create shortcuts as well. An example of this here [5].)

First, pause/play. When run, this script will pause and unpause itunes. This is very sensitive to context, though: whatever item the cursor is on is what gets played.
  package require tcom

  set iTunes [::tcom::ref createobject "iTunes.Application"]

  $iTunes PlayPause

  exit

Simple enough. Next, I create a hotkey for geoshell. When pressing control+window+p, the following command gets executed
  "c:\tcl\bin\wish.exe" "C:\Documents and Settings\wl\batch\itunes_pause_play.tcl"

and all seems good.

To expand on this, I next create a pair of tcl scripts to control the volume
  package require tcom

  set interval -2
  set iTunes [::tcom::ref createobject "iTunes.Application"]

  set vol [$iTunes SoundVolume]

  if {[incr vol $interval] <= 0} {
      set vol 0
  }

  $iTunes SoundVolume $vol

  exit

and
  package require tcom

  set interval 2
  set iTunes [::tcom::ref createobject "iTunes.Application"]

  set vol [$iTunes SoundVolume]

  if {[incr vol $interval] >= 100} {
      set vol 100
  }

  $iTunes SoundVolume $vol

  exit

I create hotkeys for control+window+down to call the first script, and control+window+up to call the second script. I think what I really should have done is used one script that accepts arguments, and pass the arguments to change the volume.

After playing around with the COM and itunes a bit more, I wanted an easier way testing these ideas out. So, I created a little server to control itunes. A person telnets to port 9630 and issues a series of commands to control itunes. The interface is a bit clunky, though. The point of this isn't really to create a networked version of itunes, although adding something like Skype [6] could be really fun to play with.... I had some weird crashes when multiple people were using this, so I'm enforcing a one user at a time rule.

Is this longer than appropriate for a wiki addition? Maybe, maybe not. Either way, I'm moving these things to a webserver: http://wliao.freeshell.org/tcl/itunes_server/itunes_server.txt

First, a sample session of this:
  > blah
  you got nothing. Try:
  info album | artist | bpm | coment | kind | name | time
  pause
  playlist kind | list | name | tracks [ n ]
  volume [ n ]
  quit | exit
  quit | exit

  > info name
  sleepy sunday show #20
  > info kind
  MPEG audio file
  > playlist tracks
  1 sleepy sunday show #20
  2 tartanpodcast #62
  3 tartanpodcast #63
  4 tp - finniston live!
  5 tp64 - tartanpodcast #64; the marathon
  6 tpsss21 - sleepy sunday show #21
  > playlist track 5
  Playing track 5 -- tp64 - tartanpodcast #64; the marathon
  > playlist list
  9 playlists
          1 Library
                  923 tracks
          2 Party Shuffle
                  21 tracks
          3 all_songs
                  916 tracks
          4 Recently Added
                  4 tracks
          5 Recently Played
                  213 tracks
          6 spods
                  6 tracks
          7 super_shuffle
                  150 tracks
          8 Podcasts
                  6 tracks
          9 stream
                  8 tracks

  > playlist list 2
  Playing first track of playlist 2 (Party Shuffle)
  > info album
  Trailer Park
  > info name
  Tangent
  > info artist
  Beth Orton
  >

The script is at the url above. I didn't do a lot of error checking, and some of the phrasing is very clunky, I think... me no likey.

One thing I know doesn't work is if nothing is playing, the track commands will usually (always?) fail. And of course, there's a ton of things I'd like to add to this ... over time, of course.

The original author of this page and the snippets is WL

Category Application - Category Music