I'm building yabbistir, a clipboard history for macOS. Its one trick is where it opens: press ⇧⌘V and the panel appears 8 points under the text caret of whatever app you're typing in. You type to search, press ↵, and the item is pasted back into that app. Your eyes don't travel and your hands don't leave the keyboard.
That needs three things macOS doesn't hand you directly:
the caret's position in someone else's app,
a sensible answer when that app won't tell you,
a window that takes keystrokes without taking focus away from the app you're typing in.
Here is how each one works, with code from the app, shortened.
Asking Accessibility where the caret is
There's no "where is the caret" API on macOS. There is the Accessibility API, the one assistive apps like VoiceOver use to read other apps' interfaces. Your app needs the Accessibility permission for it ( AXIsProcessTrusted() tells you whether you have it), and then it can ask three questions:
Which element has keyboard focus?…