Programming Cron Scripts

I’ve been seeing people ask about writing AppleScripts that work with Chris Johnson’s wonderful Cron app. (Note, I have no idea where you can get a copy of it nowadays.)

Writing Cron-able Scripts

Here’s a reasonably simple template that you can use. You should probably be on a Macintosh when you’re reading this. There’s a somewhat more complex Generic Cron App in the Negative Space Collection.

--How many seconds should we stay open after getting an
property idletime : 10
property quittime : 30
property datestamp : 0
property CommandQueue : {}

on run
   set datestamp to current date
end run

--Surround “event argsargc” & “class argc” with Option-\ & Shift-Option-\
on «event argsargc» given «class argc»:x
   set the end of CommandQueue to the rest of x
end «event argsargc»

on idle
   if CommandQueue is not {} then
      DoSomething(item 1 of CommandQueue)
      set CommandQueue to the rest of CommandQueue
   else
      if (current date) > (datestamp+quittime) then
         quit
      end if
   end if
   return idletime
end idle

on DoSomething(TheCommands)
   --do something with the commands that Cron sent us
   repeat with TheCommand in TheCommands
      write TheCommand & return to file "Doing Something" starting at eof
   end repeat
end DoSomething

Save it as an Application, making sure that Stay Open and Never Show Startup Screen are clicked.

Calling Cron Apps

Besides writing Cron apps, you might also want to call some of the pre-written ones from your scripts. Here’s how you can do that:

tell application "Program Name"
   <<event argsargc>> given <<class argc>>:{"Program Name","param1","param2","etc."}
end tell

Other Useful Tricks

  • You can keep the cron apps you create separate from the bundled cron apps by creating a folder in the Cron folder and putting your apps (or aliases to them) in the folder. In crontab, you can refer to these as “:folder:appname”, rather than having to give the full path.