I'm new to Cocoa and I want to know how I can get a list of all open windows. I'm not talking about running programs, since this would be an easy task. I want to list windows, for example, if I have Preview running with 10 pdf opened, my program should be able to retrieve a list with all those pdf. I also want to know if there's some API available to interact with already open windows (and maybe move them or do something else) from an external application. Thank you.
-
If you're new to Cocoa, this isn't the topic for you. It's hard and solution won't involve much Cocoa… – Mike Abdullah Jan 25 '10 at 23:27
-
Yes I know that, but I'd like to be able to solve this problem. Of course I'm learning cocoa in the meanwhile and I'm not new to computer programming, so I think that cocoa is not a problem for me. The problem is that what I want to do is hard for the reason already written. – Raffo Jan 30 '10 at 21:10
3 Answers
You can use CGWindowListCopyWindowInfo
to retrieve a list of windows in the current user session.
It's a Carbon function, though - not Cocoa - but perfectly callable from a Cocoa app and documented here: Quartz Window Services Reference : CGWindowListCopyWindowInfo().

- 6,572
- 3
- 37
- 65
I have Preview running with 10 pdf opened, my program should be able to retrieve a list with all those pdf.
Well, that's the hard part. You can get the windows via Accessibility, but Accessibility doesn't associate them with any kind of document object.
AppleScript would work for some applications, but Preview isn't one of them. Indeed, most apps aren't.
So, you can get a list of windows easily enough, but getting meaningful information about or from inside those windows ranges from tricky to impossible. Your best bet may be a combination of Accessibility, AppleScript, and bug reports for un-scriptable applications, and the knowledge that your app will not work on everything.

- 95,783
- 15
- 211
- 370
-
So the think is harder than I thought... But maybe if I just could get the window title I could have the filename, but this is not obvious and could not be useful at all. – Raffo Jan 26 '10 at 20:46
-
Yeah, because the filename alone doesn't tell you *where* it is, and many different files may have the same name; and because the user may have multiple files with the same name open, leaving you with no way to tell which window identifies which file. – Peter Hosey Jan 26 '10 at 21:33
-
@PeterHosey's hint *AppleScript* is definitely not the most beautiful, but the easiest and most straight-forward I've found. This is what I use in my Cocoa application: `tell application (path to frontmost application as Unicode text) to get (path of document 1)` to get the current *document* – Julian F. Weinert Jan 06 '15 at 16:00
-
Of course you can loop through all documents, windows, applications (?), etc. – Julian F. Weinert Jan 06 '15 at 16:01
if you need WIndow List or UI Specific of window control,
if its UI Specific control ,you can refer Apple son of Grab example,
It displays Windows List running on the desktop, probably you can trim down as par your needs.

- 3,765
- 10
- 48
- 97