AlanObject AlanObject - 2 months ago 12
AppleScript Question

How to avoid a zombie window when running MacOS shel/Applescript script

I routinely open a lot of Terminal windows each with an ssh session. To streamline the process I have a series of shell scripts that look like this:

#!/bin/sh
osascript <<EOS
tell application "Terminal"
activate
do script "set_background_color salmon; ssh alan@demo.znyx.com"
end tell
EOS


This works. I can either execute the script from a shell prompt or click it in the Finder window. (To do this the file name suffix is .command instead of .sh)

The problem is that when I execute it from Finder, I end up with a dead window ("Process Completed") behind the window I intended to open. Is there any way to get rid of it or not have it open in the first place?

UPDATE:

The solution in this other question results in dialog boxes appearing that provide confirm/review/cancel options. This is undesirable.

Answer

When you double-click a .command file, the file is passed to Terminal and Terminal creates a window and executes the commands within it. The commands in your .command file use AppleScript to tell Terminal to run a command in yet another window.

Why not remove the middle man and just put the ultimate commands you want to run — set_background_color salmon; ssh alan@demo.znyx.com — in the .command file? Drop that stuff with running an AppleScript. In other words, the contents of your .command file should just be:

set_background_color salmon
ssh alan@demo.znyx.com

Alternatively, you could take the AppleScript from your .command file (the part between <<EOS and EOS), put it into Script Editor.app, and save it as an applet.