以编程方式在10.9上启用辅助设备的访问 [英] Enable access for assistive devices programmatically on 10.9

查看:814
本文介绍了以编程方式在10.9上启用辅助设备的访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要在10.9上以编程方式启用辅助设备的访问。 10.8以下我使用以下Applescript启用辅助设备的访问:

 告诉应用程序系统事件
如果UI元素启用为false,那么
将UI元素设置为true
end if
end tell

使用10.9,Apple已将无障碍选项移动到系统偏好设置➞安全&隐私权➞隐私权➞无障碍。与以前版本的OS X(对所有应用程序使用通用复选框)不同,10.9中的新功能允许用户单独选择哪些应用程序可以控制系统执行各种脚本功能。





Apple没有向开发人员提供任何API,以便以编程方式为应用启用辅助功能。因此,Mac OS 10.9将提示一个对话框的最终用户权限,当应用程序使用辅助功能API启用辅助功能。此外,用户必须在启用辅助功能后重新启动应用程序。





我们可以使用Applescript或任何其他API以编程方式在10.9上启用对辅助设备的访问吗?

解决方案

这不会回答你的问题,但是很好知道一个新的API调用,出现在10.9,让你显示授权屏幕或绕过它:

  NSDictionary * options = @ { id)kAXTrustedCheckOptionPrompt:@YES}; 
BOOL accessibilityEnabled = AXIsProcessTrustedWithOptions((CFDictionaryRef)options);

传递 YES 会强制授权屏幕出现,传递 NO 将默认跳过它。返回值与 AXAPIEnabled()返回的值相同,后者在10.9中被弃用。要确保该功能在您的系统上可用,只需将其与 NULL

if(AXIsProcessTrustedWithOptions!= NULL){
// 10.9及更高版本
} else {
// 10.8及更高版本
}

您需要向您的项目中添加 ApplicationServices.framework 到.m或.h文件:

  #import< ApplicationServices / ApplicationServices.h> 

很遗憾,授权屏幕不允许用户直接授权应用程序只是打开了系统首选项的右边部分。顺便说一句,你可以直接做而不经过无用的系统对话:

 告诉应用程序系统首选项
set securityPane to pane idcom.apple.preference.security
告诉securityPane显示锚点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]];
}


I want to enable access for assistive devices programatically on 10.9. On 10.8 and lower I was using following Applescript to enable access for assistive devices:

tell application "System Events"
if UI elements enabled is false then
    set UI elements enabled to true
end if
end tell

With 10.9, Apple has moved the accessibility options to System Preferences ➞ Security & Privacy ➞ Privacy ➞ Accessibility. Unlike previous versions of OS X, which used a universal checkbox for all applications, the new functionality in 10.9 allows users to individually choose which apps can gain control of the system to perform their various scripted functions.

Apple has NOT provided any API to developers to programmatically enable accessibility for an app. So Mac OS 10.9 will prompt a dialog for end user permission to enable Accessibility when application uses accessibility APIs. Additionally User has to Relaunch the application after enabling Accessibility.

Can we enable access for assistive devices programmatically on 10.9 using Applescript or any other APIs? Any help to fix this issue would be greatly appreciated.

解决方案

This doesn’t answer your question, but it’s good to know about a new API call that appeared in 10.9 and lets you display the authorization screen or bypass it:

NSDictionary *options = @{(id)kAXTrustedCheckOptionPrompt: @YES};
BOOL accessibilityEnabled = AXIsProcessTrustedWithOptions((CFDictionaryRef)options);

Passing YES will force the authorization screen to appear, passing NO will silently skip it. The return value is the same as the one returned by AXAPIEnabled(), which is getting deprecated in 10.9. To make sure that the function is available on your system, just compare it to NULL:

if (AXIsProcessTrustedWithOptions != NULL) {
    // 10.9 and later
} else {
    // 10.8 and older
}

You'll need to add ApplicationServices.framework to your project, and import to your .m or .h file:

#import <ApplicationServices/ApplicationServices.h>

It’s quite a pity that the authorization screen doesn’t let the user to authorize the app directly, it just opens the right part of the System Preferences. Which, by the way, you can do directly without going through the useless system dialogue:

tell application "System Preferences"
    set securityPane to pane id "com.apple.preference.security"
    tell securityPane to reveal anchor "Privacy_Accessibility"
    activate
end tell

or using Objective C:

NSString *urlString = @"x-apple.systempreferences:com.apple.preference.security?Privacy_Accessibility";
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:urlString]];

This can be paired with the first code snippet to test whether accessibilityEnabled by passing @NO to kAXTrustedCheckOptionPrompt while preventing the system pop-up to appear and instead opening the Accessibility preferences pane directly:

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]];
}

这篇关于以编程方式在10.9上启用辅助设备的访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆