看完问题“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 中获取属性值时,“系统事件”将转换 NSRange、NSpoint、NSSize 或 NSRect 到一个包含数字的 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/