Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

API for intercepting text input in another app?

I was wanting to make an app similar to something like TextExpander, but I am not sure how you would intercept the text. As far as I can tell, I need to start with NSAccessability. Could anyone share some snippets, or at least point me in the right direction?

like image 621
Weston Avatar asked Dec 28 '12 16:12

Weston


2 Answers

First off, you should be aware that, because of the sandboxing requirement, this isn't possible at all if you want to sell your app in the App Store.

If you don't intend to sandbox your app, you can use the NSEvent class method addGlobalMonitorForEventsMatchingMask: to create a global key event handler that gets called when keys are pressed in other apps (but not your own app, use addLocalMonitor... for that).

To actually insert snippets, like TextExpander, there are several ways. You can use the accessibility APIs, but that requires that the app(s) you're targeting support accessibility, which isn't always the case.

Another option is to use the Quartz Event Services (CGEvent) APIs which provide (among other things) a low-level method to simulate key events.

like image 89
omz Avatar answered Oct 24 '22 09:10

omz


Edit: Nevermind. You're asking about Mac OS. I thought you were asking about iOS.

You should look at how TextExpander is used by other apps. The target app has to build in support for TE by making an object provided by TE a delegate of the text field. You can't run your code in someone else's app. They have to compile your code into their app. That's why there's a TextExpander SDK.

Once the TextExpander code is in the target app, the text field delegate gets the shared snippets by looking for snippets put into a shared pasteboard.

like image 37
Fabian Avatar answered Oct 24 '22 08:10

Fabian