Tell me more ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I am looking for a concrete code example where EvaluationNotebook[] and ButtonNotebook[] will return different results.

The docs for ButtonNotebook say:

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

How can a palette button initiate an evaluation in a different notebook?

A basic test where they return the same (the palette):

CreatePalette[
 Button["press", Print@{EvaluationNotebook[], ButtonNotebook[]}]]

I am looking for an example where EvaluationNotebook and ButtonNotebook return different results when invoked from the same button. Of course ButtonNotebook will return $Failed when not invoked from a button.


Addendum

I'd like to point out that this question is not merely of theoretical interest. The fact that ButtonNotebook exists at all suggests that it must be different from EvaluationNotebook in some non-trivial way. Understanding the behaviour of EvaluationNotebook/ButtonNotebook is important to create robust code that will work correctly in edge cases too. I can not see their difference, so there must be some edge case I have not thought of.

share|improve this question
I know this is not your real question, but for starters, ButtonNotebook[] returns $Failed in a fresh notebook and EvaluationNotebook[] doesn't – Rojo Jun 27 '12 at 16:26
There's even an example in the EvaluationNotebook docs of your point where they use EvaluationNotebook to refer to the button notebook: pal = CreatePalette[{Button["Test", NotebookWrite[InputNotebook[], "Done"]; NotebookClose[ EvaluationNotebook[]]]}]; – Rojo Jun 27 '12 at 16:30
@Rojo That's of course the trivial difference. I didn't mention it because I thought it was clear that I'm interested in the case when the code is invoked from a button. – Szabolcs Jun 27 '12 at 16:30
Not everyone may know you're advanced, the first sentence could clarify that last point of yours – Rojo Jun 27 '12 at 16:34

1 Answer

Here's a simple example showing the difference between EvaluationNotebook and ButtonNotebook:

nb = CreateDocument[ExpressionCell[Defer[Print@EvaluationNotebook[];], "Input"]];
CreatePalette[Button["press", 
    SelectionMove[nb, All, CellContents]; SelectionEvaluate[nb]; Print@ButtonNotebook[];]]

Now when you press the button, it prints NotebookObject[x] in the other notebook and NotebookObject[x+1] in the messages window, which corresponds to the button notebook.

The same button invokes both functions, but in different notebooks.

share|improve this answer
I don't think this shows what he really wants to know which is: "assuming ButtonNotebook doesn't $Failed, what's a case where they return something different" – Rojo Jun 27 '12 at 23:24
@Rojo How is this not a case where they return something different? His question was also "How can a palette button initiate an evaluation in a different notebook?", so I was under the impression he was not aware/did not consider this possibility – rm -rf Jun 28 '12 at 0:10
Well, changing ButtonNotebook[] to EvaluationNotebook[] then would prove that EvaluationNotebook[] returns something different from EvaluationNotebook[], that's what I meant – Rojo Jun 28 '12 at 0:36
As to initiating an evaluation in a different notebook, yeah, this surely does it :) – Rojo Jun 28 '12 at 0:37
@Rojo But that doesn't make sense... EvaluationNotebook[] always returns the notebook that it is called from. So if it is called from a button, it will give that notebook object, just as ButtonNotebook would. I guess what the documentation was trying to say was that just because you press the button doesn't mean it always is the selected/input/evaluation notebook and that there are cases where this differs. If you want to access the object of the palette you pressed it from, you should use ButtonNotebook – rm -rf Jun 28 '12 at 1:11
show 4 more comments

Your Answer

 
discard

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.