7

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.

Sami
  • 3,263
  • 3
  • 29
  • 37

3 Answers3

12

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!

mjmt
  • 329
  • 3
  • 8
  • how did you find this api? – Jason Jul 29 '13 at 02:52
  • 1
    @Jason Mostly just otool (the Xcode equivalent of nm, ldd, objdump and the like), and some hacks based on it that help make sense of disassembly etc. This stackoverflow thread is helpful: http://stackoverflow.com/questions/9494495/what-is-your-favorite-disassembler-tool-in-mac-os-x – mjmt Nov 19 '13 at 19:09
  • 3
    Does this still work? How would I use this from Swift? I tried and got the error "Use of unresolved identifier '_AXUIElementGetWindow'" – Anshuman Sharma Jan 13 '18 at 20:22
  • 1
    Still works on SDK 10.15 objective-c. But undocumented API still makes me worried. – Vannes Yang Jul 05 '21 at 07:25
4

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.

Ben Stiglitz
  • 3,994
  • 27
  • 24
  • 2
    Thanks for the answer. I already suspected that, but just nice to have someone else confirm it. Anyway, I have my matching logic working good enough for my purposes through position and size matching, as well as some additional logic used to move windows a pixel left/right/up/down in case on conflicts. – Sami Nov 20 '09 at 03:09
3

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

Tom Grushka
  • 439
  • 5
  • 9