On Sep 5, 2009, at 12:00 PM, Andy L wrote:
> [Sorry, I've submitted my previous message too early by accident.]
> Can I use JSTalk to send and get text to the clipboard?
> Also, is it possible to simulate key presses?
> How would I go about doing these?
Key presses have always been a bit odd on Mac OS X. I've been wanting
to build a plugin to help with this actually, but haven't gotten
around to it yet.
Here's a sample script which grabs the clipboard, adds some text to
it, puts it back on the clipboard, and then simulates a cmd-v key press.
var pasteBoard = [NSPasteboard generalPasteboard];
// grab a string on the paste board.
var pbString = [pasteBoard stringForType:NSStringPboardType];
// make a new string based on it:
var newString = pbString + "!!!";
// tell it we're going to be putting a single string type on the
[pasteBoard declareTypes:[NSStringPboardType] owner:null];
// and then put it on
[pasteBoard setString:newString forType:NSStringPboardType]
// simulate a cmd-v
// 55, and 9 are virtual key codes. There's probably a reference
somewhere for this.
var keyDown = true;
var keyUp = false;
// command down:
CGPostKeyboardEvent(0, 55, keyDown);
// v down, then up
CGPostKeyboardEvent(0, 9, keyDown);
CGPostKeyboardEvent(0, 9, keyUp);
// command up
CGPostKeyboardEvent(0, 55, keyUp);
Hope that helps,
August 'Gus' Mueller
Flying Meat Inc.