AXUIElementCopyAttributeValue cannot complete
Asked Answered
R

2

6

I'm trying to use the accessibility API to get the active window, the problem is when I try to get the focused application I get a cannot complete error. Below is a small piece of my code:

AXUIElementRef systemElement =
    AXUIElementCreateSystemWide();

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

// error value results in kAXErrorCannotComplete

I have accessibility enabled and have compared my code to code I found on GitHub but nothing seems to work and I'm fresh out of ideas. Perhaps I'm overlooking something?

Rozanna answered 29/12, 2014 at 19:37 Comment(2)
"I get a cannot complete error" Which one in particular? Be explicit and post compiler error messages verbatim in your questions please!Kraus
kAXErrorCannotComplete is the return value of the AXUIElementCopyAttributeValue operation. That's all the information I have unfortunately. I'll update my code sample to make it clearer.Rozanna
B
6

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)");

    }



}


I have a similar problem.I also get a cannot complete error. And I have solved it by set com.apple.security.app-sandbox false which is in (your projectname).entitlements.I think this will help you

<?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>
Bah answered 20/1, 2020 at 15:13 Comment(2)
Accessibility apps do not work in the sandbox (and therefore cannot be distributed on the Mac AppStore) You must disable the sandbox to make this work.Myronmyrrh
How to do this in 2024: under "Signing and Capabilities" in your project settings there is a big section called "App Sandbox". Click the trash can in the upper right corner.Pinna
D
-2

I tried to solve this by first setting a messaging timeout:

AXError error_code = AXUIElementSetMessagingTimeout( system_element, 0.1f );

Which may not have allowed the element to copy the attribute value correctly, but it also didn't cause things to hang (which is what was happening when I was getting the kAXErrorCannotComplete). Once I was done trying to collect the attribute value, I then reset the messaging timeout back to the system default:

error_code = AXUIElementSetMessagingTimeout( system_element, 0.0f );

It seems this did not actually help in my situation, as I had initially thought. Leaving the answer although it didn't seem to correct the issue, it might provide some information for someone.

Donatus answered 20/2, 2015 at 20:57 Comment(2)
Unfortunately this did not work for me. But I did find a workaround. Instead of getting the focused application using kAXFocusedApplicationAttribute, I use GetFrontProcess and GetProcessPID. Even though they're deprecated they work fine. If they ever stop working you can replace them with their NS* variants found in the documentation.Rozanna
Thanks, I will take a look at those and see if that works for my situation. I meant to come back and update the answer, the solution did not actually fix my issue, it was a fluke that it happened to work due because of some race condition. The issue was actually within other software not handling the AXUIElementCopyAttributeValue() correctly for some reason due to their start up code.Donatus

© 2022 - 2024 — McMap. All rights reserved.