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.

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