AppleScript alarm clock - creating a time-based trigger

AppleScript alarm clock: Creating a time-based trigger.

I'm still working on my AppleScript iTunes alarm clock program, and looking for a way to set the alarm clock wake up time from the script itself. Earlier I was thinking about doing this using the Mac crontab facility, but somehow I don't think that's going to work for everyone. :)

AppleScript time trigger and the "repeat while" loop

It looks like I can solve the trigger portion of the problem programmatically using an AppleScript "repeat while" loop. That is, given a certain date/time as input, I can write AppleScript code as follows to trigger this time-based event:

-- assume this date came from the user
set targetTime to "December 22, 2006 6:49:00 PM"

repeat while (current date) is less than date targetTime
  delay 60
end repeat

display dialog targetTime

This AppleScript repeat while code segment tests the current date, and if the current date is not greater than my target date it goes to sleep for 60 seconds, and then tries again. Once the current date is greater than my target date the repeat while loop ends, and control goes on to the display dialog command.

Although I haven't shown it all out here yet, I now have all the pieces for my AppleScript alarm clock. I can prompt the user for the time they want the alarm to go off; I can use the text to speech software to say anything I want; I can fire up one or more songs through iTunes; and now using this approach I don't need a scheduling facility, I can trigger the alarm to go off with the code shown above.

Related AppleScript tutorials

Besides my AppleScript alarm clock example, I also have quite a few AppleScript tutorials out here these days, so please use the Search box at the top of this page to search for more AppleScript content. One thing I will say is that a lot of people write me about AppleScript speech synthesis capability, and if you're interested in getting started with it, I've written a very popular Mac OS X text to speech using AppleScript tutorial. It demonstrates how to use AppleScript to speak text, and also demonstrates the different "voices" that are available on Mac OS X systems.