sudo: launchctl: command not found
I am trying to start my music app, both launched and playing, and a few other tasks, at a certain time. I wrote an applescript that runs after my vpn connects, so I know the apple script is good.
I opted to use a lunch agent for this purpose. When I try to load the plist:
sudo lauchctl load /Library/LaunchDaemons/com.start.work.plist
I get this error:
sudo: launchctl: command not found
Here is 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.start.work</string>
<key>ProgramArguments</key>
<array>
<string>osascript</string>
<string>tell application "Spotify" to activate</string>
</array>
<array>
<string>osascript</string>
<string>tell application "Spotify" play</string>
<string>end tell</string>
</array>
<key>StartCalendarInterval</key>
<dict>
<key>Weekday</key>
<integer>1</integer>
<key>Minute</key>
<integer>00</integer>
<key>Hour</key>
<integer>8</integer>
</dict>
<dict>
<key>Weekday</key>
<integer>2</integer>
<key>Minute</key>
<integer>00</integer>
<key>Hour</key>
<integer>8</integer>
</dict>
<dict>
<key>Weekday</key>
<integer>3</integer>
<key>Minute</key>
<integer>00</integer>
<key>Hour</key>
<integer>8</integer>
</dict>
<dict>
<key>Weekday</key>
<integer>4</integer>
<key>Minute</key>
<integer>00</integer>
<key>Hour</key>
<integer>8</integer>
</dict>
<dict>
<key>Weekday</key>
<integer>5</integer>
<key>Minute</key>
<integer>00</integer>
<key>Hour</key>
<integer>8</integer>
</dict>
</dict>
</plist>
What am I doing incorrectly? Forgive my lack of Mac knowledge! I'm a long time Mac user, but I havent used the terminal very much, and completely new to applescript.
I opted to use a lunch agent for this purpose.
Then the PLIST file belongs in ~/Library/LaunchAgents/
not /Library/LaunchDaemons/
and you should not be using sudo
.
Secondly your PLIST file is not valid as:
plutil -lint ~/Library/LaunchAgents/com.start.work.plist
Returns:
~/Library/LaunchAgents/com.start.work.plist: : Found non-key inside <dict> at line 16
You also have a typo in your command in Terminal, it's launchctl
not lauchctl
.
Additionally, you do not need to use the osascript
command twice. Simply use e.g.
/usr/bin/osascript -e 'tell application "Spotify" to activate' -e 'delay 3' -e 'tell application "Spotify" to play'
Which translates to:
Example XML Property List code snippet:
<array>
<string>/usr/bin/osascript</string>
<string>-e</string>
<string>tell application "Spotify" to activate</string>
<string>-e</string>
<string>delay 3</string>
<string>-e</string>
<string>tell application "Spotify" to play</string>
</array>
However there are other issues in your XML Property List code, so here is how I'd write it and this has been tested and returns OK
from:
plutil -lint ~/Library/LaunchAgents/com.start.work.plist
~/Library/LaunchAgents/com.start.work.plist: OK
Example XML Property List code:
<?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.start.work</string>
<key>ProgramArguments</key>
<array>
<string>/usr/bin/osascript</string>
<string>-e</string>
<string>tell application "Spotify" to activate</string>
<string>-e</string>
<string>delay 3</string>
<string>-e</string>
<string>tell application "Spotify" to play</string>
</array>
<key>RunAtLoad</key>
<false/>
<key>StartCalendarInterval</key>
<array>
<dict>
<key>Hour</key>
<integer>8</integer>
<key>Minute</key>
<integer>0</integer>
<key>Weekday</key>
<integer>1</integer>
</dict>
<dict>
<key>Hour</key>
<integer>8</integer>
<key>Minute</key>
<integer>0</integer>
<key>Weekday</key>
<integer>2</integer>
</dict>
<dict>
<key>Hour</key>
<integer>18</integer>
<key>Minute</key>
<integer>0</integer>
<key>Weekday</key>
<integer>3</integer>
</dict>
<dict>
<key>Hour</key>
<integer>18</integer>
<key>Minute</key>
<integer>0</integer>
<key>Weekday</key>
<integer>4</integer>
</dict>
<dict>
<key>Hour</key>
<integer>18</integer>
<key>Minute</key>
<integer>0</integer>
<key>Weekday</key>
<integer>5</integer>
</dict>
</array>
</dict>
</plist>
Notes:
The value of the delay
command may need to be adjusted. The The target application needs to be loaded with the GUI showing for before issuing the play
command.
If you are going to use Launch Agents and Launch Daemons, I highly recommend you read the manual pages for launchctl
, launchd.plist
and launchd
.
You can read the manual page for command
in Terminal by typing man command
, then press enter, or for easier reading, just type command
and then right-click on it and select: Open man Page