Equinox2000
Equinox2000

Reputation: 576

How to check CGWindowID still valid

If I am getting a CGWindowID (_windowID) as follows...

NSArray *windowList = (__bridge NSArray *)CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly, kCGNullWindowID);
for (NSDictionary *info in windowList) { 
    if ([[info objectForKey:(NSString *)kCGWindowOwnerName] isEqualToString:@"window name"] && ![[info objectForKey:(NSString *)kCGWindowName] isEqualToString:@""]) {
        _windowID = [[info objectForKey:(NSString *)kCGWindowNumber] unsignedIntValue];
    }
}

How do I properly test the window id is still valid and the window has not been closed? Do i just run similar code just checking window id exists?

Thanks in advance

Upvotes: 1

Views: 859

Answers (1)

Michael Dautermann
Michael Dautermann

Reputation: 89539

The documentation for the kCGWindowListOptionOnScreenOnly constant says:

List all windows that are currently onscreen. Windows are returned in order from front to back. When retrieving a list with this option, the relativeToWindow parameter should be set to kCGNullWindowID.

So the windows would certainly be on screen, since nothing appears to be happening between the call to CGWindowListCopyWindowInfo and your action on it.

Maybe you'd want to test to make sure they are not hidden or minimized?

Upvotes: 1

Related Questions