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?
Asked
Active
Viewed 5,822 times
11
pvinis
- 4,059
- 5
- 39
- 59
-
Here is the same question, already answered: http://stackoverflow.com/questions/2107657/mac-cocoa-getting-a-list-of-windows-using-accessibility-api – Nickolay Olshevsky Dec 07 '12 at 10:05
-
Even here http://stackoverflow.com/questions/13759688/how-to-find-out-if-an-instance-of-my-app-is-already-running-or-not/13759928#13759928 – Anoop Vaidya Dec 07 '12 at 11:18
-
Widow list more directly means? – Anoop Vaidya Dec 07 '12 at 11:18
-
thanks. i'll check those two, then come back if i need something. – pvinis Dec 07 '12 at 11:21
1 Answers
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
-
4Hey 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
-
2That'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