Communicate with the Clipboard? And Keystrokes?

44 views
Skip to first unread message

Andy L

unread,
Sep 5, 2009, 3:00:32 PM9/5/09
to JSTalk Dev
[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?

Thanks in advance. Looking forward to knowing this... It'd be great if
I could migrate my clipboard-enhancing scripts to JavaScript.

-Andy

Gus Mueller

unread,
Sep 5, 2009, 4:23:49 PM9/5/09
to jstal...@googlegroups.com
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
[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.
CGInhibitLocalEvents(true);
CGEnableEventStateCombining(false);

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);


CGEnableEventStateCombining(true);
CGInhibitLocalEvents(false);


Hope that helps,

-gus

--

August 'Gus' Mueller
Flying Meat Inc.
http://flyingmeat.com/

Andy L

unread,
Sep 5, 2009, 7:44:49 PM9/5/09
to JSTalk Dev
Hi Gus,

Thank you for your very complete example. I've just set up JSTalk and
ran successfully the default script in the Editor. I'll try your
example now...

But, I was wondering, should the "jstalk" file (from the binary Zip
you provided) be copied to any particular folder (besides being one
that's in the PATH)?


On Sep 5, 9:23 pm, Gus Mueller <g...@flyingmeat.com> wrote:
> 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.
>
>...

Gus Mueller

unread,
Sep 6, 2009, 2:55:24 PM9/6/09
to jstal...@googlegroups.com
On Sep 5, 2009, at 4:44 PM, Andy L wrote:

> Thank you for your very complete example. I've just set up JSTalk and
> ran successfully the default script in the Editor. I'll try your
> example now...
>
> But, I was wondering, should the "jstalk" file (from the binary Zip
> you provided) be copied to any particular folder (besides being one
> that's in the PATH)?


You've lost me here, sorry :)

How are you wanting to run the script? From a hotkey, out of the
JSTalk editor, from the command line? Something else?

thanks,

Andy

unread,
Sep 6, 2009, 11:22:34 PM9/6/09
to jstal...@googlegroups.com
Sorry, Gus. I was referring to the files in the installation .zip. I
assume "JSTalk Editor.app" can go into the "Applications" folder. But
where does the "jstalk" file go into?

- Andy

Gus Mueller

unread,
Sep 6, 2009, 11:54:49 PM9/6/09
to jstal...@googlegroups.com
On Sep 6, 2009, at 8:22 PM, Andy wrote:

> Sorry, Gus. I was referring to the files in the installation .zip. I
> assume "JSTalk Editor.app" can go into the "Applications" folder. But
> where does the "jstalk" file go into?


Oh- that's a command line tool for JSTalk.

Good question. So, I'm not sure where the "recommended" place to put
unix executable places is these days. In my home dir, I've got ~/unix/
bin , which I add to my $PATH, and that's where I put odd scripts and
such that I don't want clobbered by the system.

You might try that if sounds reasonable. Or if anyone else has any
other suggestions...?

Andy

unread,
Sep 7, 2009, 12:03:24 AM9/7/09
to jstal...@googlegroups.com
Ah, ok. I've been putting stuff like that in "/usr/local/bin" -- I
think that's the correct place (that directory was recommended in some
other program's installation instructions), but I'm not really really
sure. ;)

Thanks.

-Andy
Reply all
Reply to author
Forward
0 new messages