B Seven
B Seven

Reputation: 45943

How to send a command to a given iTerm 2 tab?

What is a good way to send a command to a given tab in another window in iTerm 2? I gather that AppleScript may be a good way, but it should be run from within another iTerm window.

I did find a way to broadcast to all tabs, but that is a different feature. Also found a way to open a new tab and send the command, but that is a different feature as well.

Upvotes: 1

Views: 1834

Answers (1)

SushiHangover
SushiHangover

Reputation: 74164

Item2 has zero or more windows, each window has one or more tabs, and each tab has one or more sessions (i.e. split tabs).

So the question for you is:

Does the session that you need to send a command have something unique about it that you can perform an equality on to determine if it is the one you need to send to?

Example:

  • Send a git pull to a session named "Github"
  • Request all sessions that are not processing something echo their unique id

.

tell application "iTerm"
    repeat with aWindow in windows
        tell aWindow
            repeat with aTab in tabs
                tell aTab
                    repeat with aSession in sessions
                        tell aSession
                            if (name = "GitHub") then
                                write text "git pull"
                            end if
                            if (is at shell prompt) then
                                set uniqueID to "echo StackoverFlow: " & id
                                write text uniqueID
                            end if
                        end tell
                    end repeat
                end tell
            end repeat
        end tell
    end repeat
end tell

session Dictionary

properties
id (text, r/o) : The unique identifier of the session.
is processing (boolean) : The session has received output recently.
is at shell prompt (boolean) : The terminal is at the shell prompt. Requires shell integration.
columns (integer)
rows (integer)
tty (text, r/o)
contents (text) : The currently visible contents of the session.
text (text, r/o) : The currently visible contents of the session.
background color (RGB color)
bold color (RGB color)
cursor color (RGB color)
cursor text color (RGB color)
foreground color (RGB color)
selected text color (RGB color)
selection color (RGB color)
ANSI black color (RGB color)
ANSI red color (RGB color)
ANSI green color (RGB color)
ANSI yellow color (RGB color)
ANSI blue color (RGB color)
ANSI magenta color (RGB color)
ANSI cyan color (RGB color)
ANSI white color (RGB color)
ANSI bright black color (RGB color)
ANSI bright red color (RGB color)
ANSI bright green color (RGB color)
ANSI bright yellow color (RGB color)
ANSI bright blue color (RGB color)
ANSI bright magenta color (RGB color)
ANSI bright cyan color (RGB color)
ANSI bright white color (RGB color)
background image (text)
name (text)
transparency (real)
unique ID (text, r/o)
profile name (text, r/o) : The session's profile name
answerback string (text) : ENQ Answerback string

Upvotes: 5

Related Questions