Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

The notebook expression resulting from ...

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

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:

share|improve this question
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?) – kglr Nov 5 '12 at 0:22
ok I see what you mean. – Mike Honeychurch Nov 5 '12 at 5:24

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];
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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