Wolfram Research

CloseOtherNotebooks

Contributed by: Richard Hennigan (Wolfram Research)

Source Notebook

Close all other open notebooks

ResourceFunction["CloseOtherNotebooks"][]

close all but the current notebook.

ResourceFunction["CloseOtherNotebooks"][notebooks]

close all notebooks except for those specified by notebooks.

Details and Options

The current notebook is determined by EvaluationNotebook[].
In ResourceFunction["CloseOtherNotebooks"][spec], the notebook specification spec can be an explicit NotebookObject, a pattern, or a list of notebook specifications.
ResourceFunction["CloseOtherNotebooks"] takes the following options:
"Modified" False whether to close notebooks that have unsaved changes

Examples

Basic Examples

Close all but the current notebook:

In[1]:=
CreateWindow[WindowTitle -> "Close Me"];
Notebooks[]
Out[2]=
In[3]:=
ResourceFunction["CloseOtherNotebooks"][]
Out[3]=

Only the current notebook and the messages window remain open:

In[4]:=
Notebooks[]
Out[4]=

Provide a list notebooks that should be kept open:

In[5]:=
CreateWindow[WindowTitle -> "Close Me"];
nb = CreateWindow[WindowTitle -> "Keep Me Open"];
ResourceFunction["CloseOtherNotebooks"][{SelectedNotebook[], nb}]
Out[3]=
In[6]:=
Notebooks[]
Out[6]=
In[7]:=
ResourceFunction["CloseOtherNotebooks"][]
Out[7]=

Use a pattern to choose which notebooks to keep open:

In[8]:=
CreateWindow[WindowTitle -> "Close Me"];
CreateWindow[WindowTitle -> "Keep Me Open"];
ResourceFunction["CloseOtherNotebooks"][
 EvaluationNotebook[] | (nb_ /; CurrentValue[nb, WindowTitle] === "Keep Me Open")]
Out[3]=
In[9]:=
Notebooks[]
Out[9]=
In[10]:=
ResourceFunction["CloseOtherNotebooks"][]
Out[10]=

Options

Modified

By default, notebooks that have been modified in memory will not be closed:

In[11]:=
CreateWindow[WindowTitle -> "Close Me"];
nb = CreateWindow[WindowTitle -> "This one has been modified"];
NotebookWrite[nb, "testing"];
ResourceFunction["CloseOtherNotebooks"][]
Out[9]=
In[12]:=
Notebooks[]
Out[12]=

Using "Modified"True will close these notebooks without saving changes:

In[13]:=
ResourceFunction["CloseOtherNotebooks"]["Modified" -> True]
Out[13]=
In[14]:=
Notebooks[]
Out[14]=

Resource History

See Also