AXUIElementCopyAttributeValue 无法完成
AXUIElementCopyAttributeValue cannot complete
本文关键字:AXUIElementCopyAttributeValue 更新时间:2023-10-16
我正在尝试使用辅助功能 API 来获取活动窗口,问题是当我尝试获取重点应用程序时,我收到无法完成的错误。下面是我的一小段代码:
AXUIElementRef systemElement =
AXUIElementCreateSystemWide();
AXUIElementRef focused = nullptr;
AXError error = AXUIElementCopyAttributeValue (systemElement,
kAXFocusedApplicationAttribute, (CFTypeRef*) &focused);
// error value results in kAXErrorCannotComplete
我启用了可访问性,并将我的代码与我在 GitHub 上找到的代码进行了比较,但似乎没有任何效果,而且我的想法很新鲜。也许我忽略了什么?
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 解决了它。我认为这会对你有所帮助
<?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>
我试图通过首先设置消息传递超时来解决这个问题:
AXError error_code = AXUIElementSetMessagingTimeout( system_element, 0.1f );
这可能不允许元素正确复制属性值,但它也没有导致事情挂起(这就是我获得 kAXErrorCannotComplete 时发生的事情)。 尝试收集属性值后,我将消息传递超时重置回系统默认值:
error_code = AXUIElementSetMessagingTimeout( system_element, 0.0f );
似乎这实际上并没有像我最初认为的那样帮助我的情况。留下答案虽然它似乎没有纠正问题,但它可能会为某人提供一些信息。
相关文章:
- 没有找到相关文章