24
$\begingroup$

I'm trying to put together a GUI in Mathematica and need to evaluate one cell to kick that off. Is there a way to automatically evaluate that cell when the notebook is opened? Here is an example of the code I'm using in that cell. Yes I know loading them like:

<<one.nb`..." 

etc. is best, but that doesn't seem to work on my system. Thanks!

SetDirectory[NotebookDirectory[]];
 nbs = {"one.nb", "two.nb", "three.nb", "four.nb", "five.nb", "six.nb", "seven.nb",
        "eight.nb"};
For[i = 1, i <=  Length[nbs], i++,
    Block[{nb = NotebookOpen[NotebookDirectory[] <> nbs[[i]]]},
          FrontEndTokenExecute[nb, "EvaluateNotebook"]
$\endgroup$
9
  • 1
    $\begingroup$ Have you seen this? $\endgroup$ Commented Jun 22, 2012 at 2:58
  • $\begingroup$ @J.M. yes of course, but as you can see the documentation is pretty sparse with no examples of how to implement it. I've tried a few guesses as to how it might work but came up empty handed. $\endgroup$ Commented Jun 22, 2012 at 3:01
  • 1
    $\begingroup$ @rcollyer certainly tried that too and found for initialization cells, you have to evaluate them...then they ask you if you want to evaluate all initialization cells in the nb. Fun but not what I was hoping for. $\endgroup$ Commented Jun 22, 2012 at 3:18
  • 1
    $\begingroup$ @rcollyer yes setting it to true does nothing at all...until you evaluate the cell ;) $\endgroup$ Commented Jun 22, 2012 at 3:21
  • 3
    $\begingroup$ I would like to add one thing. If this is implementable, it represents a security risk, and at worst should require the user to confirm execution prior to the cells being run. Otherwise, it could potentially do anything to the system it is running on. $\endgroup$
    – rcollyer
    Commented Jun 22, 2012 at 3:34

2 Answers 2

21
$\begingroup$

Maybe NotebookDynamicExpression is what you are looking for:

SetOptions[EvaluationNotebook[], NotebookDynamicExpression :> Refresh[
   SetDirectory[NotebookDirectory[]];
   nbs = {"one.nb", "two.nb", "three.nb"};
   For[i = 1, i <= Length[nbs], i++, 
    Block[{nb = NotebookOpen[NotebookDirectory[] <> nbs[[i]]]}, 
     FrontEndTokenExecute[nb, "EvaluateNotebook"]]], None]]

Then you can erase this cell and save the notebook. Upon the opening of the said notebook, it will open and evaluate one.nb, two.nb, and three.nb.

Refresh[..., None] is used to make sure that it is evaluated once.

Note: Besides, it is in fact Dynamic, it will complain at the beginning if the notebook is not in the trusted path...

$\endgroup$
2
  • $\begingroup$ After playing with this nb a bit I see that adding the code above makes the whole nb evaluate each time a cell is even touched. Seemed like it was working. If there was a way to make sure refresh only happened once, I'm sure this would work perfectly. $\endgroup$ Commented Jun 22, 2012 at 19:30
  • $\begingroup$ what actually happens is each time any cell or piece of code is selected or copied the process starts again and reloads the whole nb. Clearing the code from the nb does nothing to reset the options and it continues to evaluate every cell, each time any attempted code change is made. $\endgroup$ Commented Jun 23, 2012 at 16:38
17
$\begingroup$

This is possible, at least in version 7, but as rcollyer supposed there is a Global security option which cannot be set from within the Notebook (automatically, that is).

First change the Global option:

SetOptions[$FrontEnd,
  GlobalInitializationCellWarning -> False
]

Then in the Notebook you need to evaluate:

SetOptions[EvaluationNotebook[],
  InitializationCellEvaluation -> True, 
  InitializationCellWarning -> False
]

Or use the Option Inspector in either case.

$\endgroup$
2
  • $\begingroup$ Thanks this works well. You simply execute these in the notebook then you can delete them. The cells marked as initialisation cells then evaluate when the notebook opens without any warning messages. $\endgroup$
    – lara
    Commented Sep 26, 2013 at 9:10
  • $\begingroup$ @Lara Yes, that's correct. I'm glad you found this useful, and you're welcome. $\endgroup$
    – Mr.Wizard
    Commented Sep 26, 2013 at 12:04

Your Answer

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

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