2

I'm trying to run an AppleScript which ejects all drives (from the excellent answer by Monomeeth here) daily at 8 am using launchd. After some searching, I decided the best way to do this was by using launchd. However, I'm running into an error.

The .plist which I have in my user LaunchAgents directory is as follows:

<?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>local.SkeletonBow.EjectAllDrivesAt8</string> 
  <key>Program</key> 
  <string>/usr/bin/osascript</string> 
  <key>ProgramArguments</key> 
  <array> 
    <string>/usr/local/etc/Daily\ Scripts/Eject\ All.scpt</string>
  </array> 
  <key>StartCalendarInterval</key>
  <dict>
      <key>Hour</key>
      <integer>08</integer>
      <key>Minute</key>
      <integer>00</integer>
  </dict>
</dict> 
</plist> 

The AppleScript in question, Eject All.scpt, runs fine when I run it with osascript Eject\ All.scpt in my zsh shell.

According to this site, I should "load the job" into launchctl. I did this by running, according to their instructions, the following:

launchctl load ~/Library/LaunchAgents/local.SkeletonBow.EjectAllDrivesAt8.plist

And then tried to test it by running

launchctl start local.SkeletonBow.EjectAllDrivesAt8.plist

However, this particular action causes a cross to be shown in the Terminal, implying that something wrong has happened. I'm guessing that it's due to the syntax in my .plist file.

1

One shouldn't include the .plist extension when starting a launchtl process directly. Your command should read, instead, launchctl start local.SkeletonBow.EjectAllDrivesAt8

Also, the launchctl man pages indicate one shouldn't use leading zeroes when setting Calendar start time. The content should be <integer>8</integer> and <integer>0</integer> for the Hour and Minute keys.

Keep in mind, too, that when using CalendarInterval (as opposed to an elapsed-time Interval) to set a launch time for a process, that if the machine should unexpectedly be asleep at 8:00AM and therefore be unable to initiate the process, it will initiate the process upon awakening. That does not happen when the time setting is interval based.

0

Maybe this part is the problem:

  <key>Program</key>
  <string>/usr/bin/osascript</string>
  <key>ProgramArguments</key>
  <array>
    <string>/usr/local/etc/Daily\ Scripts/Eject\ All.scpt</string>
  </array>

Apple's documentation doesn't mention a Program item. When I tested it, it seemed to be ignored. Something like this worked for me:

  <key>ProgramArguments</key>
  <array>
    <string>/usr/bin/osascript</string>
    <string>/usr/local/etc/Daily\ Scripts/Eject\ All.scpt</string>
  </array>

It's a bit surprising that all parts have to go in the ProgramArguments array. The first one really is the program, isn't it, Apple? Eh bien, c'est la vie....

You can also shorten the code a bit:

  <key>ProgramArguments</key>
  <array>
    <string>osascript</string>
    <string>/usr/local/etc/Daily\ Scripts/Eject\ All.scpt</string>
  </array>

(I guess launchd / launchctl looks at the PATH to find osascript.)

2
  • <key>Program</key> is perfectly fine. <key>ProgramArguments</key> is primarily for the argument of the program been executed, but there is an exception too. Have a look at the manual page for launchd.plist. See in particular, Program <string> and ProgramArguments <array of strings>. You can read the manual page for command in Terminal by typing command and then right-click on it and select: Open man Page. – user3439894 Nov 7 '20 at 0:39
  • Also, there shouldn't be any backslash escaping spaces, just, e.g. <string>/usr/local/etc/Daily Scripts/Eject All.scpt</string> – user3439894 Nov 7 '20 at 0:46

You must log in to answer this question.

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