6

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?

2 Answers 2

6

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.

3
  • thanks! I'll try this at home tonight and accept it when it works. Any insight as to WHY my method doesn't work? I'm curious.
    – Martin
    Commented Feb 28, 2015 at 12:17
  • I've been fiddling with this and got another error that I cannot get rid of. The applescript opens a file in another application (P-touch Editor v5.1, to be exact). It gets me this error: /path/to/script.applescript:1226:1292: execution error: LSOpenURLsWithRole() failed with error -10810 for the file /path/to/templatefile.lbx. (1). The execution bits have been set on both the .applescript and .lbx files. I suppose this has something to do with changing the applescript to text?
    – Martin
    Commented Feb 28, 2015 at 19:34
  • I just get execution error: System Events got an error: osascript is not allowed assistive access. (-1719) in my logfile and don't get prompted by System Preferences in 10.12.6
    – swrobel
    Commented Jul 28, 2017 at 1:32
2

While I have accepted @markhunte's question because it works in the general case, it did not work for me. That is why I post my own solution as an alternative.

Create a shellscript that launches the applescript

By having the launchagent run a shellscript that simply calls to the applescript (.scpt) the shellscript needs to be allowed access to assistive devices once and can be run by the launchagent to, in turn, call the applescript.

In my case, I ended up with the following LaunchAgent .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.mylabel.some-awesome-utility</string>
    <key>Program</key>
    <string>/Users/username/absolute/path/to/shellscript.sh</string>
    <key>StandardErrorPath</key>
    <string>/tmp/com.mylabel.some-awesome-utility.err</string>
    <key>StandardOutPath</key>
    <string>/tmp/com.mylabel.some-awesome-utility.out</string>
    <key>StartCalendarInterval</key>
    <array>
        <dict>
            <key>Hour</key>
            <integer>06</integer>
            <key>Minute</key>
            <integer>05</integer>
        </dict>
        <dict>
            <key>Hour</key>
            <integer>20</integer>
            <key>Minute</key>
            <integer>05</integer>
        </dict>
    </array>
</dict>
</plist>

This LaunchAgent will call the shellscript.sh twice daily as specified by the StartCalendarInterval key. Also note the StandardErrorPath and StandardOutPath keys that were useful while debugging.

The shellscript.sh is extremely simple:

#!/bin/sh
osascript /Users/mnmt/Documents/InCuffs/Orders/PrepareShipping/print-shipping-labels.scpt

As you can see, a simple shebang line to specify that it is indeed a shellscript followed by a single call to the applescript I had originally been calling directly from the LaunchAgent.

2
  • hi! I am trying to do something similar - although I want to run a python script that calls AppleScript to do some apple specific stuff. You mention - "the shellscript needs to be allowed access to assistive devices". How did you give assistive access to a shell script - that interface seems to only allow .app files to be added. Thanks!
    – jim70
    Commented Sep 16, 2018 at 1:28
  • @jim70 At least in macOS 10.14, nothing special needs to be done in the script. When it runs the first time, the OS will prompt the user to grant the script assistive access, and if accepted, it will take them to the appropriate panel in System Preference. The script will be automatically added to the list but not enabled. It is then up to the user to toggle the script's access manually.
    – wst
    Commented Jan 10, 2019 at 20:24

You must log in to answer this question.

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