Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

fhill2

macrumors newbie
Original poster
May 9, 2012
19
0
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

macrumors 68040
Feb 2, 2008
3,551
79
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

macrumors newbie
Original poster
May 9, 2012
19
0
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

macrumors 68040
Feb 2, 2008
3,551
79
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.
 

kryten2

macrumors 65816
Mar 17, 2012
1,114
99
Belgium
How would I send a keystroke to an application from terminal?

Use osascript like :

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

or

Code:
#!/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

macrumors 68040
Feb 2, 2008
3,551
79
If you are going to use nothing but Apple script you may as well just use:

Code:
#!/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

macrumors newbie
Original poster
May 9, 2012
19
0
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

macrumors 68040
Feb 2, 2008
3,551
79
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.
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.