Scripting iTunes: making an alarm clock

How to script iTunes have been discussed many times before. In Mac you use AppleScript, but in Windows you can instead use the Windows Scripting Host. That means you can choose either VBScript or JavaScript!

The trouble is how to exactly go about this. Even if you know these scripting languages, how do you know which commands to send to iTunes? As I always wanted a dead simple alarm clock function using iTunes and Windows Task Scheduler, I wrote a short script that I could run each morning:

  1. Open your notepad or coding environment of choice. I will choose to use VBScript instead of JavaScript, mostly because most Windows installations will association .vbs files with the Windows Scripting Host, while some might have re-associated .js-files with their preferred JavaScript-editor (ok, maybe that’s not the best reason, but it should be simple no matter the language you choose). Save your file as iTunes.vbs to get syntax highlighting, if your editor supports this.
  2. Enter the following script:

    Set objApp = CreateObject("iTunes.Application")
    Set colSources = objApp.Sources
    Set objSource = colSources.ItemByName("Library")
    Set colPlaylists = objSource.Playlists
    Set objPlaylist = colPlaylists.ItemByName("Party Shuffle")
    objPlaylist.PlayFirstTrack

    (above should be 6 rows only in your script)
    You can change “Party Shuffle” to preferred playlist, but then you would have to wake up to the same sequence of songs every morning. You could also add a:
    objApp.Shuffle = 1
    in the script to turn on shuffling, but the first song will still be the same. Party Shuffle will always pick up where you last played it.
  3. Try the script by double-clicking it from Windows Explorer or your desktop. It should start iTunes (if not already started), and start playing. If another program opens your script, you have not associated .vbs to Windows Scripting Host. Re-associate, or create a shortcut to: “wscript.exe <your path to you script in quotes>”.
  4. Now, you just have to schedule your alarm clock. Go to Windows > Accessories > System Tools > Scheduled Tasks. Start it. For simplicity, put a shortcut to it on your desktop or in your task bar. Go through the “Add Scheduled Task” wizard, and point it to your script (or wscript shortcut, if you had to do one). You can easily customize the times of the schedule, to happen only on week days, every day, every other day, etc. If you have different times every day, you can add separate schedules for the same task.
  5. After finishing the wizard, make sure you right-click on the task and go to Properties > Settings and click “Wake the computer to run this task”. The nice thing about Task Scheduler is that it can wake up your computer to for the alarm. Yes, that is right, you can hibernate or go into standby all night (do that, it will save us from some global warming) and let the computer wake up by itself. To make sure this works smoothly, also enter your Windows password under “Run As” in Properties > Task.
  6. Before trusting your alarm completely, set the task (or a new copy of the task) to run a few minutes later and hibernate the computer. Then wait until the time you set to make sure it actually manages to go all the way. Different people have different programs installed, and some might theoretically stop the Task Scheduler from running. For example: IBM-Lenovos Client Security Solution will replace the Windows authentication with its own, forcing you to manually unlock the computer before any program can run :p

That should be all. You now have your iTunes alarm clock! You can, of course, do lots of other stuff. For example – if you have a program that translates any hot key to a command, you can make scripts for going to next and previous track, play/pause and similar. To get some good ideas, check this Microsoft article out. There is also some info here and here. Finally, do you have an idea for a script that none of these articles seem to describe? Download the Apple SDK, open the zip file and then open the iTunesCOMInterface.h file. Don’t be scared by all the C code – just search through it (for example, you want to activate Repeat, search for “repeat”). The name of the methods in the C code are most of the time identical to, or similar to, the commands you need to write in your script. Good luck!