3

If you look in the ArcGIS 10.1 help, the Pythonaddins module SaveDialog shows the following:

SaveDialog({title}, {name_text}, {starting_location})

But if you open the python window in ArcMap, you get a little something extra:

SaveDialog({title}, {name_text}, {starting_location}, {filter}, {filter_label})

When you use the SaveDialog, it doesn't behave very well, and has a dropdown for "Save as type" that I cannot figure out how to access. It would seem that, although the help doesn't contain instructions for doing so, they have built in a means to control the dialog so you can filter it for only certain types.

I've tried many alternatives, but cannot figure out how to get this dialog to let me filter. Does anyone have an idea of how this would be implemented (since it's completely undocumented)?

1

1 Answer 1

4

I thought this got documented. Since it's undocumented, it's not officially supported, so your mileage may vary, not responsible for damage, etc.

filter is a callable that returns a True/False value as to whether the path specified can be saved to or opened (in the case of OpenDialog). filter_label is what shows up in the filter text in the dialog below the filename text box. If you don't specify filter_label, it will call str(filter) and use that.

As an example:

save_folder = os.path.dirname(arcpy.mapping.MapDocument('current').filePath or os.getcwd())
def save_mxd(filename):
    if not filename:
        return False
    if os.path.splitext(filename)[1].lower() == ".mxd":
        return True
    return False

save_path = SaveDialog("Save a map document", "Map document", save_folder, save_mxd, "Mxd Files (*.mxd)")

Or you could be terse and use a lambda:

save_path = SaveDialog("Save", "MXD", "", lambda x: x and x.lower().endswith('.mxd'), "MXDs")

Since the Dialog function just calls whatever it's passed with one argument, you can get fancy and use a class:

class FileGDBSave(object):
    def __str__(self):
        return "New Feature Class in a File GDB"
    def __call__(self, out_path):
        if out_path:
            out_dir = os.path.dirname(out_path)
            extension = os.path.splitext(out_dir)[1].lower()
            # Writing to a File Geodatabase (folder name ends with .gdb)
            if os.path.isdir(out_dir) and extension == ".gdb":
                 # Feature class doesn't exist yet
                 if not arcpy.Exists(out_path):
                     return True
        return False

out_path = pythonaddins.SaveDialog("Save new feature class", 
                                   "Feature class", 
                                   os.path.dirname(
                                       arcpy.mapping.MapDocument("current").filePath), 
                                   FileGDBSave())

Note here the __str__ method is implemented to provide the filter label and the __call__ method acts as what gets passed the path name for determining if the file can be saved at the specified path.

2
  • 1
    (+1) for responding so quickly. My advice to @KentonW is to also log an incident with his local support for a documentation enhancement request to have this documented in the next Service Pack - especially if he plans to rely on this functionality.
    – PolyGeo
    Commented Aug 2, 2013 at 0:11
  • Jason, that lambda function works just fine. Thanks for the assist!
    – Kenton W
    Commented Aug 2, 2013 at 16: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.