0

I am trying to schedule my AppleScript to run on 60 second intervals that runs two shell scripts and have appeared to setup my plist correctly as two files are outputted (StandardErrorPath and StandardOutPath) after 60 seconds elapse, but I don't see the results of my shell scripts appearing like they did when I ran the script in Script Editor. To me this appears to mean that the ProgramArguments section is not loading my AppleScript. Can anyone point me in the right direction? Should I add some type of logging to my AppleScript to log to the error file in my plist?

AppleScript file (Shell script runs correctly in Script Editor and Terminal):

do shell script "cd /Users/user/Desktop/Projects/node/webmasters-cli && /Users/user/.nvm/versions/node/v4.3.2/bin/node app.js DesktopUSA"
do shell script "cd /Users/user/Desktop/Projects/node/webmasters-cli && /Users/user/.nvm/versions/node/v4.3.2/bin/node app.js DesktopAll"

plist (Located in /Users/user/Library/LaunchAgents)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.user.webmasters.daily.pull</string>
    <key>KeepAlive</key>
    <false/>
    <key>RunAtLoad</key>
    <false/>
    <key>ProgramArguments</key>
    <array>
        <string>/Users/user/Desktop/Projects/node/webmasters-cli/daily-api-call.scpt</string>
    </array>
    <key>StartInterval</key>
    <integer>60</integer>
    <key>StandardErrorPath</key>
    <string>/tmp/webmastersDailyTest.err</string>
    <key>StandardOutPath</key>
    <string>/tmp/webmastersDailyTest.out</string>
</dict>
</plist>

Steps to load into launchctl:

1) Changed into directory

2) ran launchctl load com.user.webmasters.daily.pull.plist

3) Checked with launchctl list. plist appeared.

4) Checked /tmp and both StandardErrorPath and StandardOutPath files are created after 60 seconds. No values within files as expected.

4
  • Why not run the shell scripts directly from launchd?
    – mmmmmm
    Feb 16, 2017 at 16:32
  • @Mark to be honest I'm just learning about scheduling automation on mac and don't know much about launchd. Can you schedule shell scripts to run on a specific frequency? (Day of Week, Hour, etc.)
    – cphill
    Feb 16, 2017 at 16:55
  • yes and that is what launchd is doing it is running a program which coiuld be a shell script - for ease of writing the plists look at tools like LaunchControl
    – mmmmmm
    Feb 16, 2017 at 16:57
  • @Mark can you recommend in an answer your suggested change to my plist to run the shell script?
    – cphill
    Feb 16, 2017 at 18:32

1 Answer 1

1

Try adding /usr/bin/osascript as the first ProgramArguments. This will explicitly tell launchd to use the osascript to run your script.

Direct Approach

Do you need to use AppleScript for this launchd job? If not, you could avoid a level of abstraction by calling directly to node:

<key>WorkingDirectory</key>
<string>/Users/user/Desktop/Projects/node/webmasters-cli</string>
<key>ProgramArguments</key>
<array>
    <string>/Users/user/.nvm/versions/node/v4.3.2/bin/node</string>
    <string>app.js</string>
    <string>DesktopUSA</string>
</array>
3
  • Thanks for the answer. I'm not sure if I would need to as I'm just learning about plists and launchd. To sum up my shell script above, I'm changing directories to where my app.js file exists and then indicating where node exists and running the nodejs comman on my js file with an argument. Your answer above looks like it only includes the second half of the command. Would it possible to do both within the plist?
    – cphill
    Feb 16, 2017 at 16:58
  • Launchd jobs can only execute one process. You could modify the launchd job to call your shell script directly – then both commands could be placed in the script. If this is for work, consider something like Power Manager or another third party tool. Feb 16, 2017 at 17:01
  • 1
    Your code above worked. All I did was add in <string>/usr/bin/osascript</string> and my AppleScript ran like a charm. Thank you!
    – cphill
    Feb 16, 2017 at 18:45

You must log in to answer this question.

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