8

As near as I can tell, Export cannot be configured through arguments or options, to present a standard system file save dialog to the user, so that he can specify a location and name for his file. Instead, it seems to require that these parameters be specified as arguments. One way around this is to use SystemDialogInput

Export[SystemDialogInput["FileSave"], ...]

which works fine in simple cases, but in more complex cases, such as the ones I need it for, this fails silently. In particular, I can't get it to work with Show:

Manipulate[f = Plot[Sin[x], {x, 0, Pi}], 
 Row[{Button[
    "Export", {Export[SystemDialogInput["FileSave"], Show[f], "PDF"]},
    ImageSize -> 100, Alignment -> {Center, Baseline}]}]]

Strangely, this works fine if, instead of using Export, a file is specified as an argument.

Why doesn't Export + SystemDialogInput work with Show?

orome
  • 12,819
  • 3
  • 52
  • 100

2 Answers2

7

If I use the option Method->"Queued" within Button, this works

    Button["Export", 
      Export[SystemDialogInput["FileSave"], Plot[Cos[x], {x, 0, Pi}], 
      "PDF"], Method -> "Queued"]

Otherwise, "Preemptive" will be the default, making it possible that not enough time is allocated for the Button action to complete. See the reference docs on Button.

david
  • 1,290
  • 1
  • 10
  • 15
3

The problem seems to be a bug in Button that occurs (at least) in Mathematica 8.0.4 running on Apple OS X. Here is what I determined. The following expression fails

Button["Export", 
   Export[SystemDialogInput["FileSave", "untitled.pdf"],
          "******", "PDF"]]

while the following two expression work

Export[SystemDialogInput["FileSave", "untitled.pdf"], 
       Plot[Sin[x], {x, 0, Pi}], "PDF"]

Button["Export", 
   Export["/Users/oldmg/Desktop/test.pdf", "******", "PDF"]]

So I don't think the problem has anything to do with graphics.

Update

Reworking raxacoricofallapatorius' post with david's answer taken into account, I tried

Manipulate[f = Plot[Sin[x], {x, 0, Pi}], 
  Button["Export", Export[SystemDialogInput["FileSave", "untitled.pdf"],
                          f, "PDF"], 
         ImageSize -> 100, Method -> "Queued"]] 

This worked like a charm. Note there is no need to use Row or Show. Also, adding the second argument, "untitled.pdf", to SystemDialogInput makes for a much improved Save dialog. I do think it would be a good idea to localize f by adding {f, ControlType->None} to the Manipulate.

There is still a bug in Export. If you cancel out the Save dialog, you will get an error message that you shouldn't. This may be OS X specific.

Szabolcs
  • 234,956
  • 30
  • 623
  • 1,263
m_goldberg
  • 107,779
  • 16
  • 103
  • 257