Dkova Dkova - 4 months ago 43
Bash Question

new tab in iTerm2

Im using Iterm2 version Build 3.0.4
I want to create alias to open new tab from the command line (in bash)
I tried this code:

function tab () {
osascript &>/dev/null <<EOF
activate application "iTerm"
tell application "System Events" to keystroke "t" using command down
tell application "iTerm" to tell session -1 of current terminal to write text "pwd"
EOF
}


but it isn't working. Can anyone solve the problem with this version (or newer version)?

Answer

iTerm2 features much-improved AppleScript support, so you can now create tabs directly, without having to send keystrokes:

tab() {
    osascript &>/dev/null <<EOF
      tell application "iTerm"
        activate
        tell current window to set tb to create tab with default profile
        tell current session of current window to write text "pwd"  
      end tell
EOF
}

To split the new tab horizontally (as you would get by pressing ⇧⌘D), add:

tell current session of current window to split horizontally with same profile

To browse iTerm2's available AppleScript commands, open Script Editor.app, select File > Open Dictionary..., and then iTerm.app.

Also consider my ttab CLI, which packages tab / window creation along with advanced features for both Terminal.app and iTerm2.app (but it doesn't support splitting a tab).

Comments