Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable access for assistive device programmatically

I want to enable Access for assistive devices in System Preferences programmatically. But Problem is that my application is not running as root user and i do not want my application to be as root user and also should not ask for any authentication in between.

I want to tap all keyboard events globally. I am using CGEventTapCreate() for the same.In the documentation of CGEventTapCreate() API it is mentioned that, Event taps receive key up and key down events if one of the following conditions is true:

  1. The current process is running as the root user.
  2. Access for assistive devices is enabled. In Mac OS X v10.4 & later, you can enable this feature using System Preferences, Universal Access panel, Keyboard view.

I tried manually by checking the Enable Access for assistive devices from System Preference and it gives me expected output.

So is there any way to do the same via program without asking for authentication and also application is not running as root user?

Thanks,

Dheeraj.

like image 234
Dheeraj Avatar asked Jun 01 '10 13:06

Dheeraj


3 Answers

You can run an Applescript (or translate the Applescript into ScriptingBridge or whatever your Objective-C layer over AppleEvents is)

Here is an Applescript that I use in one particular project that does something similar to what you need:

on isUIScriptingOn()
    tell application "System Events" to set isUIScriptingEnabled to UI elements enabled
    return isUIScriptingEnabled
end isUIScriptingOn

on turnUIScriptingOn(switch)
    tell application "System Events"
        activate
        set UI elements enabled to switch
    end tell
end turnUIScriptingOn

on run
    if not isUIScriptingOn() then
        display dialog "Enable Access for assistive devices (found in the Universal Access System Preference) must be on for this software to correctly work. This program will enable this setting for you"
        turnUIScriptingOn(true)
        display dialog "Access for assistive devices in now on"
    end if
end run
like image 191
RyanWilcox Avatar answered Nov 15 '22 18:11

RyanWilcox


Ok y'all the solution / some background info can be found at this address..

So, Apple came up with another solution in Tiger that solves these problems: the magic function AXMakeProcessTrusted. That will enable the API just for your application and needs to be called from a process running as root, so it is secure. It is also all automatic, so aside from asking the user for his/her password, there is nothing the user needs to do. The problem is that it seems no one uses it. Every third party application I have seen and even Automator just asks the user to manually check the box in System Preferences. It is more work to implement and has one huge undocumented bug (the application must be relaunched before it is actually trusted Update: reported as #5711990), but I really think people should be using it. So, I thought I would release the code to make it easy to implement it in your app. It includes a helper agent that you should be able to just drop into your project.

like image 36
Alex Gray Avatar answered Nov 15 '22 19:11

Alex Gray


It's usually considered rude to change a user's system setting without at least telling them, if not explicitly asking for permission. Most apps that need this setting just check to see if it's enable and if not, tell the user to turn it on.

like image 41
Jon Shier Avatar answered Nov 15 '22 19:11

Jon Shier