Adventures | Notes

iTunes Pure Sunday Automation

by Derek—2005.05.15 @ 1512

Slowly--and perhaps a bit reluctantly--I've been dabbling with AppleScript, partly due to the release of Apple's Tiger operating system and its new automation tools. AppleScript hails at being very user friendly and having an "English-like language," but I've found the style to be a little cryptic. Despite my misgivings, I thought I'd give AppleScript a try by doing something farily simple: tell iTunes to automatically play a playlist. I went a little further (as I'm prone to do) by wanting the process automated.

Background

Growing up, my Mother would wake up early on Sunday mornings and play a tape or CD of Sunday-appropriate music--to set a somber, religious mood for the day. I loved that, and now that we have our entire music collection on the computer, I thought this would be something to automate (although, is being lazy one of the seven deadly sins?).

What I wanted to do was have my computer turn on every Sunday morning and automatically play some Sunday music. With the Mac, this automation works with a couple of key OS X features:

  • auto-login;
  • a repeating event in iCal;
  • an iTunes playlist;
  • and a simple AppleScript

The real hang-up for me was the auto-login, since I really don't want to have my computer log in as me without a password. I'm still looking for a way to get around this.


The Automation

An iTunes Playlist

The first thing to do is have some music you want to automatically play. Set up a custom playlist in iTunes and give it a unique name (I chose the name "Pure Sunday," a tribute to a tape Melissa made in College). This will be the playlist you will automate.

iTunes Playlist

A Simple AppleScript

The AppleScript here is very simple: tell iTunes to activate then play a playlist. The final script looks so simple you wouldn't believe that it took quite a search to find these "English-like" phrases to use. Since each application has its own AppleScript "dictionary," you have to learn new methods for each application you are trying to interact with. I guess in some ways this is akin to Perl modules, but with Perl, I'm used to having documentation that shows working examples...

Open the Script Editor, available in the Applications folder, and put the following in a new document:

    tell application "iTunes"
        activate
        try
            play playlist "Pure Sunday"
        end try
    end tell

Save this script where you want. I put mine in ~/Library/Scripts so it would be available in the scripts menu bar. You can add the scripts menu bar by opening the AppleScript Utility and checking the "Show Script Menu in menu bar."

Script Menu

If the save was successful, than the AppleScript compiled correctly and you are set to go. You can test your script by hitting the "Run" button in the Script Editor.

A Repeating Event In iCal

Now that your script is saved and set up, you can use iCal to automatically run the script on a schedule. You must have a calendar created where you can add a new event. I have my event on Sunday, repeating every week at 7:00 a.m. The real trick is to have iCal open an "alarm" which triggers the AppleScript. To do this, pick "Run script" from the alarm drop-down and then pick your iTunes script you just created.

I had the alarm open at "0 minutes before," to start when the event begins.

iCal New Event

Auto-Login

To allow the iCal alarm to run, the computer has to be running. If you leave your computer on 24/7, than you're fine. Since I shut down every night, I needed a way to have the computer turn on before 7:00 a.m. Going to /Applications/System Preferences, choose "Energy Saver" and click on the "Schedule..." button.

Energy Saver Schedule Button

This will open up a "sheet" allowing you to pick a schedule when your computer will startup and shutdown every day. I was just interested in Sunday, and picked a time just before my iCal event.

Auto Startup

Since I have multiple accounts in my machine, I needed to allow the computer to startup under my login. For right now, this was the easiest solution, but not the most ideal. I'm going to look for a way to launch everything without having to be logged in. Under the Accounts preference pane in the System Preferences, click the "Login Options" button to "Automatically log in as" someone.

Login Options

Final Thoughts

Being my first attempt at automating something on my Mac, I consider it a success. As I mentioned before, it would be great if I didn't have to automatically login as someone in order to play the music. I'm sure there is a way, or maybe its just not a good idea for security/CPU reasons.

One problem that did arise last week was something I didn't consider: muting. Before we went to bed on Saturday night, Melissa hit the "mute" button on the computer in order to quickly stop the music I was playing while she was on the phone. We didn't think much of it until the next morning when I wondered why my script hadn't started. It had, it's just that the computer was muted. I guess I could add some AppleScript to set the computer volume level, or make sure it isn't muted. I think I'll save that for another time.