O'Reilly Hacks
oreilly.comO'Reilly NetworkSafari BookshelfConferences Sign In/My Account | View Cart   
Book List Learning Lab PDFs O'Reilly Gear Newsletters Press Room Jobs  


 
Buy the book!
Mac OS X Panther Hacks
By Rael Dornfest, James Duncan Davidson
June 2004
More Info

HACK
#96
Use iCal to Schedule Tasks
iCal can keep not only your appointments in check, but also the appointments of your system
[Discuss (0) | Link to this hack]

The first tool old-school Unix hands reach to when they need to schedule something to run on a schedule is cron . However, you don't have to go to the command line to schedule tasks. You can use iCal, the personal calendaring application that comes with Mac OS X. iCal features multiple calendars that can be published to other computers and synchronized with .Mac. To schedule something in iCal, you create an event: an entry on the calendar that is at a specific time with a specific duration. Events can be one-time occurrences or they can repeat.

Each event can have an alarm that displays a notice on your computer screen, opens a file, or even launches an application at a certain time prior to the event so that it can be ready for you. In addition, alarms can go off even if iCal is not running. iCal uses the iCal Helper application (stored in /Applications/iCal.app/Contents/Resources) to keep track of events and fire them off on schedule whether or not iCal itself is running.

You can use iCal's alarms along with AppleScript to execute just about any kind of task you like. To do so requires only three simple steps:

  1. Create an AppleScript application that performs the functionality you want and save it somewhere.

  2. Create a one-time or repeating event in iCal.

  3. Set the alarm properties on that event to open your AppleScript application.

TIP

One logical place to store your scripts is in ~/Library/Scripts. Anything you store here shows up in the Script menu, if you've enabled it. To enable the Script menu, go to /Applications/AppleScript and double-click Install Script Menu. You can store almost anything here, including AppleScripts, shell scripts, and even application aliases. (If you're a former Mac OS 9 user, now you have a replacement for your unconfigurable Apple menu.)

For example, if you want to email a listing of all the files in your Home directory every week, you can create the following AppleScript with the Script Editor (/Applications/AppleScript) that scans the directory and emails the results:

set listing to (do shell script "/bin/ls -l $HOME")
tell application "Mail"
 set the newMessage to (make new outgoing message with properties ¬
 {subject:"Home dir ls output", content:listing})
 tell newMessage
 make new to recipient with properties {address:"you@somewhere.com"}
 end tell
 send newMessage
end tell

Once you have saved this script as an AppleScript application named ListHomeDir, you can set it up to run in response to an event. The key to running an application when an event is scheduled is to have the alarm set to "Open file" and then select the application as the file to open, as shown in .

Figure 1. Setting a repeating event in iCal to execute a task

Once the alarm has been set, as long as you are logged into the computer at the time the event is scheduled for, the AppleScript application executes. For many tasks, this sort of scheduling works out just fine. But if you want to run a task every hour, or when you aren't logged in to your Mac, you'll need to go to the command line and use the Unix scheduling tools.


O'Reilly Home | Privacy Policy

© 2007 O'Reilly Media, Inc.
Website: | Customer Service: | Book issues:

All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.