Front most window using CGWindowListCopyWindowInfo

Grab a copy of Apple’s sample app: Son Of Grab. Then build and run it, and you’ll have a very handy interactive app for investigating all the windows known to the window server.

The answer to your question is that windows are ordered front to back, but there are far more user interface elements defined as windows than you are expecting. This includes the menubar, the dock, the services menu, the Desktop, etc. These are defined at different levels, corresponding to kCGWindowLayer in your output.

You want the first window defined at the standard window level (zero). And if you look at your output, the first window with kCGWindowLayer == 0 is in fact the 7th.

Leave a Comment