# How to grab output from evaluating input cells in a generated notebook

The notebook expression resulting from ...

nb = CreateDocument[ExpressionCell[Defer@Random[], "Input"]];
FrontEndTokenExecute[nb, "EvaluateNotebook"];
NotebookGet@nb


does not include the output of Random[] as I might have expected from the FrontEndTokenExecute command. Instead NotebookGet@nb acts as if FrontEndTokenExecute was not evaluated despite its execution in the frontend?

I guess this is due to the separation/communication between the frontend and the kernel but can output from evaluations in a generated notebook be grabbed in a single kernel call?

See answer below:

-
I do get a new notebook with a random number in it... –  Sjoerd C. de Vries Nov 4 '12 at 22:52
Ron I get the output as well. OS X 10.6.8 with V8.0.4. –  Mike Honeychurch Nov 4 '12 at 23:42
@Sjoerd & Mike The random number appears in the generated notebook itself but on my machine the expression NotebookGet@nb returns instead Notebook[{Cell[BoxData[RowBox[{"Random", "[", "]"}]], "Input"]}, WindowSize -> {740, 752}, WindowMargins -> {{350, Automatic}, {Automatic, 50}}, FrontEndVersion -> "8.0 for Mac OS X x86 (32-bit, 64-bit Kernel) (October 5, 2011)", StyleDefinitions -> "Default.nb"]? –  Ronald Monson Nov 4 '12 at 23:51
If you execute NotebookGet@nb in separate cell you get you expected output (perhaps confirming your guess?) –  kguler Nov 5 '12 at 0:22
ok I see what you mean. –  Mike Honeychurch Nov 5 '12 at 5:24

## 1 Answer

It turns out that instead of FrontEndTokenExecute[nb, "EvaluateNotebook"] the seemingly equivalent NotebookEvaluate[nb, InsertResults -> True] does exhibit the expected behaviour which is good as this function seems neater anyway (and has a few more options).

nb = CreateDocument[ExpressionCell[Defer@Random[], "Input"]];
NotebookEvaluate[nb, InsertResults -> True];
NotebookGet@nb

-