0

I try to run the next script through LaunchAgent:

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

    try
        if application screenSharingApp is running then do shell script "killall '" & screenSharingApp & "'"
    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:

<?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">
<plist version="1.0">
    <dict>
        <key>Label</key>
        <string>com.myapp</string>
        <key>StandardErrorPath</key>
        <string>/Users/user1/Desktop/stderr.log</string>
        <key>StandardOutPath</key>
        <string>/Users/user1/Desktop/stdout.log</string>
        <key>ProgramArguments</key>
        <array>
            <string>/usr/bin/osascript</string>
            <string>/Users/user1/Desktop/1.scpt</string>
        </array>
        <key>RunAtLoad</key>
        <true/>
        <key>StartInterval</key>
        <integer>300</integer>
    </dict>
</plist>
9
  • 1
    How exactly do you run it through a LaunchAgent? - Normally you would require a logged in user to be able to comunicate with a GUI app like that. – jksoegaard Apr 12 '20 at 14:51
  • @klanomath added – Rougher Apr 12 '20 at 14:56
  • @jksoegaard I run through Terminal: sudo launchctl load /Library/LaunchAgents/com.myapp.plist – Rougher Apr 12 '20 at 14:57
  • I assume it does work if you run osascript /Users/user1/Desktop/1.scpt directly from Terminal? – nohillside Apr 12 '20 at 15:05
  • @nohillside Yes, it does. It works in ScriptEditor also – Rougher Apr 12 '20 at 15:08
1

My recommendations:

  1. 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:

    <?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">
    <plist version="1.0">
    <dict>
        <key>Disabled</key>
        <false/>
        <key>Label</key>
        <string>com.myapp</string>
        <key>ProgramArguments</key>
        <array>
            <string>/usr/bin/osascript</string>
            <string>/usr/local/bin/common.scpt</string>
        </array>
        <key>RunAtLoad</key>
        <true/>
        <key>StandardErrorPath</key>
        <string>/tmp/com.myapp.stderr</string>
        <key>StandardOutPath</key>
        <string>/tmp/com.myapp.stdout</string>
        <key>StartInterval</key>
        <integer>300</integer>
    </dict>
    </plist>
    
  2. Increase application kill check delay in the script:

     ...
     repeat until application screenSharingApp is not running
         delay 0.5
     end repeat
     ...
    
  3. 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!

2
  • It doesn't help me. I marked that simple restart can help me, BUT only for first running. I also tried through crontab, and I get the same behaviour – Rougher Apr 13 '20 at 11:12
  • @Rougher Fixed some errors in my plist (had a wrong StandardErrorPath: /tmp/com.myapp.**stdout** in my previous version). I tested all my recommendations again in a 10.15 vm and I indeed get the same error after loading the plist (... Application isn’t running.) but after a reboot it works without any error. – klanomath Apr 13 '20 at 12:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .