The Documentation for ButtonNotebook says:

  1. ButtonNotebook[] gives the notebook, if any, that contains the button which initiated the current evaluation.

  2. If a button in a palette initiates evaluation in another notebook, then ButtonNotebook[] will be the palette, but EvaluationNotebook[] will be the other notebook.

  3. If the current evaluation was not initiated by a button, then ButtonNotebook[] will return $Failed.

These statements raise several questions. The first is:

1. What is "a button"?

Obviously it isn't only Button because Dynamic[ButtonNotebook[]] doesn't display $Failed:



The same is true when ButtonNotebook[] is evaluated as a part of CellDynamicExpression:

CellPrint@ExpressionCell[Dynamic@bnb, "Output", 
  CellDynamicExpression :> (bnb = ButtonNotebook[])]


... or as a part of Initialization in DynamicModule:

DynamicModule[{bnb}, {Dynamic[bnb]}, Initialization :> (bnb = ButtonNotebook[])]


So, what is assumed under "button" exactly?

2. What is "evaluation"?

Consider the following example from the Documentation page for Button:

Button["Front End", SelectionMove[ButtonNotebook[], All, ButtonCell], Evaluator -> None]


From this example it is clear that evaluation may happen in the FrontEnd only, i.e. without any interaction with the Kernel, and ButtonNotebook[] will correctly refer to the corresponding Notebook. At least this point looks clearly documented.

3. When ButtonNotebook[] and EvaluationNotebook[] are different?

The statement number 2 cited at the beginning of this question "describes" a situation when ButtonNotebook[] and EvaluationNotebook[] are different, but no examples are given on the corresponding Documentation pages. Let us try to construct an example according to the description:

Button["Evaluate new notebook",
 nb = CreateDocument[ExpressionCell[Defer@Pause[1], "Input"]]; 
 SelectionMove[nb, All, CellContents]; SelectionEvaluate[nb];
 Print[{nb, ButtonNotebook[], EvaluationNotebook[]}]]


After clicking the button the following gets printed:


Both ButtonNotebook[] and EvaluationNotebook[] return the same NotebookObject. But may be we simply haven't fulfilled the requirement that the button should be "in a palette"? But what means "a palette"? The tutorial "Creating Palettes" suggests CreatePalette as the primary tool to create them, so let's try:

ourNotebook = EvaluationNotebook[];
CreatePalette@Button["Evaluate new notebook",
  nb = CreateDocument[ExpressionCell[Defer@Pause[1], "Input"]]; 
  SelectionMove[nb, All, CellContents]; SelectionEvaluate[nb];
  NotebookWrite[ourNotebook, ToBoxes@{nb, ButtonNotebook[], EvaluationNotebook[]}]]


Again, both ButtonNotebook[] and EvaluationNotebook[] refer to the palette Notebook and not to the Notebook where the button initiated evaluation (which is still running when ButtonNotebook[] and EvaluationNotebook[] are evaluated themselves).

So the main question remains completely unclear: in which situations ButtonNotebook[] and EvaluationNotebook[] return different NotebookObjects?

  • 1
    $\begingroup$ Relevant: mathematica.stackexchange.com/q/7523/12 $\endgroup$
    – Szabolcs
    May 10, 2017 at 15:09
  • $\begingroup$ I would trust John Fultz's answer more than the documentation ... $\endgroup$
    – Szabolcs
    May 10, 2017 at 15:34
  • $\begingroup$ So the main question is a duplicate, and the answer is never, they are the same, right? Only 2. remains. $\endgroup$
    – Kuba
    May 10, 2017 at 15:38
  • $\begingroup$ @Kuba I think 1. also remains because potentially we can use ButtonNotebook[] for testing whether we are in a "button" or not. $\endgroup$ May 10, 2017 at 15:42
  • $\begingroup$ SelectedNotebook[] and ButtonNotebook[] are not equivalent here : { Button["SelectedNotebook Red",SetOptions[SelectedNotebook[],Background-> RGBColor[1,0,0]]], Button["ButtonNotebook Red",SetOptions[ButtonNotebook[],Background-> RGBColor[1,0,0]]], Button["Reset to White",SetOptions[SelectedNotebook[],Background-> RGBColor[1,1,1]];SetOptions[ButtonNotebook[],Background-> RGBColor[1,1,1]]] } //CreatePalette. $\endgroup$
    – andre314
    May 10, 2017 at 20:01


Your Answer

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