"If you can't read the selection, ask the app to copy it — then hand the clipboard back exactly as you found it."
Borrow the Clipboard, Then Give It Back
When AX can't see the selection, there's one thing every app understands: Cmd+C. If Flint synthesizes a copy, the opaque app puts the selected text on the clipboard, and Flint can read it there. The catch is obvious — that clobbers whatever the user had on the clipboard. So the probe is built to be reversible: snapshot the clipboard, borrow it for one copy, read the result, and restore the original. Done right, the user's clipboard is untouched and they never know it was borrowed.
Synthesizing Cmd+C
The copy itself is a synthetic keystroke. You build a key-down and key-up event for the C key with the Command flag set and post them to the event stream, exactly as if the user had pressed Cmd+C. The frontmost app receives it and does what it always does with a copy. Flint isn't parsing the app's internals — it's using the one universal gesture the app already implements, and reading the standard place the result lands.
NSPasteboard the instant after posting, you'll get the old contents and think the probe failed. The correct pattern is to record the pasteboard's changeCount before, then wait until it increments (with a short timeout) — the bump is your proof the copy actually happened.Reversible Means Restored
The whole reason this probe is acceptable is the restore step. Before the copy, Flint captures the complete current pasteboard. After reading the probed text, it writes that captured state back. If the copy never produced anything (changeCount never moved), Flint restores anyway and reports no selection — it never leaves the clipboard in a borrowed state, success or failure. The next lesson is about doing that capture-and-restore completely, because 'restore the clipboard' is harder than it sounds.