打开终端并从automator运行命令以打开两个选项卡并运行命令

时间:2015-11-13 09:44:38

标签: macos terminal automator

我想要做的是运行一个automator脚本。如果发生了什么,它会打开带有两个选项卡的终端,每个选项卡和ssh到root@192.168.0.1和root @ ssh @ 192.168.0.2;你会怎么做?

2 个答案:

答案 0 :(得分:4)

您可以使用运行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命令创建一个新的终端窗口并将给定的命令字符串发送到shell。请注意,如果要将其他命令发送到同一终端,请将do script命令的结果存储在变量中 - 它将是对已创建的终端的引用,您可以将其与{{1 in命令的参数,用于向该终端发送更多命令。

答案 1 :(得分:2)

补充Chris Page's helpful answer

如果您希望两个终端选项卡都位于相同的窗口中,那么事情就变得棘手:终端AppleScript API的长期限制是无法以编程方式创建现有窗口中的新标签页。

您可以使用GUI脚本解决问题;虽然以下处理程序makeNewTab()相当健壮,但它需要事先一次性授权才能进行辅助访问 - 请参阅下面处理程序的注释。

请注意,授权通用执行环境(如Terminal.app和Automator.app)进行辅助访问意味着由它们运行的​​任何脚本都具有这些权限。

如果您想要更多地控制选项卡创建过程,例如分配特定配置文件(外观和行为设置)的功能,请参阅此答案底部的my answer here及其应用程序。< / p>

(*
  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