AlanObject AlanObject - 10 months ago 57
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:

osascript <<EOS
tell application "Terminal"
do script "set_background_color salmon; ssh"
end tell

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?


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

Answer Source

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 — 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

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