launchd and applescript

I want to use launchd to run an applescript whenever I reboot my computer and hourly after that. So I wrote a launchd plist file that uses the command “osascript path/to/script” to run the applescript, placed it in my Launch Agents folder, then rebooted and launchd runs the script like I want. Easy!

I want to do this same thing on another computer. So I copied the applescript over, made the launchd file, then rebooted the computer. I found that launchd wouldn’t run the applescript. I don’t know why??? But after I manually run the applescript by hand one time, after that launchd would run the applescript properly. I just have to run the script once then everything works.

But this is a problem for me as I want to do this on other computers and I don’t want to have to manually run the script once on each new computer. Does anybody know why this is happening? My only thought is that the script has to be recompiled on each new computer, and running it once by hand takes care of that so that launchd can run the script. But I’m not sure that’s right and I don’t know how to fix it. Any ideas?

I have had similar issues with scripts and especially script apps.
It seems to be a permission thing, but unfortunately I have not come up with a solution that much different to yours.
I have to open them, but instead of running them, I Save As… and replace the file with it.

Hi regulus,

did you load the agent explicitly with

launchctl load -w /path/to/launchd.plist

Hi Stefan

I think regulus did,

No Stefan. I assumed that I didn’t need to do this because I rebooted the computer. I’m not too experienced in this but I thought that launchd automatically loaded everything in a user’s LaunchAgents folder during a reboot or logout/login operation. And the plist file is set to load at reboot and hourly.

I will try it though and report back. But can you explain exactly what this does and what the -w option does. Would running this command mean that I won’t have to reboot the computer to get a new launchd plist to work? I looked at the man page and I’m not quite certain I understand it exactly.

Thanks.

It could be, that the disabled key is set in the launchd.plist file.
the -w flag sets the disabled key to false while loading (or to true while unloading).

To test, if the launchd agent is loaded properly, type in terminal

launchctl list | grep myAgent

myAgent is a recognizable part of the plist file name

Thanks Stefan, I’m all working now. Issuing the launchctl command seems to have fixed my problem plus it means I don’t have to reboot the machine any more so that’s great. I wrote an “installer” applescript to copy my applescript, the launchd file, and a few other things to their appropriate place on a computer. Then it issues the launchctl command… so now everything runs great with no interruption to a user. Perfect.

The -w flag isn’t really needed in my case because I don’t even have a disabled key in my plist file. But I doubt the -w flag hurts anything and the command works as is so I left it in.

btw: Have you seen mine?

Ha. No I hadn’t seen that. That would have shown me the launchctl command so I wouldn’t have had any troubles… but that’s OK because I learned something doing it myself. I actually used Lingon to create the initial launchd plist file, then used the plist’s code in my installer script. So my installer is very similar to yours.