PDA

View Full Version : Shell: paste + retrieve contents of clipboard.




fhill2
Jun 21, 2012, 01:38 AM
I mainly use Max/MSP for my audio programming, but today I am working on a project that requires the use of shell. Is it possible to do this:
Retrieve the contents of the clipboard?
Send a keystroke to an application without loosing focus, for example, I want to initiate a paste command (with command-v) to an application?
Thanks!



subsonix
Jun 21, 2012, 03:45 AM
You can access the clipboard from the shell with pbpaste and pbcopy. How you are going to paste to another application depends on what input methods are available.

fhill2
Jun 21, 2012, 05:22 AM
What do you mean by input methods? :)

subsonix
Jun 21, 2012, 05:56 AM
What do you mean by input methods? :)

The means that the program have to receive data.

fhill2
Jun 21, 2012, 06:44 AM
How would I send a keystroke to an application from terminal?
regarding input methods, I suppose the application Ableton Live has 2 input methods?
Key Input.
Drag + Drop File.

subsonix
Jun 21, 2012, 08:52 AM
How would I send a keystroke to an application from terminal?
regarding input methods, I suppose the application Ableton Live has 2 input methods?
Key Input.
Drag + Drop File.

A shell is text based, GUI applications responds to events from the window server, different paradigms. You could pipe or redirect the output of the clipboard in a shell to special files like sockets, device nodes and so on, but that requires that the receiving application has such an interface and accepts input there according to some protocol.

notjustjay
Jun 21, 2012, 11:16 AM
I believe a middle-click would paste clipboard text into a shell.

kryten2
Jun 21, 2012, 12:16 PM
How would I send a keystroke to an application from terminal?

Use osascript like :

#!/bin/bash
osascript "/path/to/your/script.scpt"

or

#!/bin/bash
osascript <<-EOF


-- Activate the application you'd like to target
tell application "Finder" to activate
tell application "System Events"
-- Send your keystroke
tell process "Finder"
keystroke "v" using command down
end tell
end tell

EOF

subsonix
Jun 21, 2012, 12:58 PM
If you are going to use nothing but Apple script you may as well just use:


#!/usr/bin/osascript


Also, in order to paste to Ableton you need to first select a part of the UI (like a track) that can receive content from the clip board. You need to research to what degree it's scriptable.

fhill2
Jun 21, 2012, 02:41 PM
Using System Events with a keystroke command and pbpaste copy all require the Application to be in focus.
Is there a way to target an application and send it keystrokes without interrupting current application focus?

A shell is text based, GUI applications responds to events from the window server, different paradigms. You could pipe or redirect the output of the clipboard in a shell to special files like sockets, device nodes and so on, but that requires that the receiving application has such an interface and accepts input there according to some protocol.

How can I find out which sockets and device nodes my application has?
Thanks

subsonix
Jun 21, 2012, 03:20 PM
How can I find out which sockets and device nodes my application has?
Thanks

It almost certainly doesn't have any, at least no "public" ones and for this purpose. I mentioned it as an example of how you "in theory" can send data between processes using only shell features. Other possibilities are if there exist some Ableton command line tools that let's you do this specifically. But, you best bet is probably to use Applescript as far as I can tell.

fhill2
Jun 21, 2012, 03:39 PM
Okay, thankyou for the help!