Swift MacOs – Accessibility – AxError: 无法完成

huangapple go评论107阅读模式
英文:

Swift MacOs - Accessibility - AxError: cannot complete

问题

我正在尝试创建一个函数,该函数将返回任何应用程序中的选定文本。为此,我正在使用辅助功能 API,我的函数如下:

func getSelectedText() -> String? {
    // 获取当前活动的应用程序
    guard let frontmostApplication = NSWorkspace.shared.frontmostApplication,
        let runningApplication = NSRunningApplication(processIdentifier: frontmostApplication.processIdentifier) else {
            return nil
    }

    // 获取应用程序的 AXUIElement
    let applicationElement = AXUIElementCreateApplication(frontmostApplication.processIdentifier)

    var focusedElement: CFTypeRef?
    let result = AXUIElementCopyAttributeValue(
        applicationElement,
        kAXSelectedTextAttribute as CFString,
        &focusedElement
    )
    // !!! result = AxError: cannotComplete !!!
    if result != .success {
       return nil
    }

    let focusedUIElement = focusedElement as! AXUIElement

    // 获取选定的文本
    var selectedText: AnyObject?
    let selectedTextResult = AXUIElementCopyAttributeValue(focusedUIElement, kAXSelectedTextAttribute as CFString, &selectedText)

    if selectedTextResult == .success, let selectedText = selectedText as? String {
        return selectedText
    } else {
        // 尝试获取焦点元素的值
        var value: AnyObject?
        let valueResult = AXUIElementCopyAttributeValue(focusedUIElement, kAXValueAttribute as CFString, &value)

        if valueResult == .success, let value = value as? String {
            return value
        } else {
            return nil
        }
    }
}

然而,我在以下行中遇到了 "AxError: cannotComplete" 错误:

let result = AXUIElementCopyAttributeValue(
    applicationElement,
    kAXSelectedTextAttribute as CFString,
    &focusedElement
)

应用程序沙箱已启用,辅助功能也已在操作系统中授权给该应用程序,不确定是否还有其他原因导致失败?

编辑

当我尝试使用以下代码请求权限时:

let prompt = kAXTrustedCheckOptionPrompt.takeUnretainedValue() as String
let options: NSDictionary = [prompt: true]
let appHasPermission = AXIsProcessTrustedWithOptions(options)

appHasPermission 参数为 false。

英文:

I'm trying to create a fuction, that will return selected text in any application. For that purpose I'm using Accessibiliy API, and my function looks like:

func getSelectedText() -> String? {
    // Get the currently active application
    guard let frontmostApplication = NSWorkspace.shared.frontmostApplication,
        let runningApplication = NSRunningApplication(processIdentifier: frontmostApplication.processIdentifier) else {
            return nil
    }

    // Get the AXUIElement for the application
    let applicationElement = AXUIElementCreateApplication(frontmostApplication.processIdentifier)

    var focusedElement: CFTypeRef?
    let result = AXUIElementCopyAttributeValue(
        applicationElement,
        kAXSelectedTextAttribute as CFString,
        &focusedElement
    )
    // !!! result = AxError: cannotComplete !!!
    if result != .success {
       return nil
    }

    let focusedUIElement = focusedElement as! AXUIElement

    // Get the selected text
    var selectedText: AnyObject?
    let selectedTextResult = AXUIElementCopyAttributeValue(focusedUIElement, kAXSelectedTextAttribute as CFString, &selectedText)

    if selectedTextResult == .success, let selectedText = selectedText as? String {
        return selectedText
    } else {
        // Try getting the value of the focused element instead
        var value: AnyObject?
        let valueResult = AXUIElementCopyAttributeValue(focusedUIElement, kAXValueAttribute as CFString, &value)

        if valueResult == .success, let value = value as? String {
            return value
        } else {
            return nil
        }
    }
}

However, I'm getting AxError: cannot compete on the following line:

let result = AXUIElementCopyAttributeValue(
    applicationElement,
    kAXSelectedTextAttribute as CFString,
    &focusedElement
)

App Sandbox is enabled, accessibility is also granted for this app in OS, not sure is there any other reason to fail like that?

EDIT

When I try to request permission using the following:

let prompt = kAXTrustedCheckOptionPrompt.takeUnretainedValue() as String
let options: NSDictionary = [prompt: true]
let appHasPermission = AXIsProcessTrustedWithOptions(options)

appHasPermission param is false

答案1

得分: 0

抱歉,无法为受沙箱限制的应用提供无障碍API。您应该关闭沙箱功能。

英文:

Unfortunately, Accessibility API isn't available for sandboxed apps. You should turn the sandboxing off.

huangapple
  • 本文由 发表于 2023年1月9日 15:58:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75054454.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定