How can I send text to a Terminal or iTerm window?

I'm presenting a demo of some multi-user software and would like to create several windows, programatically sending (different) text to each one, something like

send to terminal window 1:  insert into foo values('hello');
send to terminal window 2:  insert into foo values('world');

I'm currently using Terminal and iTerm, but can use any other program.

  • From where (which application) do you want to send the text? Do you want to just display the text or do you want to have it handles as if a user typed it?
    – nohillside
    Mar 11 '16 at 21:12
  • As if a user typed it. Context: imagine a demo with the narration "user 1 adds record foo in window 1, user 2 adds bar in window 2." I'm guessing there's applescript involved, so I'm open to any program (even a shell script with osascript). Mar 11 '16 at 21:19

You can use the Applescript keystroke command.

tell application "iTerm" to activate
delay 0.5
tell application "System Events"
    keystroke "a"
    delay 0.2
    keystroke "b"
    delay 0.2
    keystroke space
    delay 0.2    
    keystroke "c" using control down
end tell

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .