O'Reilly logo

AppleScript: The Missing Manual by Adam Goldstein

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 scripts—without even being asked.

Note

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 times—or, 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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required