How to run custom AppleScript in the background at all times?

Given an AppleScript file designed to poll for an application dialog box every 30 seconds, how can I easily setup my Mac OS so that this AppleScript file starts every time I login silently in the background?

Code for the script ( in case interested ) is listed below, and is designed to click "Later" on the AirMail Beta app:

repeat
    if (exists application "AirMail Beta") then
        tell application "System Events" to tell process "Airmail Beta"
            if exists (button "Later" of front window) then
                click (button "Later" of front window)
            end if
        end tell
    end if
    delay 30
end repeat

The above script works, but if anyone can think of a better way to auto-click a dialog without writing an AppleScript that polls every few seconds, feel free to offer such a solution in comments. Mainly though, I'd like to know the best way to manage AppleScripts designed to run continuously so they can be enabled / disabled and startup without being seen at login automatically.


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!


Step one:

When saving the script in the script editor, save as an application bundle, then add it to the startup items in login system preferences.

Step two "Background"

Use a app iBackground Scripts for Mac

Simply drag and drop an AppleScript that has been saved as an 'Application bundle' on to this droplet. Clicking on the 'Yes' button will set your script to run in the background and will not display in the Dock


I had this same issue. I was trying to click the "OK" button on kontakt in the background while i used my computer. This ended up working for me.

tell application "System Events"
    if exists of application process "Kontakt 5" then
        tell application "Kontakt 5.6.0" to activate
        delay 0.5
        repeat
            try
                click UI element "OK" of window 1 of application process "Kontakt 5"
            end try
        end repeat
    end if
end tell