27

Script:

#!/usr/bin/osascript
tell application "System Preferences"
    activate
    set current pane to pane "com.apple.preferences.users"
    delay 2
    tell application "System Events"
        click radio button "Login Items" of tab group 1 of window "Users & Groups" of application process "System Preferences"
    end tell
end tell

How do I provide assistive access to this script? When running this through 'Script Editor', all it it needs is 'Script Editor' to be enabled under 'Accessibility' in 'Security & Privacy'.

But I need to run this (and some other similar script files) to be executed without user's intervention. I've tried saving the file in .sh, .app, .scpt, .applescript formats and run via terminal as sudo without any luck.

I've gone through other questions like this and other without any help.

EDIT: Thanks for the response guys. We've already tried these methods and they work without any doubt and that's where problem lies. We've way too many mac machines to go around and enable on them manually. We were hoping for some command that will add Terminal to the list with user's permission (kind of like UAC in Windows) or if possible silently.

4 Answers 4

21

If you run AppleScript code via osascript in Terminal, then you need to add Terminal to System Preferences > Security & Privacy > Privacy > Accessibility, to allow it assistive access.


I took your code, saved it to a file named testcode and made it executable using chmod. I then ran it in Terminal and received the following error:

./testcode:157:286: execution error: System Events got an error: osascript is not allowed assistive access. (-1728)

I then added Terminal to System Preferences > Security & Privacy > Privacy > Accessibility and ran it again. It then processed the script properly and returned:

radio button Login Items of tab group 1 of window Users & Groups of application process System Preferences

It of course opened the System Preferences to that target.

3
  • In my case, the caller was Alfred 3. It wasn't getting added to the list automatically so I just manually added it to Accessibility and then my script call worked. Commented Jun 14, 2018 at 18:01
  • 6
    I have the same error even-though I have terminal in Security & Privacy
    – Kevin
    Commented Jul 8, 2018 at 10:02
  • In my case, I was using tmux, so I had to add tmux to the accessibility list along with all the other suggestions here. Basically any process that is a "parent" of the terminal environment would need to be added.
    – Ben Davis
    Commented Nov 18, 2022 at 18:25
8

I got an error like 636:751: execution error: System Events got an error: osascript is not allowed assistive access. (-1719) even though I ran the osascript command from iTerm 2 which was on the list of applications in "System Preferences > Security & Privacy > Privacy > Accessibility".

I stopped getting the error after I also added the System Events application (in /System/Library/CoreServices/) to that list.

2
  • Surprising that this worked. Wish I knew why. I added all of the other more obvious applications first... to no avail.
    – Del Brown
    Commented Feb 7, 2019 at 1:12
  • You are my life-saver.
    – alswl
    Commented Feb 14, 2019 at 7:23
1

The key point in the linked answer is:

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.

Step 1 is to open the security tab and allow your script app or the individual scripts permission to automate the interface.

enter image description here

Step 2 - profit and avoid error -1728

2
  • 1
    Unfortunately this doesn't actually work for me with a LaunchAgent, my logs show execution error: System Events got an error: osascript is not allowed assistive access. (-1719) and I don't get prompted by System Preferences to allow assistive access. macOS 10.12.6
    – swrobel
    Commented Jul 28, 2017 at 1:34
  • Try adding also osascript to "Assistive Access" – at least internally, AppleScript treats "osascript" NOT as simply a command but as a process of its own. . . . Paste following code into ScriptEditor: . . . tell application "System Events" to do shell script "osascript -e 'tell application \"System Events\"\n display dialog (items of (name of (every process whose name contains \"sc\") as list) as string)\n end tell'" . . . and you'll see a list of (sadly not "delimetered") processes with "osascript" towards its end. Commented Nov 25, 2018 at 15:14
0

The script in the comment by @clemsam lang was helpful for me:

tell application "System Events" to do shell script "osascript -e 'tell application \"System Events\"\n display dialog (items of (name of (every process whose name contains \"sc\") as list) as string)\n end tell'"

Try adding also osascript to "Assistive Access" – at least internally, AppleScript treats "osascript" NOT as simply a command but as a process of its own. Paste following code into ScriptEditor: ...

tell application "System Events" to do shell script "osascript -e 'tell application \"System Events\"\n display dialog (items of (name of (every process whose name contains \"sc\") as list) as string)\n end tell'"

... and you'll see a list of (sadly not "delimetered") processes with "osascript" towards its end.

You must log in to answer this question.

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