Apple - How to run custom AppleScript in the background at all times?
In my opinion, the best way to do it is by using Apple's own task scheduler: launchd
, because you don't need to install third-party software. First, the theory: to run a script from the command line, you just run:
osascript /PATH/TO/YOUR/script.scpt
Knowing this, all you have to do is create a plist
file in ~/Library/LaunchAgents/
with this:
<?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>air-mail-beta.job</string>
<key>ProgramArguments</key>
<array>
<string>/usr/bin/osascript</string>
<string>/PATH/TO/YOUR/SCRIPT</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
The name of the plist
file doesn't matter, but it should be in ~/Library/LaunchAgents/
. Also, make sure to change /PATH/TO/YOUR/SCRIPT
accordingly.
Finally, you just need to tell launchd
that you want this script to always run. To do that, you just do:
launchctl load -w ~/Library/LaunchAgents/NAME-OF-YOUR-PLIST.plist
and you're done! If it looks like the script didn't start, you can do this:
launchctl start air-mail-beta.job
where air-mail-beta.job
is the property under <key>label</key>
that we've set in the plist
file.
Finally, should you ever need to disable the script, don't forget to unload
it with:
launchctl unload -w ~/Library/LaunchAgents/NAME-OF-YOUR-PLIST.plist
I know this solution is more technical, but trust me, that's a better way to deal with your issue. If you have any question, just ask!