Overriding NSDocument.prepareSavePanel(_:) hides file format popup button

I would like to provide a default filename when saving a document depending on the document data. I thought I could do so by overriding NSDocument.prepareSavePanel(_:) and setting NSSavePanel.nameFieldStringValue, but simply implementing that method seems to hide the file format popup button shown by default (see image). Calling super doesn't help.

Is it possible to set a default filename and keep the file format popup button? On macOS 15, I can toggle NSSavePanel.showsContentTypes, but how about macOS 14 and older?

Answered by DTS Engineer in 836921022

Have you enabled the allowsOtherFileTypes option for the NSSavePanel? Reviewing the NSDocument sources I can see those sources are aware of that option and gate some operations based on its setting.

Have you enabled the allowsOtherFileTypes option for the NSSavePanel? Reviewing the NSDocument sources I can see those sources are aware of that option and gate some operations based on its setting.

allowsOtherFileTypes alone makes the file format popup button appear, but then setting accessoryView makes it disappear again. Is it possible to have both?

override func prepareSavePanel(_ savePanel: NSSavePanel) -> Bool {
    savePanel.allowsOtherFileTypes = true
    savePanel.accessoryView = NSTextField(labelWithString: "asdf")
    return true
}

Ah, I see. Since your app allows multiple file types you'll also need to override shouldRunSavePanelWithAccessoryView to allow the popup to be displayed.

I just tried, but it doesn't change anything. The documentation also states that it already returns true by default.

For me, the file format popup was displayed when I had an override of prepareSavePanel(_:), and when I set the nameFieldStringValue. However, if I set the accessoryView then the format popup was hidden. (The logic is that in the past developers would implement their own format popup in the accessory view.)

So if you set the accessoryView, the fix is to also set showsContentTypes = true. That will tell the save panel to display the format popup.

Overriding NSDocument.prepareSavePanel(_:) hides file format popup button
 
 
Q