Osascript is not allowed assistive access. (-1728)

applescriptscriptsudoterminal

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.

Best Answer

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.