Creating a time-based trigger for my AppleScript-based alarm clock

By Alvin J. Alexander, devdaily.com

I'm still working on my AppleScript-based alarm clock application, 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 cron facility, but somehow I don't think that's going to work for everyone. :)

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 sample 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 based 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.


devdaily logo