我发现一个解决方案是将窗口列表过滤到仅位于 Dock“下方”的那些窗口(就窗口分层而言)。
下面的代码对我来说效果很好。它获取屏幕上的所有窗口(不包括桌面元素)。它从列表中提取“Dock”窗口的窗口 ID。然后再次获取屏幕窗口,仅过滤到 Dock 窗口“下方”的那些窗口。
// Fetch all on screen windows
CFArrayRef windowListArray = CGWindowListCreate(kCGWindowListOptionOnScreenOnly|kCGWindowListExcludeDesktopElements, kCGNullWindowID);
NSArray *windows = CFBridgingRelease(CGWindowListCreateDescriptionFromArray(windowListArray));
NSLog(@"All on screen windows: %@", windows);
// Find window ID of "Dock" window
NSNumber *dockWindowNumber = nil;
for (NSDictionary *window in windows) {
if ([(NSString *)window[(__bridge NSString *)kCGWindowName] isEqualToString:@"Dock"]) {
dockWindowNumber = window[(__bridge NSString *)kCGWindowNumber];
break;
}
}
NSLog(@"dockWindowNumber: %@", dockWindowNumber);
CFRelease(windowListArray);
if (dockWindowNumber) {
// Fetch on screen windows again, filtering to those "below" the Dock window
// This filters out all but the "standard" application windows
windowListArray = CGWindowListCreate(kCGWindowListOptionOnScreenBelowWindow|kCGWindowListExcludeDesktopElements, [dockWindowNumber unsignedIntValue]);
NSArray *windows = CFBridgingRelease(CGWindowListCreateDescriptionFromArray(windowListArray));
NSLog(@"On screen application windows: %@", windows);
}
else {
NSLog(@"Could not find Dock window description");
}