如何打开新的终端选项卡并将焦点返回到上一个选项卡?

How to open new Terminal tab and get focus back to the prev tab?

我想 运行 终端选项卡中的一个命令,它将打开一个新选项卡,运行 那里也有一个命令,然后将焦点返回到上一个终端选项卡(我运行第一个命令),可以吗? 使用 corse osascript。

我目前拥有的:

osascript -e 'tell application "Terminal" to activate' -e 'tell application "System Events" to tell process "Terminal" to keystroke "t" using command down'
osascript -e 'tell application "Terminal"' -e 'do script "cd /path/to/dir; clear;" in selected tab of the front window' -e 'end tell' > /dev/null
osascript -e 'tell application "System Events" to tell process "Terminal" to key code "48" using control down' > /dev/null

这只有在我只有 2 个打开的标签页时才有效。

osascript -e 'tell application "Terminal" to set selected of tab 1 of front window to true'

1 替换为适当的标签编号 select 您喜欢的任何标签。

或者您可以保存当前标签号:

osascript \
    -e 'tell application "Terminal" to set oldTab to the selected tab of the front window' \
    -e 'tell application "System Events" to tell process "Terminal" to keystroke "t" using command down' \
    -e 'delay 1' \
    -e 'tell application "Terminal" to set selected of oldTab to true'