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

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

问题描述

我想在 10.9 上以编程方式启用对辅助设备的访问.在 10.8 及更低版本上,我使用以下 Applescript 来启用辅助设备的访问:

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

在 10.9 中,Apple 已将辅助功能选项移至系统偏好设置 ➞ 安全性和;隐私 ➞ 隐私 ➞ 可访问性.与之前版本的 OS X 对所有应用程序使用通用复选框不同,10.9 中的新功能允许用户单独选择哪些应用程序可以控制系统以执行其各种脚本功能.

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 没有向开发者提供任何 API 来以编程方式为应用启用辅助功能.因此,当应用程序使用辅助功能 API 时,Mac OS 10.9 将提示一个对话框,要求最终用户允许启用辅助功能.此外,用户必须在启用辅助功能后重新启动应用程序.

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.

我们能否在 10.9 上使用 Applescript 或任何其他 API 以编程方式启用对辅助设备的访问?任何解决此问题的帮助将不胜感激.

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.

推荐答案

这并没有回答您的问题,但是很高兴知道 10.9 中出现的新 API 调用,它可以让您显示授权屏幕或绕过它:

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);

传递YES 将强制出现授权屏幕,传递NO 将自动跳过它.返回值与 AXAPIEnabled() 返回的值相同,后者在 10.9 中已被弃用.要确保该函数在您的系统上可用,只需将其与 NULL 进行比较:

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
}

您需要将 ApplicationServices.framework 添加到您的项目中,并导入到您的 .m 或 .h 文件中:

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

或使用目标 C:

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

这可以与第一个代码片段配对,通过将@NO传递给kAXTrustedCheckOptionPrompt同时防止系统弹出来测试是否accessibilityEnabled出现并直接打开辅助功能首选项窗格:

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天全站免登陆