问题
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.
回答1:
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 CGWindowID
s. 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.
来源:https://stackoverflow.com/questions/311956/getting-a-unique-id-for-a-window-of-another-application