Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I execute a command in an iTerm window from the command line?

Tags:

iterm

How do I run an iTerm session from the command line, passing in the command which is to be executed in the iTerm window?

The xterm analog is -e, i.e.

xterm -e sleep 10
like image 625
Mark Harrison Avatar asked Sep 20 '15 04:09

Mark Harrison


People also ask

Which command is used to execute the script on the terminal?

-c, –command: This option is used when we want to run a particular command rather than interactive shell and get terminal information in the file given as argument or typescript by default. The script will automatically exit after successful execution.


1 Answers

I had found the official documentation, but hadn't thought to wrap the Applescript up with osascript like SushiHangover- very nice. His answer didn't work for me, probably because I'm using the latest beta 3.0 version, so here's one that does work (and also simplifies a bit).

#!/bin/bash
osascript - "$@" <<EOF
on run argv
tell application "iTerm"
    activate
    set new_term to (create window with default profile)
    tell new_term
        tell the current session
            repeat with arg in argv
               write text arg
            end repeat
        end tell
    end tell
end tell
end run
EOF
like image 105
SamG Avatar answered Oct 11 '22 17:10

SamG