I'm trying to understand SonOfGrab. I want the top-most (i.e., visible) window at a point, or at least its frameRect. Does CGWindowListCopyWindowInfo return in any particular order? Is kCGWindowLayer the stacking level? If so, which direction?
Top Window at Point
See the docs for the window list options. For any of the constants with "OnScreen" in their name, it's documented that "Windows are returned in order from front to back."
The
kCGWindowLayer
constant corresponds to what's called the window level in Cocoa. So, normal windows are in one layer, modal windows are in another, floating panels are in another, etc. So, it is related to the z-order, but windows in the same layer will have the same value, so the layer alone isn't sufficient to tell you the z-order.
Thanks. That seems to be working. However, I am stil having a few issues.
Currently, I only do this for windows in my Cocoa application. Working with NSScreen, etc. What I do is first select a screen, which then appears in my capture window, Then I click a "window" button, which gives 15 sec to set the window, which is done by clicking that window's image in the capture window. I map that point back to the full screen to identify the window and get it's frame rect. In some windows, I get back a content rect or specified view rect instead. I just run through the app's document windows (several per doc).
I decided to try to extend this process to any window, not just my own, which is why I started looking at SonOfGrab. I'm finding out that these routines apparently work in Retina coordinates, so I am going to have to compensate for that. Also, I would prefer to get the window's contentRect rather than frameRect. Not sure that is doable for windows not in my app. I also have a drag rect selection, so I suppose that could be used, but it is not as accurate or convenient as just clicking on the window image in the capture window.
Got this working now. Had to reverse the order in which I was doing things. Now I check all windows, determine if top one is one of mine, check for specified capture rect. That pretty much gets it. Took awhile to get all the coordinate filps right.