0
votes

In Cocoa on the Mac, I'd like to detect when a window belonging to another app is closed. How can I do this?

I only know how to detect other window's resize or minilize using

AXObserverAddNotification(observer,application,kAXWindowMiniaturizedNotification, nil);

AXObserverAddNotification(observer, application, kAXWindowResizedNotification, nil);

But I don't find notification like kAXWindowClosedNotification.

1

1 Answers

0
votes

The notification is kAXUIElementDestroyedNotification but it is also sent for other elements. Observe the window or check in the callback if the element is a window.

if (CFStringCompare(notification, kAXUIElementDestroyedNotification, 0) == kCFCompareEqualTo) {
    CFTypeRef role;
    AXError error = AXUIElementCopyAttributeValue((AXUIElementRef)element, kAXRoleAttribute, &role);
    if (error == kAXErrorSuccess) {
        if (CFStringCompare(role, kAXWindowRole, 0) == kCFCompareEqualTo) {
            NSLog(@"Window is closed");
        }
        CFRelease(role);
    }
}