Schedule launch of a file

This thread http://bbs.applescript.net/viewtopic.php?id=18612 explains how to schedule system sleep/wakeup by means of a shell script.
Is it also possible (w. plain Applescript/Unix) to schedule the launch of a file…?

I have been learning the ins and outs of launchd, and have posted an article covering some basics of the usage. There is also a link there to another good site about the utility.

I continue to play around with it, and have noted so far that it is nearly flawless when executed on an Intel Mac, but quirky on G4 based systems. I have no access to a G5 system, and I have found even more quirkiness in my old G3 iBook.

If you want to pursue using it for launch, I would happy to help you in any way I can.

hi eelco,

you also may want to take a look at the ‘at’ command (/usr/bin/at) for one time and of course the famous ‘cron’ facility.

i can’t tell exactly what you want to do, but let’s say you wanted to open a Word document every day at 7am. you could use the cron facility to trigger a command like this:

/usr/bin/open /Users/eelco/Documents/document.doc

of course, if it’s a process and not just a file launchd would probably be a better way.

cheers.

Thanks.

Indeed I’ve been looking into some of the stuff mentioned and read that “at” consumes processor and battery capacity which is the reason that Apple deactivated it by default.
That effect may be marginal compared to much of what Apple DOES throw at us, though.

Cronnix is a fine utility that use “a” (which?) Unix timing mechanism but afaik it is not scriptable.

Launchd, as far as I can determine, is not a timed service so must run continously evaluating a time condition.
Same disadvantage as “at”.

So, to script a timed event (your example was OK, waltr) in the way I intended is no that simple…

Again, it depends on your processor, and in some cases, which app(s) you want to launch. Launchd is written to do timed events on a daily basis, or at whatever interval you choose. (Currently, it cannot do M-F, for instance.) For example, if I set up a launchd agent to create a file with TextEdit every day at 6:20 AM, it works perfectly on all machines. If I ask it to launch iTunes, however, it only works reliably on the Intel machine, not the G4.

The workaround does not really require continuous running, but rather launching the agent every x seconds to evaluate the time and run the desired script if the time falls into the proper window. This is challenging only if the time is critical. I currently have a single agent that launches every 6 hours so that one script runs every Monday morning, and another runs every other Tuesday. The load on my CPU is nothing, and I don’t really care what times either script runs. I have another agent on my old G3 iBook that launches every 4 minutes, because I want the script to run M-F within a more narrow time frame. Again, the CPU load is nothing.

It is my opinion that launchd is the wave of the future, and I believe that is why it is flawless on Intel chips; the boys writing the utility most likely knew at the time which way Apple was heading.

Good luck

hi eelco,

Cronnix is just a GUI wrapper for the ‘cron’ facility. you could use it to set a timed event as above. you shouldn’t have to script it unless you are distributing your AppleScript.

if you are thinking about distributing, or creating an installer, i’d definitely go with ‘cron’. you could edit the file ‘/etc/crontab’ to get a root process running. here is a script that will add a handy reminder for you at 5:30p everyday (it has an uninstaller built in):


set myDecision to button returned of (display dialog "Add or Delete cronjob?" buttons {"Add", "Delete"} default button "Add")

if myDecision is "Add" then
	set theDate to (do shell script "/bin/date \"+ %m-%d-%y\"")
	do shell script "/bin/echo '# Added '" & theDate & "' for myImportantCronJob' >> /etc/crontab" with administrator privileges
	do shell script "/bin/echo '30	17	*	*	*	root	/usr/bin/say \"Time to go home\"' >> /etc/crontab" with administrator privileges
	do shell script "/bin/echo '# The comments are important for the myDeleteCronJob' >> /etc/crontab" with administrator privileges
else
	do shell script "/bin/cp /etc/crontab /etc/crontab.sed" with administrator privileges
	do shell script "/usr/bin/sed '/myImportantCronJob/,/myDeleteCronJob/d' /etc/crontab.sed > /etc/crontab" with administrator privileges
	do shell script "/bin/rm -rf /etc/crontab.sed" with administrator privileges
end if

NOTES: 1) do a ‘vi /etc/crontab’ to check the results. if you leave vi open while you run the script, you’ll have to close it and reopen to see the changes. 2) if you ‘add’ multiple times, you only need to ‘delete’ once.

that should get you going without needing Cronnix. you may want to read ‘man cron’ and google around for a ‘how to’.

CAVEAT SCRIPTER: before running the AppleScript, you may want to open the Terminal and do a ‘sudo cp /etc/crontab /etc/crontab.bk’ just to be safe.

cheers.

EDITED: changed the time–my script would have gone off at 5:30a!!

Just seconding the use of CronniX…it’s a nice utility for the UNIX impaired (I count myself among them…learning, but slowly) to access cron, which is very handy. Mind you, I’ve never used “launchd” so I don’t know the difference.

CronniX home page

But I have used cron via CronniX for a couple years now. Set once and forget, it’s been bulletproof. I’d stick with the User crontab and stay away from the System crontab…I put some scripts in the System one once, and boy what a mess…my Mac didn’t like that at all.:o