How to make Automator open only one instance of the Terminal Application?
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?
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 andwindow 1
exists and is not currently processing a command, then thedo script
command is processed inwindow 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.