MacOS – Assistive Access when script is launched by agent

applescriptlaunchdmacosui

Problem

Applescript run by osascript is not allowed Assistive Access when launched by agent (~/Library/LaunchAgents). Although it works when run from Applescript editor or commandline directly (using osascript).

Situation

The applescript performs several GUI tasks for batch printing and is run twice daily. Therefore it needs permission to access Assistive Devices and is launched by an agent.

The script runs as expected when launched from:

  • Applescript Editor
  • osascript commandline (non-sudo)

When launched by Agent it returns:

/location/of/my.scpt: execution error: System Events got an error: osascript does not have access to assistive devices. (-25211)

(Message translated from Dutch by me.)

This led me to believe the issue is with osascript, but like I stated above, it works fine when entered directly from command-line using:
osascript /location/of/my.scpt.

Also, based on these two posts I edited the /Library/Application\ Support/com.apple.TCC/TCC.db SQLite database and when I check the 'access' table it shows:

$ sudo sqlite3 /Library/Application\ Support/com.apple.TCC/TCC.db "SELECT * FROM access"
Password:
kTCCServiceAccessibility|com.logitech.gaming|0|1|0|??

kTCCServiceAccessibility|com.valvesoftware.steam|0|1|0|??

kTCCServiceAccessibility|com.blizzard.starcraft2|0|0|1|
kTCCServiceAccessibility|com.apple.AccessibilityInspector|0|1|0|??

kTCCServiceAccessibility|com.apple.ScriptEditor2|0|1|0|??

kTCCServiceAccessibility|com.apple.Terminal|0|1|0|??

kTCCServiceAccessibility|/usr/bin/osascript|1|1|1|
kTCCServiceAccessibility|com.my.label|0|1|1|
kTCCServiceAccessibility|/osascript|1|0|1|

As you can see osascript is allowed access and I also added my agent's .plist for good measure.

Possible Cause?

The only thing I can think of is that, for some reason, the script is run by a 'different user' when launched by an Agent and therefore has different assistive devices allowed? Seems unlikely, however, as the TCC.db does not seem to be user-specific and the agent is located in ~/Library/LaunchAgents.

Help, anyone?

Best Answer

A simple fix is to save the script as a text file instead.

At the top of the script add the osascript shebang

#!/usr/bin/osascript

example:

#!/usr/bin/osascript
say "hello"
tell application "Safari" to activate
tell application "System Events"

    delay 2
    keystroke "p" using command down
    tell application process "Safari"

        tell application "System Events"
            tell process "Safari"
                click menu button "PDF" of sheet 1 of window 1
                delay 1

                click menu item "Save PDF to Web Receipts Folder" of menu of menu button "PDF" of sheet 1 of window 1
            end tell
        end tell
    end tell

end tell

The in the save dialog choose Text as the file format. The file will be saved as plain text but with the .applescript extension.

In terminal make the save script text file executable.

I used:

chmod +x /Users/UserName/Scripts/newTest1.applescript

In the command arguments of the LaunchAgent just add the path to the file.

Do not add the osascript command to the arguments. You do not need it.

The saved script text file will act like an executable shell script.

When you first load or run the LaunchAgents you will get a prompt to set the Assistive Access in System Preferences. If you already have System preferences open you will not but the Script text file will be added to the list.

You now just have to check its check box to allow it.

I would reload the LaunchAgent so it picks up straight away.

I have double checked this with the script above and all works as expected.