Getting a unique ID for a window of another application
Asked Answered
L

1

5

I'm a newbie Cocoa developer and I'm developing my first application. I want to read a unique identifier from any window of any application - whether it's Cocoa or Carbon. Cocoa applications make their window IDs available to AppleScript (although I'm sure there's a much better way to do this through a proper Objective C route), but I'm trying to access window IDs from documents in the Adobe apps. This seems to be a lot trickier. All I can seem to find in the reference libraries is HIWindowGetCGWindowID:

"This function returns the window ID assigned by the window server when a window is created. The window ID is not generally useful with any other Carbon function, but may be used with other Mac OS X functions that require a window ID, such as functions in OpenGL."

Can this be used to get the ID from my program? Or is it just a function that can be used within one application?

If someone could point me in the right direction, I'd be eternally grateful.

Leid answered 23/11, 2008 at 0:38 Comment(2)
What exactly do you want to do with the window ID you receive?Zo
I'm making a time tracking application and I want to get the active document path. Grabbing the window ID of the active window means I can track time for unsaved documents and means that I have a unique ID for when the file is removed or renamed.Leid
T
13

The function HIWindowGetCGWindowID() can only return a CGWindowID for one of your app's windows, since a WindowRef from another program won't be valid in yours.

The function CGWindowListCopyWindowInfo() from CGWindow.h will return an array of dictionaries, one for each window that matches the criteria you set, including ones in other applications. It only lets you filter by windows above a given window, windows below a given window and "onscreen" windows, but the dictionary returned includes a process ID for the owning app which you can use to match up window to app. In each returned dictionary the kCGWindowNumber key will point to the window ID as a CFNumber. There is also a CGWindowListCreate() function that only returns an array of CGWindowIDs. There is basically no documentation for these functions beyond the CGWindow.h header and the Son of Grab sample code. Also, it's 10.5 only.

Toronto answered 23/11, 2008 at 3:59 Comment(1)
Fantastic. Thankyou so much Boaz! Stack Overflow rules.Leid

© 2022 - 2024 — McMap. All rights reserved.