5
$\begingroup$

I have 10 different notebook files which I want to combine into a single .nb file. I know I can copy-paste all the cells of those 10 files to a new notebook window and then save it as nb. I can do so by using Insert > File too. But these two methods just copy the input-output and don't run any codes.

But I want all the codes written in those files to get executed in the new notebook one by one (I should be able to decide which among the 10 files would be executed first)

I have tried different methods but they just execute the files in different windows and don't create any new file.

I've seen this page: https://reference.wolfram.com/workbench/index.jsp?topic=/com.wolfram.eclipse.help/html/tasks/importing/notebook.html

But I couldn't find any such tool like import wizard in wolfram mathematica 9.0. Can anyone tell me how to import codes from different notebook files.

I've also seen manipulating notebooks documentation of wolfram mathematica but that didn't help either.

Note: I'd prefer simple command that can be written in mathematica for achieving this task.

$\endgroup$
3
  • $\begingroup$ You might want Get or to make your 10 different notebooks packages, rather than actually inserting their text into your working notebook. $\endgroup$
    – evanb
    Commented Jul 10, 2021 at 9:39
  • $\begingroup$ @evanb it doesn't work like that..Instead of showing the cells in input-output format. It writes all data as text like Notebook[{Cell[Textdata[StyleBox[.........CellGroupData[Boxdata.. This is not what I wanted. $\endgroup$ Commented Jul 10, 2021 at 12:02
  • $\begingroup$ Welcome to Mathematica.SE! I hope you will become a regular contributor. To get started, 1) take the introductory tour now, 2) when you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge, 3) remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign, and 4) give help too, by answering questions in your areas of expertise. $\endgroup$
    – bbgodfrey
    Commented Jul 10, 2021 at 15:07

1 Answer 1

8
$\begingroup$

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 enter image description here

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]
]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.