2
$\begingroup$

I am trying to build certain modules in Mathematica 9. I want it to dynamically ask for the directory path ( like file explorer ) when the code cell is executed.

How can I do that ?

(*================================================= DIRECTORY CONTROL \============================================*)

Directory[];
(*current directory*)
SetDirectory[NotebookDirectory[]];(*set to local roaming directory *)

SetDirectory["File_Directory"];
(* set to local roaming SubDirectory *)
$\endgroup$
  • $\begingroup$ @rm-rf -rf Thank you. How can I enable the pop up by default. Shift+Enter should display the folder Browser window. Is there a method to do that ? $\endgroup$ – Rene Duchamp Apr 24 '13 at 21:58
5
$\begingroup$

To choose the directory interactively, use SystemDialogInput, which brings up a native dialog

SystemDialogInput["Directory"]

enter image description here

If successful, it'll return the chosen directory as a string, or return $Canceled if you select "Cancel".

$\endgroup$
  • $\begingroup$ Exactly what I want. $\endgroup$ – Rene Duchamp Apr 24 '13 at 22:04
  • $\begingroup$ Any way to add custom message like : choose the sub folder or choose the subsubfolder ... It may create confusion when more than one such dialog pop up right ... $\endgroup$ – Rene Duchamp Apr 24 '13 at 22:10
  • $\begingroup$ It would be bad UX to have multiple popups appearing at the same time for such a simple (and common) operation. Instead, if you're using something like a Button[] for your interface, consider a descriptive name for the button, such as Button["Choose directory", dir = SystemDialogInput["Directory"]] $\endgroup$ – rm -rf Apr 24 '13 at 22:13
  • $\begingroup$ Ok sounds good. Thanks a lot ! $\endgroup$ – Rene Duchamp Apr 24 '13 at 22:17
1
$\begingroup$

Extending R.M's answer:

Button["Choose directory", dir = SystemDialogInput["Directory"], Method -> "Queued"] 

works without error.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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