I tried to get notified when another Application creates a window using the Accessibility API and AXObserver. Here's my code:
let pid = NSWorkspace.shared.frontmostApplication!.processIdentifier
var observer: AXObserver?
if AXObserverCreate(pid, { (observer, element, notification, userData) in
// Does this get executed when a notification comes in?
print(notification)
}, &observer) == .success {
// This does get printed!
print("Successfully created Observer!")
}
if AXObserverAddNotification(observer!, element, notification, nil) == .success {
// This also gets printed!
print("Successfully added Notification!")
}
CFRunLoopAddSource(RunLoop.current.getCFRunLoop(), AXObserverGetRunLoopSource(observer!), CFRunLoopMode.defaultMode)
Am I missing something? The code compiles and runs, but my calback doesn't get executed. Does the callback get executed when a notification comes in?