2
$\begingroup$

I wanna Open, Evaluate and Close notebooks sequentially.

Code from question here

nb1 = NotebookOpen["/Users/you/Documents/1.nb"];
SelectionMove[nb1, All, Notebook]
SelectionEvaluate[nb1]
nb2 = NotebookOpen["/Users/you/Documents/2.nb"];
SelectionMove[nb2, All, Notebook]
SelectionEvaluate[nb2]

nearly works, but opens notebooks simultaneously at first.

How to do it so notebook1 Opens, Evaluates and Closes, and then notebook2 does the same, notebook 3 and so on...?

$\endgroup$
4
$\begingroup$

You could use a function to monitor the evaluation of the notebook evaluation, as described here: https://stackoverflow.com/a/8054270/879601

Note, this requires use of a second kernel in case the monitoring kernel clashes with the executing kernel.

NotebookPauseForEvaluation[nb_] := Module[{},
  While[NotebookEvaluatingQ[nb], Pause[.25]]]

NotebookEvaluatingQ[nb_] := Module[{},
  SelectionMove[nb, All, Notebook];
  Or @@ Map["Evaluating" /. # &, Developer`CellInformation[nb]]]

UsingFrontEnd[nb1 = NotebookOpen["/Users/you/Documents/1.nb"];
  SetOptions[nb1, Evaluator -> "Test"];
  SelectionMove[nb1, All, Notebook];
  SelectionEvaluate[nb1];
  NotebookPauseForEvaluation[nb1];
 ];

nb2 = NotebookOpen["/Users/you/Documents/2.nb"];
SelectionMove[nb2, All, Notebook]
SelectionEvaluate[nb2]
| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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