6

I would like to export data to a file using a SaveAs type dialog, using something like FileNameSetter[name,"Save"] to give a browse button.

Any help with this would be greatly appreciated. It is very possible that I have simply misunderstood the documentation.

Many thanks, Christina

Christina
  • 199
  • 9

1 Answers1

8

SystemDialogInput["FileSave"] will do this for you.

Something like

fileName = SystemDialogInput["FileSave"]
If[fileName != $Canceled, Export[fileName, myData]]

should do the whole trick. The fire extension determines the type of export (if MMA knows it and supports it).

Preselecting a directory path and filtering allowable file extensions can be done as follows:

SystemDialogInput["FileSave", 
                  {
                    FileNameJoin[{NotebookDirectory[],"*.*"}], 
                    {
                      "JPEG" -> {"*.jpg", "*.jpeg"}, 
                      "Portable Network Graphics" -> {"*.PNG"}
                    }
                  }
]
Sjoerd C. de Vries
  • 65,815
  • 14
  • 188
  • 323
  • Thank you. I found that Save[SystemDialogInput["FileSave"],data] allows me to save data to a file. – Christina Jun 07 '12 at 21:12
  • @Christina Save is fine if you just want to save some Mathematica symbols to be read in again by Mathematica. If you want to save data in a more structured and exchangeable format Export is usually the first choice. – Sjoerd C. de Vries Jun 07 '12 at 21:29
  • Thank you for all the help. It works perfectly now. – Christina Jun 11 '12 at 13:41
  • Thank you for this answer. It has helped me a lot. I found that I needed to change "If[fileName != $Canceled, Export[fileName, myData]]" to "If[UnsameQ[fileName, $Canceled], Export[fileName, myData]]" otherwise in case the user rejected the file name I suggested and entered another valid filename, Mma was unable to evaluate the 'If" condition. – Simon Nov 10 '17 at 21:42
  • 1
    @Simon Glad to be of help. UnsameQ (=!=) is indeed the better choice in many inequalities. – Sjoerd C. de Vries Nov 11 '17 at 15:29