Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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


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

share|improve this question
up vote 2 down vote accepted

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



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'
share|improve this answer
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 Sep 26 '12 at 14:18
You only want to use osascript for AppleScript commands, not ones that can be executed in the shell (killall python). – adayzdone Sep 26 '12 at 15:10

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
share|improve this answer
The windows list is ordered as they are displayed from front to back, and rest of means all but the first one. – Daniel Beck Sep 26 '12 at 11:49

You must log in to answer this question.

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