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?


2 Answers 2


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"]
  • $\begingroup$ Good idea to use If[path1=!=$Canceled, do something] to cover situations in which the user presses the cancel button. $\endgroup$ Commented Aug 15, 2012 at 0:19

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

  • 1
    $\begingroup$ I imagine performing these settings may leave you slightly vulnerable. $\endgroup$ Commented Aug 13, 2012 at 16:58
  • $\begingroup$ @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? $\endgroup$
    – Jagra
    Commented Aug 13, 2012 at 17:10
  • 2
    $\begingroup$ 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). $\endgroup$ Commented Aug 13, 2012 at 17:46
  • $\begingroup$ @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. $\endgroup$
    – Jagra
    Commented Aug 13, 2012 at 18:02

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.