My AppleScript iTunes alarm clock program

AppleScript/iTunes FAQ: Can you demonstrate an AppleScript iTunes example (such as an AppleScript iTunes alarm clock)?

For a little while now I've been creating an AppleScript iTunes alarm clock application (an AppleScript alarm that would work with iTunes). The basic idea of the application was to do the following:

  1. Let me enter the time the alarm should go off.
  2. Try to wake me up several times using spoken words and different voices (using the Applescript say command).
  3. If all that fails, the AppleScript program should automatically start iTunes and play a song of my choosing. (In my case the song "Young at Heart" by Tony Bennett.)

My AppleScript iTunes alarm clock program

I'm not going to try to explain the program too much ... I hope you can get the idea of how it works by looking at the AppleScript source code. I'll just try to highlight a few key points here:

  1. The first thing the program does is get the desired wake up time from the user by prompting them with the AppleScript display dialog command.
  2. The program then stays in a loop until the current date is greater than or equal to the time the user entered.
  3. The program then starts speaking various phrases using different voices provided by the Mac OS/X operating system.
  4. The AppleScript program launches iTunes, finds a song whose name contain the string "Young at Heart" with an artist name containing the string "Bennett", then plays that song. (Note that I have several different versions of this song by different artists, so I need to specify more than just the name of the song.)

Source code for my AppleScript iTunes alarm clock program

I could make this AppleScript example a lot more convenient for other users -- and implement a ton of other ideas I have -- but at this point I thought I would just share the code. So, here's the initial source code for my AppleScript iTunes alarm clock application:

-- AppleScript "alarm clock" application (version 1)
-- written by Alvin J. Alexander,

-- prompt the user for the desired wake up time
set targetTime to the text returned of (display dialog "Enter the wake-up date/time:" default answer "" & (current date))

-- wait until we reach the target date/time
repeat while (current date) is less than date targetTime
  -- should be 60
  delay 2
end repeat

-- start talking
say "Good morning Al." using "Whisper"
delay 2

say "Al, it's time to rise and shine." using "Fred"
delay 2

say "The current time is " & getTimeInHoursAndMinutes() using "Vicki"
delay 2

say "Wake up sleepy head" using "Trinoids"
delay 2

say "You asked us to wake you up at this time." using "Victoria"
delay 2

say "It's time to wake up, it's time to wake up!" using "Cellos"

-- launch iTunes and play the desired song
tell application "iTunes"
  set ts to (every file track of playlist "Library" whose name contains "Young at Heart" and artist contains "Bennett")
  repeat with t in ts
    play t
  end repeat
end tell

-- get the time in the desired format
on getTimeInHoursAndMinutes()

  -- Get the "hour"
  set timeStr to time string of (current date)
  set Pos to offset of ":" in timeStr
  set theHour to characters 1 thru (Pos - 1) of timeStr as string
  set timeStr to characters (Pos + 1) through end of timeStr as string

  -- Get the "minute"
  set Pos to offset of ":" in timeStr
  set theMin to characters 1 thru (Pos - 1) of timeStr as string
  set timeStr to characters (Pos + 1) through end of timeStr as string

  --Get "AM or PM"
  set Pos to offset of " " in timeStr
  set theSfx to characters (Pos + 1) through end of timeStr as string

  return (theHour & ":" & theMin & " " & theSfx) as string
end getTimeInHoursAndMinutes

Running my AppleScript iTunes alarm clock program

You can run this script on your Mac OS/X computer by copying the code shown above, and pasting it into the ScriptEditor on your system. Of course you'll probably need to change the name of the song that you run in iTunes, but other than that it should work out of the box. After that just save it as a script on your system, and you're all set.

Feel free to modify this script however you'd like. Or if you have suggestions that you'd like to see implemented just let me know. FWIW my other current ideas are:

  1. Provide the capability to play several songs in sequence.
  2. Increase the volume as the song is playing.
  3. Improve the prompt for the wake up time.
  4. Provide a way for the user to cancel the script (besides the normal [Command][.] method of canceling AppleScript programs.)

On a related note, here's a link to my Mac OS X text to speech using AppleScript tutorial. It demonstrates how to make Mac OS X voices speak text, and also gives you an idea of what the different voices sound like.