Search code examples
objective-cmacoscore-graphicsquartz-graphics

Get the list of windows for running application on Mac


I used samples from CGWindow API and SonOfGrab samples to get the windows list and grab them, in particularly I used CGWindowListCopyWindowInfo for retrieving the list of windows along with CGWindowListOption specified. However I have a problem here. The thing is that I only want to see the list of windows which belong to a running applications, i.e. I don't want to get windows for dock, system menu etc.

I tried to set kCGWindowListExcludeDesktopElements and kCGWindowListOptionOnScreenOnly flags, they removed some windows I'm not interested in, however even with these options set, I still have 'Dropbox icon in menu bar' window, dock window, system panel window etc. Is there a way to filter them out?

I also tried to request [[NSWorkspace sharedWorkspace] runningApps] to get the list of running applications. My idea was to get the process identifiers from runningApps and remove from the list of windows those ones who don't have process identifiers from the runningApps list. Unfortunately it didn't work out, since the list of running applications is way bigger than I expected and it also contains dashboard applications and other things. I tried to find a property or something like this to distinguish between these 'system apps' and 'real apps', however I wasn't able to achieve this.

Does anyone know how to solve this?

Thanks.


Solution

  • you could:

    • get the bundle for a running application
    • check its plist for if it is has a UIElement key -> those wouldn't be in the dock

    => then filter the windows against the remaining PIDs