11

I want to get the window list for a running application. I can get the running application list from [[NSWorkspace sharedWorkspace] runningApplications], but the window list is only available on NSApplication. Is there some way to convert from NSRunningApplication to NSApplication, or some way to get the window list more directly?

pvinis
  • 4,059
  • 5
  • 39
  • 59

1 Answers1

10

You need to look at the CoreGraphics call CGWindowListCopyWindowInfo.

You call it like this

    CFArrayRef windowList = CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements, kCGNullWindowID);

and then iterate over the array of window information, find the ones that are from the application you're interested in, and do what you want with it.

Ken Aspeslagh
  • 11,484
  • 2
  • 36
  • 42
  • 4
    Hey Ken! That's a good answer/hint, but it still doesn't tell how to get the `CG/NSWindow` ;-) `NSRunningApplication` doesn't provide windows/window number and I'm crashing (probably doing something wrong) when trying to get the `NS/CGWindow` with my `CGWindowID` obtained with `CGWindowListCopyWindowInfo` – StuFF mc Aug 03 '14 at 18:09
  • 2
    That's because you can't create an NSWindow for another application. As far as getting the windows for a specific application, you can examine and compare the value of the `kCGWindowOwnerPID` key to the `processIdentifier` property of `NSRunningApplication`. – mattsven Oct 15 '17 at 14:16