Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open terminal and run a command from automator to open two tabs and run a command

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?

like image 979
Jake McAllister Avatar asked Nov 13 '15 09:11

Jake McAllister


People also ask

How to write commands to a terminal from another program?

In order to write commands to a terminal from another program or terminal you must use a system input-output control system call (ioctl). (This may not always be the case but is is the solution I have found). I will also be presenting a solution in Python but I have cited other resources including a method in c below.

How do I execute a command line command after -- option?

# Use “-- ” to terminate the options and put the command line to execute after it. Developers seem to forget that this is the only way to do what you want, i.e., automatically set up different working environments in tabs. The "new" syntax, that adds commands to run at the end of the options after -- does not allow to do that.

How to launch multiple commands at the same time in Linux?

Also each of those three lines should start with the command mate-terminal, and if you want them all to launch all at once, you need not semicolons at the end of each line, but single ampersands. That will put the first two commands in the background, allowing the shell to launch the other commands in parallel.


2 Answers

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.

like image 170
Chris Page Avatar answered Nov 03 '22 00:11

Chris Page


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] 
like image 40
mklement0 Avatar answered Nov 03 '22 00:11

mklement0