Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CGWindowID from AXUIElement

I'm trying to automate a foreign OSX application using the accessibility API. Some of the state of the application isn't available through the API, so I acquire it through screen scraping. To do this, I need to get CGWindowID for an accessibility object with a 'Window Role'.

Is there any direct way of acquiring CGWindowID of a 'Window Role' accessibility object? I can get it heuristically, by matching various attributes of the window, such as the size, title and location, but this is really hacky, and I'd feel better if my application would also support the corner cases, even if they are unlikely.

like image 429
Sami Avatar asked Nov 16 '09 15:11

Sami


3 Answers

There is a function from at least Leopard and up (and still around as of 10.7.3):

extern "C" AXError _AXUIElementGetWindow(AXUIElementRef, CGWindowID* out);

Usual caveats about using something like this apply though. It may change as soon as the next OS update!

like image 57
mjmt Avatar answered Oct 14 '22 19:10

mjmt


There’s no way to do that; the accessibility hierarchy is completely decoupled from the actual window/view hierarchy. I think your matching will work best.

like image 20
Ben Stiglitz Avatar answered Oct 14 '22 18:10

Ben Stiglitz


The correct current declaration in a Swift Bridging Header is:

#import <AppKit/AppKit.h>

AXError _AXUIElementGetWindow(AXUIElementRef element, uint32_t *identifier);

As used here: https://github.com/rxhanson/Rectangle/blob/master/Rectangle/Rectangle-Bridging-Header.h

like image 1
railsfanatic Avatar answered Oct 14 '22 19:10

railsfanatic