Question

I have an application that can load in third party code. One of the capabilities that the third party code can do is add formats in which the app can export to. I am using saveDocumentTo: as means for implementing export.

I understand that I can customize the menu of available filetypes to save in via overriding writableTypesForSaveOperation: for my document, but what doesn't work is that in the save dialog an appropriate file extension isn't added to the filename when selected from the menu.

I tried overriding fileNameExtensionForType:saveOperation: but that doesn't even get called.

How can I make the Save dialog find the correct file extension (provided it isn't known at compile time)?

Was it helpful?

Solution

I've done this within a custom export accessory view for the Save Panel. The custom export accessory view just changes the NSSavePanel's allowed file types whenever the user changes the format they want to export to.

If you want to set the extension, pass an array with one element containing that extension.

The docs have some important detail for -[NSSavePanel setAllowedFileTypes:]'s behavior in this regard, for supporting more complex cases:

Discussion

A file type can be a common file extension, or a UTI. A nil value indicates that any file type can be used. The default value is nil.

If no extension is given by the user, the first item in the allowedFileTypes will be used as the extension for the save panel. If the user specifies a type not in the array, and allowsOtherFileTypes is YES, they will be presented with another dialog when prompted to save.

NSOpenPanel: In versions of Mac OS X less than v10.6, this property is ignored. For applications that link against v10.6 and higher, this property determines which files should be enabled in the open panel. Using the deprecated methods to show the open panel (the ones that take a types: parameter) will overwrite this value, and should not be used. The allowed file types can be changed while the panel is running (for example, from an accessory view). The file type can be a common file extension, or a UTI. This is also known as the “enabled file types.” A nil value indicates that all files should be enabled.

You may also see dedicated export dialogs in some cases which can reduce the complexity of this if you have several distinct formats. As before, you just update the allowed file types to support this (not necessarily dynamically in this case).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top