Run a part of script every 'n'th time the script runs.

I have a script widget that runs every five seconds and does a couple of simple jobs. Recently, I wanted to add another task to it, but this one is quite expensive and only needs to run every minute or so.

I could add another widget for the job and set its repeat rate to 60 seconds, but that's an ungainly solution, since I don't want an extra button on the bar for no real reason.

Anyway, I found a solution that runs the big task every 12th time the script runs, so from a single widget, I can have jobs running at both 5 second and one-minute intervals.

The trick relies on the persistence of properties in AppleScript:

property run_counter : 0

set run_job_every to 5

set retval to run_counter as text

if run_counter is 0 then
	set retval to retval & "*"
end if

set run_counter to (run_counter + 1) mod run_job_every

return retval

Create a script widget and give it the above code. The text on the widget will cycle around "0*", "1", "2", "3" and "4". The numbers represent the frequent job and the "*" represents the less frequent job running.