Shell: paste + retrieve contents of clipboard.

Discussion in 'Mac Programming' started by fhill2, Jun 20, 2012.

  1. macrumors newbie

    Joined:
    May 9, 2012
    #1
    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!
     
  2. macrumors 68040

    Joined:
    Feb 2, 2008
    #2
    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.
     
  3. thread starter macrumors newbie

    Joined:
    May 9, 2012
    #3
    What do you mean by input methods? :)
     
  4. macrumors 68040

    Joined:
    Feb 2, 2008
    #4
    The means that the program have to receive data.
     
  5. thread starter macrumors newbie

    Joined:
    May 9, 2012
    #5
    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.
     
  6. macrumors 68040

    Joined:
    Feb 2, 2008
    #6
    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.
     
  7. macrumors 603

    notjustjay

    Joined:
    Sep 19, 2003
    Location:
    Canada, eh?
    #7
    I believe a middle-click would paste clipboard text into a shell.
     
  8. macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #8
    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
     
  9. macrumors 68040

    Joined:
    Feb 2, 2008
    #9
    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.
     
  10. thread starter macrumors newbie

    Joined:
    May 9, 2012
    #10
    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?

    How can I find out which sockets and device nodes my application has?
    Thanks
     
  11. subsonix, Jun 21, 2012
    Last edited: Jun 21, 2012

    macrumors 68040

    Joined:
    Feb 2, 2008
    #11
    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.
     
  12. thread starter macrumors newbie

    Joined:
    May 9, 2012
    #12
    Okay, thankyou for the help!
     

Share This Page