我想在10.9上启用辅助设备的访问。在10.8和更低版本上,i_使用以下AppleScript启用辅助设备的访问:
tell application "System Events"
if UI elements enabled is false then
set UI elements enabled to true
end if
end tell
在10.9版本中,苹果将可访问性选项移动到系统首选项安全和隐私隐私可访问性。与OS X以前的版本不同,它对所有应用程序都使用了通用复选框,10.9中的新功能允许用户单独选择哪些应用程序可以获得对系统的控制,以执行其各种脚本化功能。
苹果还没有为开发者提供任何API,以编程方式实现应用程序的可访问性。因此,当应用程序使用可访问性API时,Mac OS 10.9将提示一个对话框,以获取最终用户的权限以启用可访问性。此外,用户必须在启用辅助功能后重新启动应用程序。
我们是否可以使用AppleScript或任何其他API在10.9上以编程方式启用辅助设备的访问?任何帮助解决这个问题的人都将不胜感激。
最佳答案:
这并不能回答您的问题,但最好了解10.9中出现的新API调用,并允许您显示授权屏幕或绕过它:
NSDictionary *options = @{(id)kAXTrustedCheckOptionPrompt: @YES};
BOOL accessibilityEnabled = AXIsProcessTrustedWithOptions((CFDictionaryRef)options);
传递
YES
将强制显示授权屏幕,传递NO
将自动跳过它。返回值与AXAPIEnabled()
返回的值相同,该值在10.9中已被弃用。要确保该功能在您的系统上可用,只需将其与NULL
进行比较:if (AXIsProcessTrustedWithOptions != NULL) {
// 10.9 and later
} else {
// 10.8 and older
}
您需要将
ApplicationServices.framework
添加到项目中,然后导入到.m或.h文件:#import <ApplicationServices/ApplicationServices.h>
很遗憾,授权屏幕不允许用户直接授权应用程序,它只打开系统首选项的正确部分。顺便说一下,您可以直接进行,而不必经过无用的系统对话:
tell application "System Preferences"
set securityPane to pane id "com.apple.preference.security"
tell securityPane to reveal anchor "Privacy_Accessibility"
activate
end tell
或使用目标C:
NSString *urlString = @"x-apple.systempreferences:com.apple.preference.security?Privacy_Accessibility";
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:urlString]];
这可以与第一个代码片段配对,通过将
accessibilityEnabled
传递给@NO
来测试是否kAXTrustedCheckOptionPrompt
,同时防止出现系统弹出窗口,而不是直接打开“辅助功能首选项”窗格:NSDictionary *options = @{(id)kAXTrustedCheckOptionPrompt: @NO};
BOOL accessibilityEnabled = AXIsProcessTrustedWithOptions((CFDictionaryRef)options);
if (!accessibilityEnabled) {
NSString *urlString = @"x-apple.systempreferences:com.apple.preference.security?Privacy_Accessibility";
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:urlString]];
}