views:

641

answers:

2

Hi Everyone:

I am attempting to run an Applescript inside a launchd plist, but for some reason it just isn't working. It could be that it is my computer, but I am thinking that there may be something else wrong with it. If someone could take a look and comment on this post, I would really appreciate it!

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"&gt;
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.pf.Testing</string>
<key>ProgramArguments</key>
<array>
<string>/usr/bin/osascript</string>
<string>-e</string>
<string>'tell application "Finder"' -e  'set didQuit to (path to home folder as string) &amp; ".myApp"' -e 'if (exists file didQuit) then' -e 'tell application "TestApp"' -e 'activate' -e 'end tell' -e 'end if' -e 'end tell'</string>
</array>
<key>StartInterval</key>
<integer>20</integer>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>

Thanks for any help!

LATEST PLIST:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"&gt;
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.pf.Testing</string>
<key>ProgramArguments</key>
<array>
<string>/usr/bin/osascript</string>
<string>-e</string>
<string>'tell application "Finder"'</string>
<string>-e</string>
<string>'set didQuit to (path to home folder as string) &amp; ".myApp"'</string>
<string>-e</string>
<string>'if (exists file didQuit) then'</string>
<string>-e</string>
<string>'tell application "TestApp"'</string>
<string>-e</string>
<string>'activate'</string>
<string>-e</string>
<string>'end tell'</string>
<string>-e</string>
<string>'end if'</string>
<string>-e</string>
<string>'end tell'</string>
</array>
<key>StandardErrorPath</key>
<string>/Users/pf/Desktop/Problem.log</string>
<key>StartInterval</key>
<integer>20</integer>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
+1  A: 

A likely problem is that launchd is not executing your AppleScript in the logged-in user's GUI context, and therefore AppleScript can't talk to the Finder.

Make sure the plist is installed as a LaunchAgent, and not a LaunchDaemon (the plist should be located in /Library/LauchAgents or ~/Library/LaunchAgents).

Try adding the following to the plist, to make the script run in the GUI context:

<key>LimitLoadToSessionType</key>
<string>Aqua</string>

Note that this will only work reliably on 10.5 and above; I was not able to get per-user LaunchAgents working correctly on 10.4.

Nick Dowell
Hi Nick:Thanks for your reply. Unfortunately, even after using the LimitLoadToSessionType code and double checking that there was a file in the path, it still doesn't run. It's really weird, especially because when I run the same code in Terminal, it works just fine... Any ideas?
PF1
Another idea: rather than passing the applescript commands as arguments, try putting them in a separate script file and passing its path to osascript.
Nick Dowell
+1  A: 

I think you need to break your final argument up into separate arguments - each argument (the -e and the individual lines of AppleScript) should be in a separate <string /> element. Either that, or as Nick says just pass in a .applescript file with the whole script in.

The problem is that your command gets interpreted as:

/usr/bin/osascript -e '\'tell application "Finder"\' -e  \'set didQuit to (path to home folder as string) & ".myApp"\' -e \'if (exists file didQuit) then\' -e \'tell application "TestApp"\' -e \'activate\' -e \'end tell\' -e \'end if\' -e \'end tell\''

which isn't what you meant.

Graham Lee
Hi Graham: I've done what you suggested, but now I get the following error when I use StandardErrorPath to log the problem: 0:1: syntax error: A unknown token can’t go here. (-2740)
PF1
if you run exactly the same script in Script Editor, does that tell you where your error is?
Graham Lee
No, the only difference in Script Editor is that I formatted it to be an Applescript document. This works fine. And that same code runs fine in the Terminal and doesn't generate an error. Does that code work for you?
PF1
That suggests then that we need to see the newly-edited plist file, as the problem must still lie there.
Graham Lee
Okay. I just updated the main post with the new code.
PF1
Just a guess, but could the problem be because I am including the /usr/bin/osascript in the ProgramArguments section versus the Program section?
PF1
@PF1: that shouldn't make a difference, the man page documents that either is acceptable. I have to admit that currently I'm stumped...
Graham Lee
@Graham Lee: Does the same code work for you if you have a .whatever file in your home directory? Maybe it's my computer...
PF1