以编程方式启用辅助设备的访问权限

Dhe*_*raj 7 scripting cocoa accessibility objective-c accessibility-api

我想以编程方式在"系统偏好设置"中为辅助设备启用Access.但问题是我的应用程序没有以root用户身份运行,我不希望我的应用程序成为root用户,也不应该要求在它们之间进行任何身份验证.

我想全局点击所有键盘事件.我正在使用CGEventTapCreate().在CGEventTapCreate()API的文档中,提到了,如果满足下列条件之一,则事件抽头接收密钥和密钥事件:

  1. 当前进程以root用户身份运行.
  2. 启用辅助设备访问.在Mac OS X v10.4及更高版本中,您可以使用"系统偏好设置","通用访问"面板,"键盘"视图启用此功能.

我通过从系统首选项检查辅助设备的启用访问来手动尝试,它给了我预期的输出.

那么有什么方法可以通过程序执行相同的操作而不需要身份验证,并且应用程序也不是以root用户身份运行的?

谢谢,

Dheeraj.

Rya*_*cox 6

您可以运行Applescript(或将Applescript转换为ScriptingBridge或AppleEvents上的Objective-C层)

这是我在一个特定项目中使用的Applescript,它可以执行与您需要的类似的操作:

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
Run Code Online (Sandbox Code Playgroud)