Close all Mac Terminal windows, but the one running a script

2

1

I am trying to create a shell script that runs a python simulation programing in 4 terminal windows. I have the script that launches the program four times in four separate terminal windows(total of 5 windows, 4 for the python programs, and one to control the other terminal windows). I want to now create a script that I can run in the control terminal window that closes and kills the programs of the other four terminal windows, but still have the control one open.

What I have so far is something like this

#!/bin/sh

osascript -e 'tell app "Terminal" 
do script "killall python"
end tell'
osascript -e 'tell app "Terminal" to quit'
osascript -e 'tell app "Terminal" to open'

The problem is that the last line doesn't work because it closes all the windows including the one the script is executing in. I am not really familiar with shell or apple script so any help would be welcomed. I posted on Stack, but I think this might be a better place for an automation type question. Thanks

Greg Brown

Posted 2012-09-25T15:25:47.993

Reputation: 133

Answers

2

You can assign the id of the "Main" window to a variable and then at the end of the script, close all windows whose id does not match that of the "Main" window. The AppleScript portion can be along these lines:

tell application "Terminal"
    set mainID to id of front window
    -- insert your code
    close (every window whose id ≠ mainID)
end tell

EDIT

#!/bin/sh

killall python

osascript -e 'tell application "Terminal"' -e 'set mainID to id of front window' -e 'close (every window whose id ≠ mainID)' -e 'end tell'

adayzdone

Posted 2012-09-25T15:25:47.993

Reputation: 592

I am a total noobie when it comes to apple script and sh scripting can you edit the answer to function code, and for the "insert your code" part I would just killall python then have the rest close all windows – Greg Brown – 2012-09-26T14:18:25.743

You only want to use osascript for AppleScript commands, not ones that can be executed in the shell (killall python). – adayzdone – 2012-09-26T15:10:10.900

1

If the window running the script is frontmost and Terminal doesn't ask for confirmation when closing the other windows, you could try something like this.

tell application "Terminal"
    close rest of (get windows)
end tell

Lri

Posted 2012-09-25T15:25:47.993

Reputation: 34 501

The windows list is ordered as they are displayed from front to back, and rest of means all but the first one. – Daniel Beck – 2012-09-26T11:49:37.217