Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Enabling GUI scripting via AppleScript

So I have a launch-daemon that needs to utilize GUI scripting. I need to be able to enable it through AppleScript (or really any scriptable, automated means). I am trying to do this:


tell application "System Events" to if not UI elements enabled then

set UI elements enabled to true

return UI elements enabled

end if


but it doesn't seem to do anything. The return query is just to check that it worked, but it never does. I have tried saving the script and running it as administrator (sudo osascript myScript.scpt) but it still doesn't work. I never get prompted for admin credentials. This occurs when logged in as administrator or non-admin user. Any ideas?

Posted on Nov 12, 2012 5:25 PM

Reply
19 replies

Nov 12, 2012 7:20 PM in response to twtwtw

I don't think it's legal to use a multi-line if statement out of a tell application to... statement. the format tell application "System Events" to is intended for single line entries.

The single line entry is the if statement - it looks a little weird, but is perfectly legal.


The posted example also works on my machine, so maybe there is something else involved.

Nov 12, 2012 7:37 PM in response to twtwtw

10.7.4. Doesn't work on 10.7.5 either. I have 800 machines to test on, so far it doesn't work on any of the 10.7.4 or 10.7.5 systems I've tested.


What I'm trying to figure out is how the system actually keeps track of this setting. I know that access for assistive devices is managed by the presence of the file /private/var/db/.AccessibilityAPIEnabled, but this has two key disadvantages:


1) Creation of the file does not immediately enable access for assistive devices or GUI scripting

2) Easily disabled by the user (I have tried setting both user and system immutable flags to no avail; I have a ticket with enterprise support open about this).


I already have a daemon running on these machines that could, in theory, check to see if GUI scripting is enabled and enable it if not, but right now I can't even get it to turn on without manually enabling access for assistive devices in system preferences.

Nov 12, 2012 8:52 PM in response to Nextyoyoma

Here is the handler that UI Browser uses:


on enabledGUIScripting(switch)

-- Call this handler and pass 'true' in the switch parameter to enable GUI Scripting before your script executes any GUI Scripting commands, or pass 'false' to disable GUI Scripting. You need not test the 'UI elements enabled' setting before calling this handler, because authorization is required only if 'UI elements enabled' will be changed. Returns the final setting of 'UI elements enabled', even if unchanged.

tell application "System Events"

activate -- brings System Events authentication dialog to front

set UI elements enabled to switch

return UI elements enabled

end tell

end enabledGUIScripting

Nov 13, 2012 8:03 AM in response to Nextyoyoma

Is this a cloned system, or a standard install? sometimes cloned systems have odd structures or permissions.


Aside from that (some radically different system setup), the only thing I can think of is a name conflict, but since you're running this plain from Script Editor that limits name conflict possibilities to third-party osaxen. but no one really uses osaxen anymore. look in /Library/ScriptingAdditions/ and ~/Library/ScriptingAdditions/ on the off chance there's some unknown addition.


Also, try this diagnostic: create a fresh new user account and run the script there (you can delete the account when done). If the script works in a new user account then we know it's something specific to your user account. It's someplace to start...

Mar 30, 2015 8:25 AM in response to twtwtw

twtwtw wrote:


This is incredibly non-obvious, but what you want is this:


tell application "System Events"

set UI elements enabled to true

end tell


that turns on assistance access, which is what allows GUI scripting. You'll generally need administrator permissions to do it.

I just tried this in Yosemite but just receive an error message "System Events got an error: Can’t set UI elements enabled of application to true.". Is this supposed to work in newer versions of OS X?

Enabling GUI scripting via AppleScript

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.