New tab in iTerm2

Im using version Iterm2 Build 3.0.4 I want to create an alias to open a 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 does not work. Can someone solve the problem with this version (or newer version)?

+4
source share
1 answer

iTerm2 v3 supports significantly improved support for AppleScript, so now you can create tabs directly without sending 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 by clicking โ‡งโŒ˜D), add:

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

To write pwdto a new session created by a section (lower half of a new tab):

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 set newSplit to split horizontally with same profile
        tell newSplit
          select
          write text "pwd"
        end tell    
      end tell
EOF
}

iTMP2 AppleScript Script Editor.app, File > Open Dictionary..., iTerm.app.

ttab CLI, / Terminal.app iTerm2.app ( ).

+4

Source: https://habr.com/ru/post/1649746/


All Articles