launchd with Applescript

I’m trying to get launchd to run a script when the user logs in to their account. Been following other comments but can’t seem to get this to work. I’m doing this on OS 10.8.

This is my plist file created in text edit and saved as .plist (UTF-8)

<?xml version="1.0" encoding="UTF-8"?> Label com.myJob.agent LowPriorityIO Program /usr/bin/osascript ProgramArguments osascript /Users/nameAdmin/Library/Scripts/myJob.scpt StartCalendarInterval RunAtLoad

And here is a plan applescript I’m trying to run just to get it working. Named and put in the correct place.

tell application “Finder”
display dialog “launchd ran”
end tell

I have tried the launch load command and logged out and in but never get anything…

Browser: Firefox 32.0
Operating System: Mac OS X (10.8)

Hi FlameCoder,

I think this line is wrong:

The program should be the actual shell script. i.e. your plist is wrong I think.

Edited: in other words, you should use the osascript in your shell script.


I doubt that the plist works with an empty StartCalendarInterval parameter.
Try to remove the key

No, it’s correct

Hi Stefan,

I see that now. Good eyes! :slight_smile:

Removing the StartCalendarInterval key didn’t make it work. I get nothing when I log in. I want to be able to mount shares when a user logs in.

Just to make sure I’m doing this correctly I put the plist file named com.myJob.agent.plist into my users’ LaunchAgents folder. then run command in terminal launchctl load -w /path/To/Plist.

What else should I need to do? How can I see where it’s hanging at?

Program is an optional key while programArguments are not. They are all merged together so your actual command is:

/usr/bin/osascript osascript /Users/nameAdmin/Library/Scripts/myJob.scpt

which is wrong.
use something like this instead:

[code]<?xml version="1.0" encoding="UTF-8"?>

Label com.myJob.agent LowPriorityIO ProgramArguments /usr/bin/osascript /Users/nameAdmin/Library/Scripts/myJob.scpt StartCalendarInterval RunAtLoad [/code]

no, paradoxically the double osascript seems to be required.

I’m using a couple of agents calling osascript

According to the manual and my own plist which are working not:

You can leave program out, the first string in ProgramArguments will be used instead. At least it works on my machines (using Tiger, Leopard, Snow Leopard servers) when either one of them are left out

Changing the plist file to below seems to have worked for me… Now what should be done if I have a applescript saved as a stay open application? Removing the /usr/bin/osascript string doesn’t make the app run.

<?xml version="1.0" encoding="UTF-8"?> Label com.example.agent ProgramArguments /usr/bin/osascript /Users/userAdmin/Library/Scripts/myJob.scpt RunAtLoad Disabled

That is because when you’re in the world of BSD there is no such thing as an application, it’s just a directory. Inside an application there is an executable named applet. You need to start the application this way. So you can completely ignore osascript and launch the application immediately.

[code]<?xml version="1.0" encoding="UTF-8"?>

Label com.example.agent ProgramArguments /Users/userAdmin/Library/Scripts/ RunAtLoad Disabled [/code]

Thank you! It is working great now.

Funny, I’m playing around with launchd agents since launchd came out and it worked always only with osascript both in Program and ProgramArguments.

That is indeed funny :cool: