Run script through launch agent is failed I try to run the next script through LaunchAgent:

on run argv
    set current_path to POSIX path of ((path to me as text) &amp; "::")
    set common to load script (current_path &amp; "common.scpt")
    set screenSharingApp to "Screen Sharing"

    try
        if application screenSharingApp is running then do shell script "killall '" &amp; screenSharingApp &amp; "'"
    end try
    repeat until application screenSharingApp is not running
        delay 0.1
    end repeat

    tell application screenSharingApp
        launch
        activate
    end tell

end run


But always I get this error: execution error: Screen Sharing got an error: Application isn’t running. (-600)

If I run the same script through Script Editor or Terminal is running OK.

What can it be? Why does it not work?

My plist:

&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"&gt;
&lt;plist version="1.0"&gt;
    &lt;dict&gt;
        &lt;key&gt;Label&lt;/key&gt;
        &lt;string&gt;com.myapp&lt;/string&gt;
        &lt;key&gt;StandardErrorPath&lt;/key&gt;
        &lt;string&gt;/Users/user1/Desktop/stderr.log&lt;/string&gt;
        &lt;key&gt;StandardOutPath&lt;/key&gt;
        &lt;string&gt;/Users/user1/Desktop/stdout.log&lt;/string&gt;
        &lt;key&gt;ProgramArguments&lt;/key&gt;
        &lt;array&gt;
            &lt;string&gt;/usr/bin/osascript&lt;/string&gt;
            &lt;string&gt;/Users/user1/Desktop/1.scpt&lt;/string&gt;
        &lt;/array&gt;
        &lt;key&gt;RunAtLoad&lt;/key&gt;
        &lt;true/&gt;
        &lt;key&gt;StartInterval&lt;/key&gt;
        &lt;integer&gt;300&lt;/integer&gt;
    &lt;/dict&gt;
&lt;/plist&gt;
My recommendations:


Move anything to neutral ground:

This removes the launch agent from the launchd database:

sudo launchctl unload /Library/LaunchAgents/com.myapp.plist
sudo launchctl remove com.myapp


Move the .scpt file:

sudo mv ~/Desktop/common.scpt /usr/local/bin/


Remove unneeded std* files:

rm ~/Desktop/stdout.log
rm ~/Desktop/stderr.log


Modify /Library/LaunchAgents/com.myapp.plist with sudo nano /Library/LaunchAgents/com.myapp.plist accordingly:

&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"&gt;
&lt;plist version="1.0"&gt;
&lt;dict&gt;
    &lt;key&gt;Disabled&lt;/key&gt;
    &lt;false/&gt;
    &lt;key&gt;Label&lt;/key&gt;
    &lt;string&gt;com.myapp&lt;/string&gt;
    &lt;key&gt;ProgramArguments&lt;/key&gt;
    &lt;array&gt;
        &lt;string&gt;/usr/bin/osascript&lt;/string&gt;
        &lt;string&gt;/usr/local/bin/common.scpt&lt;/string&gt;
    &lt;/array&gt;
    &lt;key&gt;RunAtLoad&lt;/key&gt;
    &lt;true/&gt;
    &lt;key&gt;StandardErrorPath&lt;/key&gt;
    &lt;string&gt;/tmp/com.myapp.stderr&lt;/string&gt;
    &lt;key&gt;StandardOutPath&lt;/key&gt;
    &lt;string&gt;/tmp/com.myapp.stdout&lt;/string&gt;
    &lt;key&gt;StartInterval&lt;/key&gt;
    &lt;integer&gt;300&lt;/integer&gt;
&lt;/dict&gt;
&lt;/plist&gt;

Increase application kill check delay in the script:

 ...
 repeat until application screenSharingApp is not running
     delay 0.5
 end repeat
 ...

Load the plist:

sudo launchctl load /Library/LaunchAgents/com.myapp.plist





Check that no older "Screen Sharing" app is present on another volume and linked in the launch services database!