MacOS Accessibility noValue error when accessing kAXFocusedUIElementAttribute

64 Views Asked by At

I am working on a MacOS app which uses accessibility API to detect text fields.

I have done so far and I was preparing to launch my app. It works perfectly on my Mac. Then I decided to share the app with some of my friends for testing purposes.

Then I noticed after getting accessibility permission while accessing kAXFocusedUIElementAttribute return AXError = 25212 which is AXError.noValue.

The interesting part is it can detect URL fields of Google Chrome and Safari but no more.

Here is my code for finding a focused element

extension pid_t {

    var focussedField: AnyObject? {
        let axUIElementApplication = AXUIElementCreateApplication(self)
        var focussedField: AnyObject?
        let result = AXUIElementCopyAttributeValue(axUIElementApplication, kAXFocusedUIElementAttribute as CFString, &focussedField)
        guard result == .success else {
            logger("Failed to get focussedField \(result.rawValue)", source: .pid)
            Events.PIDExtensionFocusedFieldError(code: result.rawValue).sendEvent()
            return nil
        }
        return focussedField
    }
}

I am observing front most applications with these pieces of observer.

cancellable = NSWorkspace.shared.notificationCenter
    .publisher(for: NSWorkspace.didActivateApplicationNotification)
    .map { notification -> NSRunningApplication? in
        notification.userInfo?[NSWorkspace.applicationUserInfoKey] as? NSRunningApplication
    }
    .sink { [weak self] application in
    // assigning application and other stuffs
    }

I have searched for some questions and tried to debug via logs and Accessibility Inspector.

Here is the twist: After launching Accessibility Inspector works perfectly on every buggy device.

0

There are 0 best solutions below