Paste into currently active app

I need to create an app that has a global shortcut key that when pressed will paste some text into the currently active app on Mac. So for example, if you’re using Textedit, pressing the shortcut key would paste some text into directly into Textedit. The idea is to have some frequently used snippets of text in my app that can be pasted into whatever app you’re currently using with a key press.

Obviously I could setup a global shortcut key that copied my text snippet into the clipboard and then you could use the normal CMD+V to paste it, but I’d like to do it in one step with a single shortcut if possible.

I’ve looked at AppleScript but this seems to be a no go for the App Store and with Mavericks new per application accessibility settings it’s a lot more complex to implement.

Anyone know if this will even be possible? Is there any way of triggering a paste command from my app into another?

What you describe was called ‘plugging characters into the keyboard’ in the PC Dos era. The program modified the keyboard driver buffer by adding characters into it, therefore acting as if someone had typed. I do not know if this is still possible under Windows, but for all intents and purposes, I see no application ever doing that in Mac OS X. Apple system is guarded against applications taping into the very core of it, and as you mention the App Store, sandboxing will stop such an attempt dead in its tracks.

I am not so sure Apple Script is impossible to use in an App Store program, as after all it is like just another shell, but as you pointed out, the complexity maybe a big hurdle.

The option of placing text into the clipboard seems like the most sensible (and easy) one. After all, Command-V looks very much like a shortcut to me…

If you look into the System Preferences, Keyboard, then “Text” you can use the Auto text replacement as a substitute. I have several set-up for dealing with customer e-mails.

Otherwise the process you’re looking for is to:
#1 Set the text on the clipboard to something (only a foreground application can do this, so it requires some noodling).
#2 Then the best bet is to send the keyboard combinations to trigger a Paste.

However as Michel says, you may have a hard time if you intend to ship your application on the Mac App Store, I’m not even sure a Sandboxed application can issue keyboard event and I know for sure you won’t be able to use System Events to do it.

I forgot to mention that in my auto test replacement, I have it set up so when I type ‘nmi’ it auto expands it for me to ‘My name is Sam Rowlands and I’m one of the developers here at Ohanaware. Thank you for your e-mail.’ In nearly every Cocoa application, It took several attempts to enter it in this forum without the OS auto expanding it!

The other way I thought, is you could show a floating palette, then simply drag the snippets from the palette into a text field. Not as fast, but totally App Store savvy and easier for non Keyboard Jockeys.

I had a feeling it may be the case that it won’t work on the Mac. Oh well! The drag and drop idea is a good one though, so I’ll probably just use that. Not as quick for those who like shortcut keys, but better than nothing.

Cheers for the help!

Just to clarify, it’ll work on OS X, but it is a Sandbox violation and thus if you want to sell on the Mac App Store, you’ll be refused.