Section 13.5. Scheduling AppleScript Commands


13.5. Scheduling AppleScript Commands

If there's one annoyance in AppleScript, it's that scripts are finicky to run. Sure, you can double-click them, or trigger them with folder actions or Mail rules. But in all these situations, it's you who decides what should trigger the script.

Thankfully, if you're feeling lazy, you can set repeating schedules for your scripts. At whatever days and times you specify, Mac OS X will whir into action and run your scriptswithout even being asked.

Your scheduled scripts will only run if your computer is on and awake at the moment they're scheduled to run. If your Mac is off or asleep, AppleScript can't wake up your computer just to run your scripts.

The key to this feature is the cron program from Unix. cron lets you specify Unix programs you want to run at certain timesor, with the help of osascript, you can even specify AppleScripts you want to run at certain times.

Here, for example, is how you'd tell cron to run a Desktop-cleaning AppleScript every day at 10:15 PM:

  1. Enter your script in Script Editor.

    This is the script that will run regularly, so make sure it does something useful. The folder action from Section 11.3.2.2, for example, is the inspiration for this desktop-cleaning script:

    set maxItems to 5 tell application "Finder"     if not (exists folder "Old Desktop" of home) then         make new folder at home with properties {name:"Old Desktop"}     end if     set desktopFiles to every item of the desktop     if (count desktopFiles) > maxItems then         move items (maxItems + 1) through (count desktopFiles) of ¬             desktopFiles to folder "Old Desktop" of home     end if end tell

  2. Save your script somewhere handy.

    Your Library Scripts folder would be as good a place as any. Give the script a creative name, like Clean.scpt, and make sure the File Format is set to Script.

  3. Open Terminal, and type this:

    sudo pico /etc/crontab

    pico is a simple Unix text editor. /etc/crontab specifies the path of the file you want to open; this so-called crontab file is what dictates what day and time recurring events should occur. Finally, the sudo bit tells Unix you mean business: you need to run pico as the superuser (Table 13-1), because you're editing a system file.

  4. When prompted, type your administrator password.

    This gives you the authority to edit the crontab file, and actually runs pico.

    Make sure you replace user with your actual user name, as that is the person that Mac OS X will impersonate when it runs your script. Also, replace /Library/Scripts/Clean.scpt with the path to the script you want to run.

    Gem in the Rough
    Scheduling Scripts with iCal

    Although the Unix-based script scheduler from Section 13.5 is a geek's dream, it's not very friendly to novices. Thankfully, you don't have to use Unix to schedule your scripts; if you can accept a little less control, iCal serves as a perfectly acceptable alternative. Here's how to schedule a script in iCal:

    First, make sure your script is saved as an Application (Section 2.2.2). This'll prevent problems later on, because iCal can trigger only programs, not raw scripts.

    Choose File -N), and name your new calendar Scripts. Obviously, if you already have a Scripts calendar, there's no need to create a new one.

    Choose File -N), and give your event a name. This event will trigger your script, so make sure you place the event on the date and time you want the script to run.

    In the Alarm section of the Info pane, choose "Open file." This lets you pick a file that should run when the event is approaching. Select the AppleScript from step 1, and adjust the schedule using the lower pop-up menu.

    Adjust any other settings you want. For example, if you want iCal to trigger the script at regular intervals, choose an interval from the "repeat" pop-up menu.

    That's ityour script is now scheduled to run on the date and time you specified. Of course, you didn't have the precise minute-by-minute control you would have had with cronbut just think of all the Unix nerdiness you avoided.


  5. Use the arrow keys to navigate to the bottom of the pico window, and add this line:

    15 22 * * * user osascript "/Library/Scripts/Clean.scpt"

    This geeky list presents tons of information to cron. First, the number 15 on the left tells cron, "Run my program 15 minutes after the hour." The 22 indicates that your command should run 22 hours after midnightin other words, at 10:00 PM. Taken together, these two settings tell cron, "Run my program at 10:15 PM."

Next, there are three asterisks. To cron, an asterisk means, "Ignore this setting." For instance, the leftmost asterisk tells cron, "Run the program regardless of what day of the month it is." The middle asterisk implies, "Run the program regardless of what month it is." Finally, the right asterisk tells cron to run the program no matter what day of the week it is. Had you replaced these asterisks with specific numbers, cron would have limited your program to running on only certain days, or only in certain months.

If you're ever confused about which setting controls which periods of time, just look at the line near the top of the /etc/crontab file that begins #minute hour mday month wday. By looking down from these table headings, you can discern, for example, that the second column from the left controls which hours your program will run.

The rightmost portion of the line tells cron what Unix program you want to run. In this case, you specify osascript, along with the actual script file you want to run.

At this point, your Terminal window should look something like Figure 13-6.

Figure 13-6. Your crontab file after editing. If you wanted to schedule more than one script, you could add additional lines beyond this one.


  1. Press Control-X to exit pico.

    Confirm your changes by pressing y, and press Enter when pico asks you whether you want to resave the file as crontab.

  2. Wait patiently.

    Every night at 10:15, your script will spring into action and clean up your desktop. You don't even have to trigger it yourself!

If this chapter whetted your appetite for Unix, check out a dedicated book like Learning Unix for Mac OS X (O'Reilly). With several hundred pages of Unix information, you're bound to get more out of a book like that than this single chapter.



AppleScript. The Missing Manual
AppleScript: The Missing Manual
ISBN: 0596008503
EAN: 2147483647
Year: 2003
Pages: 150

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net