0

I can close google chrome with following bash command:

kill -9 $(ps -x | grep "Google Chrome" | awk "{print $1}")

It closes all instances of google chrome. But I need command that close all the instances of google chrome except first, because music is playing there and I don't want to stop it.

I use word instance to express new google chrome window which opens after I press super N.

  • 1
    This is going to be nearly impossible from the command line as you'll first need to gather the PID from chrome://memory/ and then somehow construct a list of helper processes that belong to tabs except the one you want to exit. An Applescript to close all tabs except for the current tab might be possible, but I've only seen those hooks for safari. – bmike Mar 28 '14 at 21:06
  • I don't want close tabs, but instances. The difference between tabs and instances: to open new tab: super+t, open new instance: super+n – Maxim Yefremov Mar 29 '14 at 4:27
  • You still can't map an instance to a pid easily from the process listing alone. I hope come one explains a way to do this since it would be awesome to script - just saying it might not be feasible with how chrome spawns process on the OS. – bmike Mar 29 '14 at 12:17
  • I don't think that when you open a new window it creates a new process instance. Chrome has several windows, and Chrome has several instances, but this is not a 1 — 1 relationship. – Nicolas Barbulesco Mar 29 '14 at 20:31
4

To close all windows except the frontmost window, run:

osascript -e 'tell app "Google Chrome" to close (windows 2 thru -1)'

To close all windows except the window opened first, run a script like this in AppleScript Editor:

tell application "Google Chrome"
    if number of windows < 2 then return
    set min to id of window 1
    repeat with w in windows 2 thru -1
        if id of w < min then set min to id of w
    end repeat
    close (windows where id is not min)
end tell

"Instances" are usually called windows in OS X, and windows are not separate instances of a process like the instances opened by open -n.

| improve this answer | |
  • 1
    ❯ osascript -e 'tell app "Google Chrome" to close (windows 2 thru -1)' ⏎ 28:53: execution error: Google Chrome got an error: Can’t get windows 2 thru -1. Invalid index. (-1719) – Maxim Yefremov Mar 29 '14 at 11:23
  • 2
    Are there two or more windows open? You could also use osascript -e 'tell app "Google Chrome" to if number of windows >= 2 then close (windows 2 thru -1)'. – Lri Mar 29 '14 at 11:26
  • It closes all the windows except last. How to modify this script to close all the windows except FIRST? And it ignore Google Chrome windows opened by selenium rc. This means: I got a lot of google chrome windows opened by selenium rc, but this apple script don't close them. Just ignore. – Maxim Yefremov Mar 29 '14 at 11:32
  • I think that such nonClosable windows are in separate instances of process, because I cannot switch them with key super + ` – Maxim Yefremov Mar 29 '14 at 11:40
  • This is a much nicer approach. Closing windows is fine. But killing processes is violent, it is calling for dysfunctions. – Nicolas Barbulesco Mar 29 '14 at 22:19

You must log in to answer this question.

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