You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

关于Catalyst应用中通过macOS Accessibility API设置选中文本失效的技术问询

Catalyst Apps Fail to Replace Selected Text via Accessibility API

I've built an app that adds Vim keybindings across the entire macOS system. It works flawlessly in all native macOS apps that support Accessibility, but I'm hitting a roadblock with Catalyst apps:

  • Text reading functionality mostly works, though there are bugs with line numbers and line ranges
  • The critical issue: I can't replace selected text via the Accessibility API. After running my code, the target text range gets selected as expected, but the new content never replaces the selected text—even though both API calls return .success.

I've been filing Accessibility API-related bug reports with Apple for two years now without any feedback or fixes. I'm trying to figure out if this is an issue with my implementation, or a confirmed API bug.

Here's the core implementation code:

func updateSelectedText() -> Bool {
    var selectedTextRange = CFRange()
    selectedTextRange.location = 2
    selectedTextRange.length = 3
    let newValue = AXValueCreate(.cfRange, &selectedTextRange)
    guard AXUIElementSetAttributeValue(axFocusedElement, kAXSelectedTextRangeAttribute as CFString, newValue!) == .success else { return false }
    guard AXUIElementSetAttributeValue(axFocusedElement, kAXSelectedTextAttribute as CFString, "hehe" as CFTypeRef) == .success else { return false }
    return true
}

This function returns true, but in Catalyst apps it only selects the text from position 2 to 5—it never replaces that selection with "hehe". I have an Xcode project ready that reproduces this issue.

内容的提问来源于stack exchange,提问作者godbout

火山引擎 最新活动