views:

464

answers:

2

Getting the active application is not a problem, I already have this implemented.

What I want to know is how to send a "Cmd-C" key combination to the active application so it puts the selected text in the clipboard (general pasteboard).

Or even better: how to tell the active application to do a copy operation without sending the keystroke, or without highlighting the "Edit" menu when the copy operation is triggered.

I can't use NSAppleScript (10.2) because this specific class must be as backwards-compatible as possible (down to 10.0 ideally).

I have looked at NSAppleEvents (which would seem the way to go) but can't seem to find a way to do this. A specific answer would be very welcome!

Thanks in advance!

UPDATE:

The NSAppleScript solution by Rob works but I'm still interested in doing this without the need for NSAppleScript. I've tried using CGPostKeyboardEvent (using the last boolean value to set the key to its "up" or "down" status) but I can't seem to find the key codes or any character code tables. Could someone precise how CGPostKeyboardEvent would be used to type a "c"? (a CGCharCode and CGKeyCode table would be perfect).

+5  A: 

See GCPostKeyboardEvent.

Darren
Thanks for the fast reply, but the reference page for GCPostKeyboardEvent says this method is deprecated in 10.6... I'm working on a commercial product and I need a future-proof solution. The discussion says CGEventCreateKeyboardEvent would be a good replacement candidate, but it's only supported from 10.4 and up... is there another way to do this?
Form
@Form I think the way to handle that is conditional compilation. Use CGPostKeyboardEvent for < 10.3 and CGEventCreateKeyboardEvent for >= 10.4
Amuck
Apple dropping runtime support for the 10.6 SDK is the least of your concerns. Especially you're trying to target pre-10.4, which I suppose would be 10.3.9 since that's the earliest Universal SDK.
Darren
Why would the deprecation in 10.6 be the least of my concerns? (please elaborate if you don't mind)"10.3.9 since that's the earliest Universal SDK" >> didn't know that... does that mean the earliest version of Mac OS X I can build a single app bundle for is 10.3.9? What about an older version?
Form
There's a difference between SDK support and runtime support. Deprecated means that it may be dropped in some future SDK, say 10.8. It does not (necessarily) mean that they will drop runtime support for that deprecated function. Once an API is released publicly Apple essentially agrees to support it forever, or at least a very long time (decades). Furthermore, if you target an old SDK, like 10.4 or even 10.3, you will invariably be using many APIs that have since been deprecated in 10.5 and 10.6.
Darren
In Snow Leopard, the oldest SDK you can target is 10.4. If you have a older version of XCode you can target 10.3.9. Beyond that you would be building a PPC-only application.
Darren
Ok thanks for the clarification! I guess I'll try CGPostKeyboardEvent then!
Form
Oh, and how would I send a key combination (i.e: Cmd-C) using this method?
Form
+2  A: 

If you can enable System Events (access for assistive devices) on the computer, you can send this script with an NSAppleEvent [EDIT: actually NSAppleScript] initWithSource:

tell application "System Events" to keystroke "c" using command down

I don't really have any idea when System Events was introduced, though.

Rob
This seems like a good solution although I think you meant NSAppleScript and not NSAppleEvent. I tried to do that but while the script works in the Script Editor, it does not in my application... I'm hearing the "no text is selected, you can't copy" beep when the script is run in my code. Safari has the focus when this happens, so I'm pretty much clueless about what the problem is...
Form
You're right about the NSAppleScript. Sorry about that. The beep means that System Events is not enabled. It has to be enabled in System Preferences->Universal Access->Enable Access for Assistive Devices.
Rob
You might also be interested in this, then: http://www.macosxhints.com/article.php?story=20060203225241914
Rob