打开终端和 运行 来自自动化器的命令以打开两个选项卡和 运行 一个命令
Open terminal and run a command from automator to open two tabs and run a command
我想做的是 运行 一个自动化脚本。发生的事情是它打开带有两个选项卡的终端,每个选项卡和 ssh 到 root@192.168.0.1 和 root@ssh@192.168.0.2;你会怎么做?
您可以使用 运行 AppleScript 操作来 运行 脚本,例如:
on run {input, parameters}
tell application "Terminal"
activate
do script "ssh root@192.168.0.1"
do script "ssh root@192.168.0.2"
end tell
return input
end run
终端的 do script
命令创建一个新终端 window 并将给定的命令字符串发送到 shell。请注意,如果您想向同一个终端发送其他命令,请将 do script
命令的结果存储在一个变量中——它将是对已创建终端的引用,您可以将其与 [=13] 一起使用=] do script
命令的参数以向该终端发送更多命令。
补充:
如果您希望两个终端选项卡都在 相同 window 中,事情就变得棘手了:一个长期存在的问题终端 AppleScript API 的局限性是无法以编程方式在 现有 window.
中创建新选项卡
您可以使用 GUI 脚本来解决这个问题;虽然以下处理程序 makeNewTab()
相当健壮,但它需要事先获得一次性授权才能进行辅助访问 - 请参阅下面对处理程序的评论。
请注意,授权通用执行环境(例如 Terminal.app 和 Automator.app 进行辅助访问意味着 any 脚本 运行 他们将拥有这些特权。
如果您希望更好地控制选项卡创建过程,例如分配特定配置文件(外观和行为设置)的能力,请参阅 my answer here,以及此答案底部的应用程序.
(*
Creates a new tab in Terminal's front window and optionally executes a shell command,
if <shellCmdToRun> is a nonempty string.
Note:
* This handler effectively clicks the menu item that creates a new tab and
therefore requires assistive access:
The application running this handler - e.g., Terminal.app, Script Editor.app,
or Automator.app - must be added to the list at
System Preferences > Security & Privacy > Privacy > Accessibility,
using admin credentials.
* This handler activates Terminal first, which is required for it to work.
Caveat:
If there's no front window or if all windows are currently minimized, the
tab is created in a *new* window.
Examples:
my makeNewTab("") # open new tab (without executing a command)
my makeNewTab("ls") # open new tab and execute shell command `ls`
*)
on makeNewTab(shellCmdToRun)
tell application "Terminal"
# Note: If Terminal is not frontmost, clicking the new-tab menu item invariably
# creates the tab in a *new* window.
activate
# Find the File menu by position and click the menu item whose keyboard shortcut is
# ⌘T - this should work with any display language.
tell application "System Events" to ¬
tell menu 1 of menu item 2 of menu 1 of menu bar item 3 of menu bar 1 ¬
of application process "Terminal" to click (the first menu item ¬
whose value of attribute "AXMenuItemCmdChar" is "T" and ¬
value of attribute "AXMenuItemCmdModifiers" is 0)
# If specified, run a shell command in the new tab.
if shellCmdToRun ≠ missing value and shellCmdToRun ≠ "" then
do script shellCmdToRun as text in selected tab of front window
end if
end tell
end makeNewTab
如果您愿意安装 my ttab
CLI,您完全可以不用 AppleScript,而是 运行 来自 Run Shell Script
Automator 操作的以下内容:
# Create tab in new window (-w) and run specified command.
ttab -w ssh root@192.168.0.1
# Create additional tab in same window, with specific settings.
ttab -s Grass ssh root@192.168.0.2
我想做的是 运行 一个自动化脚本。发生的事情是它打开带有两个选项卡的终端,每个选项卡和 ssh 到 root@192.168.0.1 和 root@ssh@192.168.0.2;你会怎么做?
您可以使用 运行 AppleScript 操作来 运行 脚本,例如:
on run {input, parameters}
tell application "Terminal"
activate
do script "ssh root@192.168.0.1"
do script "ssh root@192.168.0.2"
end tell
return input
end run
终端的 do script
命令创建一个新终端 window 并将给定的命令字符串发送到 shell。请注意,如果您想向同一个终端发送其他命令,请将 do script
命令的结果存储在一个变量中——它将是对已创建终端的引用,您可以将其与 [=13] 一起使用=] do script
命令的参数以向该终端发送更多命令。
补充
如果您希望两个终端选项卡都在 相同 window 中,事情就变得棘手了:一个长期存在的问题终端 AppleScript API 的局限性是无法以编程方式在 现有 window.
中创建新选项卡您可以使用 GUI 脚本来解决这个问题;虽然以下处理程序 makeNewTab()
相当健壮,但它需要事先获得一次性授权才能进行辅助访问 - 请参阅下面对处理程序的评论。
请注意,授权通用执行环境(例如 Terminal.app 和 Automator.app 进行辅助访问意味着 any 脚本 运行 他们将拥有这些特权。
如果您希望更好地控制选项卡创建过程,例如分配特定配置文件(外观和行为设置)的能力,请参阅 my answer here,以及此答案底部的应用程序.
(*
Creates a new tab in Terminal's front window and optionally executes a shell command,
if <shellCmdToRun> is a nonempty string.
Note:
* This handler effectively clicks the menu item that creates a new tab and
therefore requires assistive access:
The application running this handler - e.g., Terminal.app, Script Editor.app,
or Automator.app - must be added to the list at
System Preferences > Security & Privacy > Privacy > Accessibility,
using admin credentials.
* This handler activates Terminal first, which is required for it to work.
Caveat:
If there's no front window or if all windows are currently minimized, the
tab is created in a *new* window.
Examples:
my makeNewTab("") # open new tab (without executing a command)
my makeNewTab("ls") # open new tab and execute shell command `ls`
*)
on makeNewTab(shellCmdToRun)
tell application "Terminal"
# Note: If Terminal is not frontmost, clicking the new-tab menu item invariably
# creates the tab in a *new* window.
activate
# Find the File menu by position and click the menu item whose keyboard shortcut is
# ⌘T - this should work with any display language.
tell application "System Events" to ¬
tell menu 1 of menu item 2 of menu 1 of menu bar item 3 of menu bar 1 ¬
of application process "Terminal" to click (the first menu item ¬
whose value of attribute "AXMenuItemCmdChar" is "T" and ¬
value of attribute "AXMenuItemCmdModifiers" is 0)
# If specified, run a shell command in the new tab.
if shellCmdToRun ≠ missing value and shellCmdToRun ≠ "" then
do script shellCmdToRun as text in selected tab of front window
end if
end tell
end makeNewTab
如果您愿意安装 my ttab
CLI,您完全可以不用 AppleScript,而是 运行 来自 Run Shell Script
Automator 操作的以下内容:
# Create tab in new window (-w) and run specified command.
ttab -w ssh root@192.168.0.1
# Create additional tab in same window, with specific settings.
ttab -s Grass ssh root@192.168.0.2