0

I want to get the text from whatever textbox the user currently has focussed, in any OSX application. I guess this will be impossible in the completely general case (what if some app implements a completely custom textbox using some low-level graphics calls?) but I would be happy to find a solution for the common case where the textbox is some generic Cocoa element.

Alex Flint
  • 6,040
  • 8
  • 41
  • 80

1 Answers1

1

You can use the Accessibility API or AppleScript, though both use forms of GUI manipulation and neither is 100%.

The Accessibility API lets you inspect the UI of a given app from the perspective of the Accessibility API. It may not see everything. You will basically need to use the Core Foundation C API mostly for this part and the core class you will learn to love is AXUIElementRef. Do note that coordinates are usually in screen hardware coordinates from top left. (counter to what Cocoa uses)

Your code will largely consist of layers of conditionals.
if (blablabla == kAXErrorSuccess) {
 // do the next bit 
}

Before you can begin, you'll need the app to get user approval for controlling other apps.

Then you'll get the app, and begin parsing through its accessibility-exposed interface representations.

Some apps and processes and UI elements will not be visible to the AX APIs at all.

Your other realistic alternative is AppleScript. This is just as awkward. Even if an app is not scriptable, the GUI is, but it's not pretty.

Neither approach is sandboxable.

uchuugaka
  • 12,679
  • 6
  • 37
  • 55