3

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.

7

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!

  • Thanks for this solution. Can the name of the .job be anything in the .plist file? I've tried making a custom .plist and it seems to successfully load the .plist with launchhd, but the script itself doesn't seem to start. I get no feedback in Terminal when doing launchctl afterwards. I know that the script works because if I simply do osascript /PATH/to/script.scpt it does what it's supposed to. – Winterflags Mar 8 '16 at 13:45
  • Experiencing exact same problem as @Winterflags – Sandwich Oct 18 '17 at 0:13
1

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 am not sure how iBackground works but depending on what setting it is changing in the saved script's info.plist the app may run in full background meaning you cannot easily quit it because it is now acts like a process and has no Gui displayed – markhunte Jun 24 '15 at 6:43
  • You can reverse it by clicking the 'No' button and it will no longer be a background script and will display in the Dock. If you don't see the change immediately, move the AppleScript to another location (Command+drag). – Ruskes Jun 24 '15 at 7:44
  • Are you confirming if you want to quit it you cannot do it through GUI if you use this app to make the change to a background app. – markhunte Jun 24 '15 at 7:48
1

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
0

You could add a stop routine your your background script. You then write another script to invoke your background routine's stopper... Some assembly required.

how to invoke the stringSet on statement of another script:

on run
    set myApp to "/Applications/applescriptFiles/demo -- the best/b.app"
    -- some hocus pocus is nesessary to avoid applescript from change path.
    -- set myApp to POSIX file myApp

    tell application myApp

        launch
        stringSet("stringie set") -- this function is in the "b" app.
    end tell
end run


(* 

save this as an application bundle 

invoked by a.app.  

twtwtw write in:
https://discussions.apple.com/thread/4917321?start=15&tstart=0

hocus-pocus: 

Give your script application a unique bundle id string (CFBundleIdentifier) and a unique bundle name (CFBundleName) too, to make life easier.  You set those in the info.plist file.  Launch the script app once to register it with launch services, and system will be able to find it thereafter.

Example:
      <key>CFBundleIdentifier</key>
      <string>com.rccharles.b</string>
    <key>CFBundleInfoDictionaryVersion</key>
    <string>6.0</string>
    <key>CFBundleName</key>
    <string>b</string>

    I added the CFBundleIdentifier info above the CFBundleInfoDictionaryVersion line. I left the CFBundleInfoDictionaryVersion and CFBundleName as is.

    *)

property myVar : "initial value"

on run
    display dialog myVar
end run

on stringSet(inputVar)
    set myVar to inputVar
    display dialog "variable myVar is " & myVar
end stringSet

You must log in to answer this question.

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