Take the 2-minute tour ×

Please consider the following:

I have created a GUI where I can choose the path of a notebook (which I need for later calculation) via

FileNameSetter[Dynamic[path1], "Open", {"Mathematica Notebook" -> {"*.nb"}}]

Now I would like Mathematica to evaluate the chosen notebook immediately after setting the path.

Has anyone an idea?

share|improve this question
add comment

2 Answers

up vote 6 down vote accepted

I suspect there might be an easier way to do this, but you might use a variation of this example:

Button["Find and run",(
    path1 = SystemDialogInput["FileOpen"];
    nb = NotebookOpen[path1, Visible -> False];
    SelectionMove[nb, All, Notebook];


I initially couldn't get NotebookEvaluate[] To work inside a button, which led me to just use the method above, however a quick search and I found this answer by celtschk: How do I make NotebookEvaluate work inside Button? which means you can simplify the above and just have:

Button["Find and run",
    NotebookEvaluate[path1 = SystemDialogInput["FileOpen"]]
,Method -> "Queued"]
share|improve this answer
Good idea to use If[path1=!=$Canceled, do something] to cover situations in which the user presses the cancel button. –  Mike Honeychurch Aug 15 '12 at 0:19
add comment

As it appears that your code will open the notebook, why not simply set all the cells you want to evaluate as initialization cells?

enter image description here

Also check your settings in Format > Option Inspector >

enter image description here

Mathematica graphics

share|improve this answer
I imagine performing these settings may leave you slightly vulnerable. –  Sjoerd C. de Vries Aug 13 '12 at 16:58
@SjoerdC.deVries -- I can see how it could prove inconvenient when one wanted to open the notebook in question to edit or change something, I've had that problem myself. Still, just wondering what vulnerability you see and under what conditions it would arise? –  Jagra Aug 13 '12 at 17:10
Well, not precisely these settings, but it is easy to make this a global setting by mistake. On a Windows system the target for the setting is less conspicuous than what you show here for a Mac (I added it to your answer as an example). And just using your scroll wheel while this field is active may turn its value into something else. Once it's a global setting anyone can execute a one-liner that will delete your whole file system. Come to think of it, this requires an additional setting (GlobalInitializationCellWarning). –  Sjoerd C. de Vries Aug 13 '12 at 17:46
@SjoerdC.deVries -- Good to know. I've always thought that global settings should have some kind of popup dialog box to ask if one feels certain about the changes and maybe a warning along the lines of your concerns. Thanks for improving the answer. –  Jagra Aug 13 '12 at 18:02
add comment

Your Answer


