What I want to do is run an automator script. Where what happens is it opens terminal with two tabs and each tab and ssh onto [email protected] and root@[email protected]; how would you do this?
To complement Chris Page's helpful answer:
If you want both Terminal tabs to be in the same window, things get tricky: a long-standing limitation of Terminal's AppleScript API is the inability to programmatically create a new tab in an existing window.
You can work around the problem with GUI scripting; while the following handler, makeNewTab()
, is reasonably robust, it requires prior one-time authorization for assistive access - see the comments on the handler below.
Note that authorizing generic execution environments such as Terminal.app and Automator.app for assistive access implies that any script run by them will have these privileges.
If you want more control over the tab creation process, such as the ability to assign a specific profile (appearance and behavior settings), see my answer here, and an application of it at the bottom of this answer.
(*
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
If you're willing to install my ttab
CLI, you can make do without AppleScript altogether, and instead run the following from a Run Shell Script
Automator action:
# Create tab in new window (-w) and run specified command.
ttab -w ssh [email protected]
# Create additional tab in same window, with specific settings.
ttab -s Grass ssh [email protected]
You can use the Run AppleScript action to run a script like:
on run {input, parameters}
tell application "Terminal"
activate
do script "ssh [email protected]"
do script "ssh [email protected]"
end tell
return input
end run
Terminal’s do script
command creates a new terminal window and sends the given command string to the shell. Note that if you want to send additional commands to the same terminal, store the result of the do script
command in a variable—it will be a reference to the terminal that was created and you can use it with the in
parameter of the do script
command to send more commands to that terminal.
To complement Chris Page's helpful answer:
If you want both Terminal tabs to be in the same window, things get tricky: a long-standing limitation of Terminal's AppleScript API is the inability to programmatically create a new tab in an existing window.
You can work around the problem with GUI scripting; while the following handler, makeNewTab()
, is reasonably robust, it requires prior one-time authorization for assistive access - see the comments on the handler below.
Note that authorizing generic execution environments such as Terminal.app and Automator.app for assistive access implies that any script run by them will have these privileges.
If you want more control over the tab creation process, such as the ability to assign a specific profile (appearance and behavior settings), see my answer here, and an application of it at the bottom of this answer.
(*
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
If you're willing to install my ttab
CLI, you can make do without AppleScript altogether, and instead run the following from a Run Shell Script
Automator action:
# Create tab in new window (-w) and run specified command.
ttab -w ssh [email protected]
# Create additional tab in same window, with specific settings.
ttab -s Grass ssh [email protected]
© 2022 - 2024 — McMap. All rights reserved.