通过脚本用zsh和iterm2打开2个屏幕

问题描述

我希望有一个脚本,该脚本能够通过脚本打开1个iterm选项卡,其中包含2个拆分屏幕。

是否可以,或者应该为此安装一个额外的库?

解决方法

是的!无需任何外部库,这是可能的。

使用osascript将命令“发送”到Iterm这个简单的脚本应打开一个新标签页,将其拆分并调用一些命令;

#!/bin/bash

osascript<<EOF
    tell application "iTerm"
        activate
        select first window

        # Create new tab
        tell current window
            create tab with default profile
        end tell

        # Split pane
        tell current session of current window
            split vertically with default profile
        end tell

        # Run command in Pane-1
        tell first session of current tab of current window
            write text "cd /tmp"
            write text "pwd"
        end tell

        # Run command in Pane-2
        tell second session of current tab of current window
            write text "echo Second tab!;"
        end tell
    end tell
EOF

Screen recording of script (.gif)