MathGroup Archive 2006

[Date Index] [Thread Index] [Author Index]

Search the Archive

RE: Initialization cell and button

  • To: mathgroup at
  • Subject: [mg70091] RE: [mg70067] Initialization cell and button
  • From: "Ingolf Dahl" <ingolf.dahl at>
  • Date: Tue, 3 Oct 2006 06:16:33 -0400 (EDT)
  • Reply-to: <ingolf.dahl at>

You must remember that the FrontEnd is almost, but not exactly, as smart as
the bomb in the computer game "Starship Titanic" by Douglas Adams... When
the "EvaluateInitialization" command is executed, the initialization cells
are not inserted into the program flow and evaluated immediately; they just
are put onto the evaluation stack, to be evaluated after the button function
as such. You thus must put the print command even later on the evaluation
stack to obtain the desired functionality. The easiest way is of course to
include a print command as the last initialization cell, but that solution
is probably too simple for your purposes (?). Another solution is to hide a
print command inside the button cell. Execute the following command to
obtain such a button:

NotebookWrite[InputNotebook[], Cell[BoxData[InterpretationBox[ButtonBox["a",
FrontEndToken["EvaluateCells"]]], ButtonEvaluator->None, Active->True,
ButtonFrame->"DialogBox"], Print@a]],"Input"]]

(You have to modify the button if you want to generate a palette from it.) 

Another approach to put commands onto the evaluation stack is to let the
ButtonFunction create a scrap notebook, write the commands there together
with a command that closes the same scrap notebook, and then use
SelectionEvaluate to evaluate these commands. But in this case we then would
have to redirect the output of Print to be able to see anything.

Best regards

Ingolf Dahl

> -----Original Message-----
> From: Markus Huber [mailto:mhla at] 
To: mathgroup at
> Subject: [mg70091] [mg70067] Initialization cell and button
> I've got the following simplified problem with a button:
> Create an initialization cell which contains a=1;
> Create a button with
> Button["a", Active -> True,
>      ButtonFunction :> (FrontEndExecute[
>            FrontEndToken["EvaluateInitialization"]];
>          Print@a), ButtonEvaluator -> Automatic] // DisplayForm
> but say no, if Mathematica asks if to evaluate the 
> initialization cells in the NB.
> Now press the button .
> In my opinion the result should be the printout of 1. Instead 
> M prints a. If you press the button again it prints correctly 1.
> Why is M not aware of the value of a when evaluating the 
> Print function although the initialization cells should have 
> been evaluated already?
> Regards,
> Markus Huber

  • Prev by Date: xvnc Mathematica menu fonts
  • Next by Date: variance of product of random variables
  • Previous by thread: Initialization cell and button
  • Next by thread: Re: Initialization cell and button