The following function will collect all currently open and unsaved notebooks and place their content in a new notebook. The contents from each unsaved notebook are grouped by a "Section" cell bearing the name of the notebook it came from. Any existing "Section" cells are downgraded to "Subsection" to make the result more tidy.
I use this function, originally written by Carl Woll, almost every workday and thought it might be made to work for OP's use case. My workflow with Mathematica is often to open a new notebook very regularly. I'll be prototyping a function and open up a new notebook and copy code over and fix some aspect of it, or I'm reading some file and print out lines in one notebook and write the string-processing function in another.
At the end I find myself with tons of open notebooks and I just want to save it all to disk and close the program. So I run
gatherNotebooks[]
and up pops this notebook
If it all looks correct I save it and run
closeOtherNotebooks[]
and everything is clear.
It seems to me this could be adapted for OP's use case quite easily. If you want to gather all the open notebooks and then evaluate them, it's easy to just select the closed cell group and evaluate it. You could also extend gatherNotebooks
to take an explicit list of NotebookObject
or a list of file names.
gatherNotebooks[] := Module[
{nbs = Notebooks[]},
nbs = Replace[
SortBy[nbs, ("WindowTitle" /. NotebookInformation[#])&],
{bef___, this:EvaluationNotebook[], after___} :> {bef, after, this}
];
nbs = Cases[nbs, nb_?unsavedNotebookQ :> nbToCollapsedCell[nb]];
NotebookPut @ Notebook @ nbs
]
$notebooksToIgnore = "Messages" | "DocuTools" (*or whatever palettes you don't want to gather*)
unsavedNotebookQ[nb_NotebookObject] :=
MatchQ[{"ModifiedInMemory", "DocumentType", "WindowTitle"} /.
NotebookInformation[nb], {True, "Notebook", Except[$notebooksToIgnore]}]
cellTypeRules = {
("Title" | "Section") -> "Subsection",
"Subsection"->"Subsubsection",
"Subsubsection"->"Subsubsubsection"
}
downgradeCellTypes = ReplaceAll[
Cell[pre___,style:(Alternatives @@ Keys[cellTypeRules]), post___] :>
Cell[pre, Replace[style, cellTypeRules], post]
];
nbToCollapsedCell[nb_]:= Cell @ CellGroupData[
{Sequence[
Cell["WindowTitle" /. NotebookInformation[nb], "Section"],
Sequence @@ downgradeCellTypes[First[NotebookGet[nb]]]
]},
"Closed"
]
closeOtherNotebooks[] := With[
{nbs = Cases[Notebooks[], Except[EvaluationNotebook[]]]},
Scan[NotebookClose, nbs]
]
Get
or to make your 10 different notebooks packages, rather than actually inserting their text into your working notebook. $\endgroup$