Home > database >  mac unix: Get name of terminal I am using through commands
mac unix: Get name of terminal I am using through commands

Time:11-11

I am looking for a terminal command that will open a new tab in the current terminal that I am using. For example, I run the command in iTerm 2 and a new tab opens in iTerm2 because that is the terminal that i am using.

I know how to open a tab by naming a static application like so:

osascript -e 'tell application "iTerm" to activate' \
     -e 'tell application "System Events" to tell process "iTerm" to keystroke "t" using command down'

But of course this still opens iTerm if I am doing something like calling the command from mac's Terminal. I would love to do something like below, but I am unsure how to get the name of the terminal I am using.

currentTerminal = getCurrentTerminalApplicationName()

osascript -e 'tell application "&{currentTerminal}" to activate' \
         -e 'tell application "System Events" to tell process "&{currentTerminal}" to keystroke "t" using command down'

I am also open to other ideas.

CodePudding user response:

While inside macOS terminal:

env | grep TERM
TERM_PROGRAM=Apple_Terminal

While inside iTerm2:

env | grep TERM
TERM_PROGRAM=iTerm.app

UPDATE

You can always do something like this:

#!/bin/bash

if [ "$TERM_PROGRAM" == "iTerm.app" ]; then
  echo "iTerm2"

cat <<EOF >/tmp/file.py
import iterm2

async def main(connection):
    app = await iterm2.async_get_app(connection)
    window = app.current_terminal_window
    if window is not None:
        await window.async_create_tab()
    else:
        print("No current window")

iterm2.run_until_complete(main)
EOF

$HOME/Library/Application\ Support/iTerm2/iterm2env-3.10/versions/3.10.4/bin/python /tmp/file.py

else
  echo "not iTerm2"
fi

This code will open new tab inside iTerm2 - otherwise, it will do nothing.

  • Related