macos - 无法在 AppleScript 中设置可设置的辅助功能属性

标签 macos applescript accessibility

看完问题“Programmatically select text range in TextEdit ”,我很好奇为什么不能在 AppleScript 中设置 AXSelectedTextRange 属性,即使属性表明它是可设置的。

打开一个 TextEdit 文档,键入几行,突出显示一部分并运行下面的脚本进行测试。

activate application "TextEdit"
tell application "System Events"
    set attr to attribute "AXSelectedTextRange" of text area 1 of scroll area 1 of window "Untitled" of application process "TextEdit"
    attr's properties -- determine if settable
    set attr's value to {1, 2}
    delay 1
    attr's value
end tell

AppleScript 处理我遗漏的辅助功能属性的方式有什么不同吗?

最佳答案

因为请求的类(NSValue 包含类 NSRange [或 Carbon 中的 CFRange ] 的对象)在 AppleScript 中不存在. AppleScript 中的 list 类等同于 Objective-C 中的 NSArray 类。

另一个例子:

set value of attribute "AXPosition" of window 1 of application process "TextEdit" to {30, 30}

什么都不做,因为值必须是 NSPoint

在 AppleScript 中获取属性值时,“系统事件”将转换 NSRangeNSpointNSSizeNSRect 到一个包含数字的 NSArray(一个 AppleScript 列表),但是当您想要设置值时不会将列表转换为这些类型。

  • NSAccessibility Protocol Reference : NSAccessibilitySelectedTextRangeAttribute : 选定文本的范围 (NSValue)。类 NSValue 可能包含这些类(NSRange、NSpoint、NSSize 或 NSRect)中的一个对象

  • Carbon Accessibility Reference : kAXSelectedTextRangeAttribute : CFTypeRef : 当前选定文本的范围(开始和结束字符位置)。该属性通常是可设置的。该值是编码的 CFRange 结构。

所以在 Objective-C、AppleScriptObjC 或任何其他可以使用 Cocoa 或 Carbon API 的语言(如 UI 浏览器)中都是可能的——但在 AppleScript 中不行。

关于macos - 无法在 AppleScript 中设置可设置的辅助功能属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10491511/

相关文章:

applescript - 用applescript打开程序

html - 使用 CSS 分配角色,而不是将其添加到 HTML 标签

Xcode IB 愚蠢警告

swift - 访问 NSTableviews 自定义 Tablecellview

ios - 如何以编程方式找到 Swift 的版本?

python - 如何从 Python 脚本中运行 AppleScript?

macos - WDIO 自动化。更新 Chrome 浏览器后,设置文本中缺少规范符号

xcode - 从脚本创建 DMG 时出现一些问题

android - 在 Android 中更改由对讲说出的文本

C# : How to detect if screen reader is running?