Monday, March 1, 2021

#1 2021-02-16 06:32:57 am

scrutinizer82
Member
Registered: 2015-08-04
Posts: 87

Why launch agents refuse to run compiled applescript directly?

I wrote a very simple launch agent. In the first version, I compiled AppleScript file (.scpt), set it to be executable, and put inside the agent's Program property. I made sure the file runs as expected both from within ScriptEditor and Terminal via osascript. As for the launch agent itself, my look-up showed its absence in the override database (which means success). However launchctl list | grep -i my_job revealed it being loaded but exiting with error 1 which usually points to impermissible errors. I looked closer at my agent's "Program" property and re-wrote it so that it would rely on "ProgramArguments" instead and included this script file as an argument to the command line utility osascript which, in turn, had to be given to the shell command itself (/bin/sh -c osascript my_script.scpt).
Only then it was complete success. Which begs the question: are launch agents unable to run AppleScript compiled code files as the arguments to their "Program" property without shell?

Last edited by scrutinizer82 (2021-02-16 06:36:11 am)


Scripting in Mac OS X 10.7.5, OS X 10.9.5, macOS 10.14.6

Offline

 

#2 2021-02-16 08:45:23 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11739
Website

Re: Why launch agents refuse to run compiled applescript directly?

The usual way is to specify /usr/bin/osascript as the value for Program and repeat osascript in the first item of the ProgramArguments array

   

Applescript:

<key>Program</key>
   <string>/usr/bin/osascript</string>
<key>ProgramArguments</key>
   <array>
       <string>osascript</string>
       <string>/Users/myself/path/to/script.scpt</string>
   </array>


regards

Stefan

Offline

 

#3 2021-02-16 09:30:40 am

scrutinizer82
Member
Registered: 2015-08-04
Posts: 87

Re: Why launch agents refuse to run compiled applescript directly?

Thank you. But my question was why it's not possible to specify the path to the AppleScript script file as a value of "Program"?


Scripting in Mac OS X 10.7.5, OS X 10.9.5, macOS 10.14.6

Offline

 

#4 2021-02-16 12:59:23 pm

robertfern
Member
Registered: 2011-11-29
Posts: 108

Re: Why launch agents refuse to run compiled applescript directly?

Because a compiled AppleScript is not a program!
It's a script which needs to executed by an AppleScript program, i.e. osascript
or you can save the script as an AppleScript application

Offline

 

#5 2021-02-16 05:18:29 pm

Fredrik71
Member
Registered: 2019-10-23
Posts: 630

Re: Why launch agents refuse to run compiled applescript directly?

scrutinizer82 wrote:

Thank you. But my question was why it's not possible to specify the path to the AppleScript script file as a value of "Program"?



Have you try to use osascript inside bash script to make your launchAgent ??


The purpose to study someone else art is not to add, its to make less more.

Offline

 

#6 2021-02-16 05:47:44 pm

robertfern
Member
Registered: 2011-11-29
Posts: 108

Re: Why launch agents refuse to run compiled applescript directly?

Here is a plist file I am currently using to run a script at login on my Mac
its path is ~/Library/LaunchAgents/com.rmf.startup.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>Disabled</key>
    <false/>
    <key>EnvironmentVariables</key>
    <dict>
        <key>PATH</key>
        <string>/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/sbin</string>
    </dict>
    <key>Label</key>
    <string>com.rmf.startup</string>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/bin/osascript</string>
        <string>/Users/robert/Scripts/StartupScript.scpt</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
</dict>
</plist>


As you can see it does have the path to script

Last edited by robertfern (2021-02-16 05:49:42 pm)

Offline

 

#7 2021-02-19 07:30:03 am

scrutinizer82
Member
Registered: 2015-08-04
Posts: 87

Re: Why launch agents refuse to run compiled applescript directly?

robertfern wrote:


   
<key>ProgramArguments</key>
    <array>
        <string>/usr/bin/osascript</string>
        <string>/Users/robert/Scripts/StartupScript.scpt</string>
    </array>

   
As you can see it does have the path to script



Yes. But it's not in the form of


<key>Program</key>
    <array>
        <string>/Users/robert/Scripts/StartupScript.scpt</string>
    </array>


which is what my question was about.


Scripting in Mac OS X 10.7.5, OS X 10.9.5, macOS 10.14.6

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)