0
$\begingroup$

I have a small function that, when run, opens up a little dialog window. One of the buttons, if clicked, opens up a file browser and lets you select a file. You can click that same button again and select another file, which it then adds to a list in the dialog window. Then, if you click the other button, it closes the dialog window and returns the name of the file(s) you selected.

Here is the code for that:

sdi = {};
res = DialogInput[
  DialogNotebook[{TextCell["Current files:"], 
    Dynamic@TextCell[Column@sdi], 
    Button["Choose file", 
     AppendTo[sdi, SystemDialogInput["FileOpen"]]], 
    Button["Finished", DialogReturn[sdi]}]];

This morning, it was working fine, while I was using MMa 10.0. Then I decided to finally upgrade to 10.3, which was the newest version that was immediately available to me. Now, if I run that same code and click the "choose file" button, I get this error:

SystemDialogInput::nprmtv: SystemDialogInput is not currently supported within preemptive evaluations.

So I Googled that error and everyone says that the solution is to put Method->"Queued" in the button, which I apparently should have been doing all along.

So now I do this:

sdi = {};
res = DialogInput[
  DialogNotebook[{TextCell["Current files:"], 
    Dynamic@TextCell[Column@sdi], 
    Button["Choose file", 
     AppendTo[sdi, SystemDialogInput["FileOpen"]], 
     Method -> "Queued"], 
    Button["Finished", DialogReturn[sdi], Method -> "Queued"]}]];

Now, when I run it, the dialog window pops up, but pressing either of the buttons does nothing! The "choose file" one doesn't open the file browser, and the "finished" one doesn't close the window.

However, if I just do:

Button["Choose file", AppendTo[sdi, SystemDialogInput["FileOpen"]], 
 Method -> "Queued"]

And click the button, the browser does pop up and I can select something.

But why isn't the Queued option working?

Edit:

As m_goldberg suggested, I don't really need cascading dialogs. The way I was doing it before, I was just doing what worked at the time. So, he suggested this code, which produces a panel that does what I need:

sdi = {}; Panel[
 Column[{"Current files:", Dynamic@Column@sdi, 
   Button["Choose file", AppendTo[sdi, SystemDialogInput["FileOpen"]],
     Method -> "Queued", ImageSize -> All]}]];

Running that just by itself in a notebook works perfectly. However, now I need to be a little more specific. I was using the code I initially posted (the thing that worked before the upgrade) inside of a function. Another function would call this function, let me choose info from the dialog box, then return it to the calling function, which would process it.

So, something like this:

ChooseFilesFn[] := (
  sdi = {}; 
  Panel[Column[{"Current files:", Dynamic@Column@sdi, 
     Button["Choose file", 
      AppendTo[sdi, SystemDialogInput["FileOpen"]], 
      Method -> "Queued", ImageSize -> All]}]];
  Return@DeleteCases[sdi, $Canceled];
  )

MyFn1[] := (
  info = ChooseFilesFn[];
  (*Import files, process data, etc.*)
  )

If I just call ChooseFilesFn[], only a empty pair of brackets shows up (the Return value of sdi, which starts empty), and no panel. My next thought was to try adding a Print@ in front of the panel. If I do that and run it, the panel with my button shows up, but the bracket is right after it, implying that the code just ran past the panel.

How can I make it so it doesn't proceed until I've chosen something with the button? My really naive way of doing it would be to put the panel in a loop and loop until sdi isn't empty anymore. But I'm guessing there's a much smarter way of doing that...

$\endgroup$
  • 1
    $\begingroup$ I am surprised that you were able to do this in an earlier version. To me it seems counter intuitive that it could ever work. According to the docs: "The Wolfram Language kernel in which DialogInput is evaluated will block until the DialogReturn in the dialog is evaluated." Therefore if the kernel is blocked how could the SystemDialogInput function work when it needs the "Queued" evaluation mode due to the lengthy time required? $\endgroup$ – Mike Honeychurch Mar 19 '16 at 4:58
  • $\begingroup$ Or to put it another way the buttons within a DialogInput need to be preemptive but that is incompatible with SystemDialogInput, e.g. Button["Choose file", SystemDialogInput["FileOpen"], Method -> "Preemptive"] $\endgroup$ – Mike Honeychurch Mar 19 '16 at 5:03
  • $\begingroup$ @MikeHoneychurch, honestly, I've read through most of the documentation I could find for dialog stuff in Mathematica and I still don't have even an okay grasp of it. Maybe I'm just a fool, but I didn't find their documentation very helpful for teaching general skill with it. It had a lot of examples (which I appreciate!) but wasn't explained well conceptually IMO. For example, I don't really understand what "...is evaluated will block until..." means. What I can figure out is that the conflict comes from which part of the function currently has "focus", but that's about all. $\endgroup$ – YungHummmma Mar 19 '16 at 21:35
2
$\begingroup$

As Mike Honeychurch points out, you really can't have cascading dialogs in Mathematica. However, do you really need them? You might find this simple panel will work for you.

sdi = {};

Panel[
  Column[{
    "Current files:",
    Dynamic @ Column @ sdi,
    Button["Choose file",
      AppendTo[sdi, SystemDialogInput["FileOpen"]],
      Method -> "Queued",
      ImageSize -> All]}]]

panel

$\endgroup$
  • $\begingroup$ Thank you for the comment. That looks pretty good, and does what I need it to when I just run it in a notebook. But the way I need to use it is in a function, where when I call the function, it does the dialog thing, lets me do that, and then returns the information to the function that called it. When I try inserting your code into that function, it doesn't seem to work. I've edited my question to reflect that. I hope it's just a simple syntax thing I'm missing! $\endgroup$ – YungHummmma Mar 19 '16 at 21:39

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.