How to avoid the GUI password prompt when using `osascript`

applescriptpasswordsudo

I’d like to use the osascript shell command to run some AppleScript, but I’d rather avoid the GUI password prompt. Instead, I’d prefer to run the script as sudo from the command-line, so no GUI popups/prompts are needed.

The command is the following:

osascript -e 'tell application "System Events" to set UI elements enabled to true'

Running this command has the same effect as going to System PreferencesAccessibility and checking Enable access for assistive devices.

If it doesn’t prompt for your password, try the opposite:

osascript -e 'tell application "System Events" to set UI elements enabled to false'

For either one of these commands you’ll get a password prompt in the OS X GUI:

Screenshot

My question is: is there any way to avoid this password prompt?


I assumed invoking osascript with sudo privileges would do the trick, but alas — even in that case the GUI prompt is shown. 🙁

sudo osascript -e 'tell application "System Events" to set UI elements enabled to true'

Is there a way around this?

Best Answer

You can suppress the password interface by modifying your Mac's authorization rights.

Use the built-in security command line tool or authbuddy to change the system.preferences.accessibility right to allow:

sudo security authorizationdb write system.preferences.accessibility allow

Opening up the system.preferences.accessibility right will permit any user to change the accessibility settings without a password prompt.