14 Replies Latest reply: Nov 13, 2012 8:03 AM by twtwtw
Nextyoyoma Level 1 Level 1 (15 points)

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?

  • 1. Re: Enabling GUI scripting via AppleScript
    twtwtw Level 5 Level 5 (4,690 points)

    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.

  • 2. Re: Enabling GUI scripting via AppleScript
    Nextyoyoma Level 1 Level 1 (15 points)

    Thanks for the reply, but that's exactly the same as what I originally posted, just without the of statement. Doesn't work. I get no prompt for admin credentials. Running through osascript as root doesn't work. The setting simply doesn't change.

  • 3. Re: Enabling GUI scripting via AppleScript
    twtwtw Level 5 Level 5 (4,690 points)

    whoops, you updated from the email, so I duplicated.  sorry.  but your syntax is way off in the example you gave. 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.

  • 4. Re: Enabling GUI scripting via AppleScript
    Nextyoyoma Level 1 Level 1 (15 points)

    Well in any case, your solution did nothing in my case. I know it should, but it doesn't.

  • 5. Re: Enabling GUI scripting via AppleScript
    twtwtw Level 5 Level 5 (4,690 points)

    it works on my machine perfectly.  post the exact code that you are currently testing.

  • 6. Re: Enabling GUI scripting via AppleScript
    Nextyoyoma Level 1 Level 1 (15 points)

    tell application "System Events"

              set UI elements enabled to true

    end tell

     

    tell application "System Events"

              return UI elements enabled

    end tell

     

     

    The second part is just to test what the current setting it.  It always stays false.

  • 7. Re: Enabling GUI scripting via AppleScript
    twtwtw Level 5 Level 5 (4,690 points)

    if there's no change in the state of UI Elements enabled (e.g. setting to true when it's already true), the command won't do anything. That's to be expected.

  • 8. Re: Enabling GUI scripting via AppleScript
    Nextyoyoma Level 1 Level 1 (15 points)

    But it's false.  I need it to be true.  I always get false.  Does this help to clarify?

     

    Screen Shot 2012-11-12 at 9.09.36 PM.png

  • 9. Re: Enabling GUI scripting via AppleScript
    twtwtw Level 5 Level 5 (4,690 points)

    what OS are you using.  it works as expected on my Lion system.

  • 10. Re: Enabling GUI scripting via AppleScript
    red_menace Level 6 Level 6 (14,615 points)

    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.

  • 11. Re: Enabling GUI scripting via AppleScript
    Nextyoyoma Level 1 Level 1 (15 points)

    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.

  • 12. Re: Enabling GUI scripting via AppleScript
    adayzdone Level 2 Level 2 (150 points)

    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

  • 13. Re: Enabling GUI scripting via AppleScript
    Nextyoyoma Level 1 Level 1 (15 points)

    The functional part of that script is identical to what I have already done. 

     

    Obviously something strange is going on with my systems.  Anybody have any idea what it might be?

  • 14. Re: Enabling GUI scripting via AppleScript
    twtwtw Level 5 Level 5 (4,690 points)

    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...