8

Actually I'm trying to get the list of ALL open windows as next:

let options = CGWindowListOption(arrayLiteral: .excludeDesktopElements, .optionOnScreenOnly)
let windowsListInfo = CGWindowListCopyWindowInfo(options, CGWindowID(0))
let infoList = windowsListInfo as NSArray? as? [[String: AnyObject]]

but the problem here is that I'm getting also Dock or Window Server, SystemUIServer or widgets on the status bar. How can I improve my code to get avoid from those elements and to get only windows' list, as Xcode, Finder, etc.?

J. Doe
  • 521
  • 4
  • 15

2 Answers2

11

It seems that all visible windows have the value 0 for key kCGWindowLayer

import Cocoa

let options = CGWindowListOption(arrayLiteral: .excludeDesktopElements, .optionOnScreenOnly)
let windowsListInfo = CGWindowListCopyWindowInfo(options, CGWindowID(0))
let infoList = windowsListInfo as! [[String:Any]]
let visibleWindows = infoList.filter{ $0["kCGWindowLayer"] as! Int == 0 }

print(visibleWindows)
Jansindl3r
  • 360
  • 3
  • 18
vadian
  • 274,689
  • 30
  • 353
  • 361
0
public static func visibleWindows() -> [NSWindow]
{
    return NSApplication.shared.windows.filter { $0.isVisible }
}