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
    Commented Mar 28, 2014 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 Commented Mar 29, 2014 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
    Commented Mar 29, 2014 at 12:17
  • 1
    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. Commented Mar 29, 2014 at 20:31

3 Answers 3


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.

  • 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) Commented Mar 29, 2014 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
    Commented Mar 29, 2014 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. Commented Mar 29, 2014 at 11:32
  • I think that such nonClosable windows are in separate instances of process, because I cannot switch them with key super + ` Commented Mar 29, 2014 at 11:40
  • This is a much nicer approach. Closing windows is fine. But killing processes is violent, it is calling for dysfunctions. Commented Mar 29, 2014 at 22:19

I arrived here trying to work out how to close all chrome instances from terminal on macOS. Here's what worked for me:

pkill -9 "Google Chrome"

you need set the number 100 as the count you need ( total app instance - 1 )

repeat 100 times
    -- get list of open apps
    tell application "System Events"
    set allApps to displayed name of (every process) as list
    end tell

    -- leave some apps open 
    set exclusions to {"Google Chrome"}

    -- quit each app
    repeat with thisApp in allApps
    set thisApp to thisApp as text
    if thisApp is in exclusions then
        tell application thisApp to quit
    end if
    end repeat
end repeat

in task.scpt

usage in terminal:

osascript /Users/task.scpt

  • How can the number (100) be determined from AppleScript (or any other programmatic way), or even in general?
    – nohillside
    Commented May 12, 2022 at 7:54
  • I have not got it yet. I will try
    – dengST30
    Commented May 12, 2022 at 8:13

You must log in to answer this question.

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