AXUIElementCopyAttributeValue无法完成

问题描述 投票:3回答:2

我正在尝试使用可访问性API来获取活动窗口,问题是当我尝试获取重点突出的应用程序时出现了无法完成的错误。以下是我的一小段代码:

AXUIElementRef systemElement =
    AXUIElementCreateSystemWide();

AXUIElementRef focused = nullptr;
AXError error = AXUIElementCopyAttributeValue (systemElement,
      kAXFocusedApplicationAttribute, (CFTypeRef*) &focused);

// error value results in kAXErrorCannotComplete

我已启用了可访问性,并将我的代码与我在GitHub上找到的代码进行了比较,但似乎没有任何效果,并且我不怎么想。也许我忽略了什么?

c++ macos accessibility osx-yosemite macos-carbon
2个回答
0
投票

tried通过首先设置消息传递超时来解决此问题:

AXError error_code = AXUIElementSetMessagingTimeout( system_element, 0.1f );

可能不允许该元素正确复制属性值,但也没有导致任何事情挂起(这就是我获取kAXErrorCannotComplete时发生的情况)。尝试收集属性值后,然后将消息传递超时重置为系统默认值:

error_code = AXUIElementSetMessagingTimeout( system_element, 0.0f );

似乎像我最初想到的那样,这实际上对我的情况没有帮助。留下答案虽然似乎不能解决问题,但可能会为某些人提供一些信息。


0
投票

import Cocoa
//import SwiftUI

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
    private let systemWideElement = AXUIElementCreateSystemWide()

    func applicationDidFinishLaunching(_ aNotification: Notification) {
NSEvent.addGlobalMonitorForEvents(matching: NSEvent.EventTypeMask.leftMouseUp, handler: keyDown)

    }

    func keyDown(event:NSEvent!) {
        let cgevent = CGEvent(source: nil)!.location
        let y = cgevent.y
        let x = cgevent.x
        var ref: AXUIElement?
        let ret = AXUIElementCopyElementAtPosition(self.systemWideElement, Float(x), Float(y), &ref);
        if ret==AXError.cannotComplete{
            print("error")
        }
        print("key down is \(x),\(y),\(ret)");

    }



}


我有类似的问题。我也遇到无法完成的错误。我已经通过将com.apple.security.app-sandbox设置为false来解决了该问题,该错误位于(您的项目名称).entitlements中。我认为这将对您有所帮助

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
        <key>com.apple.security.app-sandbox</key>
        <false/>
        <key>com.apple.security.files.user-selected.read-only</key>
        <true/>
</dict>
</plist>
© www.soinside.com 2019 - 2024. All rights reserved.