24

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"]
Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
Nothingtoseehere
  • 4,518
  • 2
  • 30
  • 58
  • 1
  • @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. – Nothingtoseehere Jun 22 '12 at 03:01
  • @RHall easy: run the options inspector, select your notebook in the drop down on the left, and search for InitializationCellEvaluation. Now whether or not it works as it is supposed to, is anyone's guess. – rcollyer Jun 22 '12 at 03:11
  • 1
    @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. – Nothingtoseehere Jun 22 '12 at 03:18
  • @RHall setting it to true still makes you to confirm? The docs do say it isn't fully integrated, but that would be annoying. – rcollyer Jun 22 '12 at 03:20
  • 1
    @rcollyer yes setting it to true does nothing at all...until you evaluate the cell ;) – Nothingtoseehere Jun 22 '12 at 03:21
  • Well that's useful. :P – rcollyer Jun 22 '12 at 03:24
  • 3
    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. – rcollyer Jun 22 '12 at 03:34
  • @rcollyer very good point! – Nothingtoseehere Jun 22 '12 at 10:20

2 Answers2

21

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...

Yu-Sung Chang
  • 7,061
  • 1
  • 39
  • 31
  • 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. – Nothingtoseehere Jun 22 '12 at 19:30
  • 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. – Nothingtoseehere Jun 23 '12 at 16:38
17

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.

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
  • 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. – lara Sep 26 '13 at 09:10
  • @Lara Yes, that's correct. I'm glad you found this useful, and you're welcome. – Mr.Wizard Sep 26 '13 at 12:04