通过MACOS可访问性API在WebKit应用程序中选择文本
我需要使用可访问性API在另一个应用程序(Apple Mail)的WebKit视图中选择文本。
对于常规文本字段,我会执行类似的操作:
func selectText(withRange range: CFRange) throws {
var range = range
guard let newValue: AXValue = AXValueCreate(AXValueType.cfRange, &range) else { return }
AXUIElementSetAttributeValue(self, kAXSelectedTextRangeAttribute as CFString, newValue)
}
但是,在Apple Mail的编写窗口中,每个文本似乎都是类型静态文本
,它不带任何必要的axselectedtextrange
它具有axselectedTextmarkErnange
,它需要axtextmarker
。我只是没有如何创建其中之一。我毫不费力地阅读用户创建选择的文本,使用此在这里/a>,但我无法通过可访问性API选择文本。
I need to select text in a WebKit view of another application (Apple Mail) using accessibility APIs.
For regular text fields, I do something like this:
func selectText(withRange range: CFRange) throws {
var range = range
guard let newValue: AXValue = AXValueCreate(AXValueType.cfRange, &range) else { return }
AXUIElementSetAttributeValue(self, kAXSelectedTextRangeAttribute as CFString, newValue)
}
However, in the composing window of Apple Mail every text seems to be of type Static Text
which doesn't come with the necessary AXSelectedTextRange
It has AXSelectedTextMarkerRange
, though, which requires an AXTextMarker
. I just don't get how to create one of these. I have no trouble reading the text from a user created selection using this here, but I'm unable to select text via the accessibility APIs.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
感谢 willeke 我能够弄清楚的提示。确实可以使用
axtextmarkerforindex
进行操作。知道它实际上很简单。这是我的代码:
Thanks to the hint from Willeke I was able to figure it out. It is indeed possible to do it using
AXTextMarkerForIndex
. Knowing that it's actually pretty straightforward.Here's my code: