尝试在终端中自动执行制表符和 shell 命令?

Trying to automate tabs and shell commands in Terminal?

我有一个自定义 vim 设置 运行 在拆分 (GNU) 屏幕会话中 运行 在终端内的几个选项卡中设置。自然地,我想自动化所有这些。所以我在谷歌上搜索了很多,大多数答案都在谈论使用 osascript -e 到 运行 一堆 AppleScript 命令。我的情况略有不同:首先我使用 TotalTerminal,一个终端插件(不认为这很重要,但以防万一)我写的是 hashbang 脚本而不是 bash 脚本,即

#!/usr/bin/osascript                                                            
tell application "System Events" to tell process "Terminal" to keystroke "t" using command down
tell application "Terminal" to activate                                         
tell application "Terminal" to do script with command "cd ~/Desktop/Projects && screen -d -U -R -A"

我从命令行 运行ning。选项卡打开有效,但 script/command 运行s 在新的 window 中,而不是在新创建的选项卡中。

这就是我建议的设置方式:

#!/usr/bin/osascript

tell application "System Events"
    tell process "Terminal"
        set frontmost to true
    end tell
end tell

tell application "Terminal"
    activate
    tell application "System Events" to keystroke "t" using command down
    do script "cd ~/Desktop/Projects && screen -d -U -R -A" in window frontmost
    do script "clear; echo 'Hello, World!'" in tab 1 of window frontmost
end tell

注意:您也可以使用tab xselect您希望下一个命令进入的选项卡。如果您切换回第一个选项卡,您应该注意到在创建新选项卡后发送给它的回显。

上面的例子可能是多了几行代码,尽管它应该让所有的过程都按顺序正确进行。我认为关键因素是将终端 设置为 true,这让当前终端 window 开始与脚本的其余部分进行交互。

编辑:OP 返回并需要进行一些更改,这是最终结果:

#!/usr/bin/osascript

tell application "System Events"
    tell process "Terminal"
        set frontmost to true
    end tell
end tell

tell application "Terminal"
    activate
    do script "mosh user@someserver" in window frontmost
    tell application "System Events" to keystroke "t" using command down
    do script "cd ~/Desktop/Projects && screen -d -U -R -A" in tab 2 of window frontmost
end tell