How do I automatically evaluate a cell when a notebook is opened?

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"]

-
Have you seen this? – J. M. Jun 22 '12 at 2:58
@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. – R Hall Jun 22 '12 at 3:01
@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. – R Hall Jun 22 '12 at 3:18
@rcollyer yes setting it to true does nothing at all...until you evaluate the cell ;) – R Hall Jun 22 '12 at 3:21
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 3:34

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

-
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. – R Hall 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. – R Hall Jun 23 '12 at 16:38

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.

-
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 Jordan Sep 26 '13 at 9: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