我有以下 Apple 脚本可以在不同的终端中执行三个命令。是否可以 (1) 合并此脚本以提高效率,以及 (2) 在不同的终端选项卡而不是不同的窗口中打开?
#!/usr/bin/osascript
# Terminal 1
tell application "Terminal"
activate
do script "
cd folder1 &&
command1"
end tell
# Terminal 2
tell application "Terminal"
activate
do script "
cd folder2 &&
command2"
end tell
# Terminal 3
tell application "Terminal"
activate
do script "
cd folder2 &&
command2"
end tell
答案1
activate
您只需发送键盘命令来打开新选项卡,然后将脚本指向此新选项卡即可。一旦它位于最前面,您就不需要每次都这样做。
这里我已经替换了echo hello
,因此可以安全地进行测试。
tell application "Terminal"
activate
tell application "System Events" to keystroke "t" using {command down}
do script "echo hello" in selected tab of the front window
tell application "System Events" to keystroke "t" using {command down}
do script "echo hello" in selected tab of the front window
tell application "System Events" to keystroke "t" using {command down}
do script "echo hello" in selected tab of the front window
end tell
您可以运行一个循环,但当每个新脚本都不同时,这似乎不值得 [& 我们实际上不知道它会做什么]。这不包含错误检查。您可能需要添加延迟或存在性检查以确保它顺利运行。