1

I have created an Automator App which would open a Terminal window and connect to a server.

tell application "Terminal"
    do script "ssh_connect"
    activate
end tell

Note: the ssh_connect is a shortcut created by me.

However, every time I clicked on the App icon, it creates a new Terminal window and what I want is just to bring the existed window back to front if already created.

What should I do to make this happen?

2
  • Have you tried specifying the window and/or tab that do script runs in?
    – red_menace
    Jul 11, 2021 at 13:50
  • Wouldn't be better in this case to write shell scripts or aliases to continure in the current window?
    – mmmmmm
    Jul 11, 2021 at 17:04

1 Answer 1

1

However, every time I clicked on the App icon, it creates a new Terminal window and what I want is just to bring the existed window back to front if already created.

What should I do to make this happen?

The example AppleScript code, shown below, was tested in Script Editor as a script, and saved as an AppleScript application, under macOS Catalina and macOS Big Sur with Language & Region settings in System Preferences set to English (US) — Primary and worked for me without issue1.

  • 1 Assumes necessary and appropriate settings in System Preferences > Security & Privacy > Privacy have been set/addressed as needed.

Example AppleScript code:

set shellCMD to "ssh_connect"

tell application "Terminal"
    
    activate
    
    if exists (tabs of windows whose history contains shellCMD) then
        set winID to ¬
            the id of the first window ¬
                whose history of tab 1 contains shellCMD
        set index of window id winID to 1
        set frontmost of window id winID to true
    else
        if (exists window 1) and ¬
            (busy of tab 1 of window 1 is false) then
            do script shellCMD in window 1
        else
            do script shellCMD
        end if
    end if
    
end tell

Notes:

The example AppleScript code is just that and is meant to show a way of coding based on given conditions.

Since this is being run as a standalone AppleScript application, as currently coded, what it will do is:

  • If Terminal is not running, it opens Terminal and runs the do script command in the first window.
  • If Terminal is running and a window exists with the value of shellCMD in its history, it brings that window frontmost, even if it's minimized.
  • If Terminal is running and no window exists, then a new window is created and runs the do script command.
  • If Terminal is running and no window exists with the value of shellCMD in its history and window 1 exists and is not currently processing a command, then the do script command is processed in window 1.

You can adjust the example AppleScript code to suit your needs.

This should work for versions of macOS from macOS High Sierra and later.

For versions of macOS prior to macOS High Sierra the example AppleScript code will need to be modified due to how Terminal handles multiple tabs in a window.



Note: The example AppleScript code is just that and sans any included error handling does not contain any additional error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay command may be necessary between events where appropriate, e.g. delay 0.5, with the value of the delay set appropriately.

11
  • Awesome, thanks for the answer. So I could change shellCMD to any string that I want, right? Jul 12, 2021 at 3:55
  • 1
    @AGamePlayer, RE: "So I could change shellCMD to any string that I want, right?" -- Yes, as long as it's a properly formed command I don't foresee an issue. Jul 12, 2021 at 4:13
  • 1
    @AGamePlayer, RE: "Automator-created App" -- Does it have to be an Automator application, why not just save the example AppleScript code, as is, as an AppleScript application from Script Editor? If you want me to troubleshoot an Automator application, then you'll need to provide all the relevant details of the Automator workflow. -- I'll test the example AppleScript code as is in macOS Big Sur as an AppleScript application and see that it functions as described in my answer. Jul 12, 2021 at 4:40
  • 1
    @AGamePlayer, Okay, I've tested this under macOS Big Sur based on what's stated in the answer and it performs as stated in the answer. If it's not doing something that you want, then you'll need to further clarify what you need beyond what you've stated in your original question. Jul 12, 2021 at 4:50
  • 1
    @AGamePlayer, RE: "And for the Automator app, it's because I need an icon and put that icon in the dock so that I can run this on the fly." -- You can do the same thing with an AppleScript application and it is less resource intensive and runs faster than doing it as an Automator application. Jul 12, 2021 at 7:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .