Thanks for being a part of WWDC25!

How did we do? We’d love to know your thoughts on this year’s conference. Take the survey here

Force NSDocument save panel to select most specific type in format popup button

My app supports different plain text file formats, including the standard .txt and Markdown. When creating a new document, my app already asks which format it should have, so when saving it, I would expect that the save panel already selects that format in the popup button, but currently it always selects "Plain Text". For example, I would expect for a Markdown document that it selects "Markdown" instead of "Plain Text".

Is there a way to force it to select the most specific format matching the document format?

Answered by DTS Engineer in 832425022

Can you provide more information about the APIs you are using and how you are using them. Specifically, I think what you're doing with the allowedContentTypes and currentContentType NSSavePanel properties are the first things I'd like to look at.

Can you provide more information about the APIs you are using and how you are using them. Specifically, I think what you're doing with the allowedContentTypes and currentContentType NSSavePanel properties are the first things I'd like to look at.

I tried playing with those properties, but what I described above already happens without using any of them, and even when using them I wasn‘t able to find a combination that worked.

Can you provide a code listing showing how you are using these APIs? I'd like to try to reproduce what you are seeing there.

For instance take the following code:

class AppDelegate: NSObject, NSApplicationDelegate {

    func applicationDidFinishLaunching(_ aNotification: Notification) {
        DispatchQueue.main.async {
            let document = try! NSDocumentController.shared.makeUntitledDocument(ofType: "net.daringfireball.markdown")
            print(document.fileType!, UTType(document.fileType!)!)
            NSDocumentController.shared.addDocument(document)
            document.makeWindowControllers()
            document.windowControllers[0].window!.title = "Markdown"
            document.showWindows()
        }
    }

}

And Info.plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>UTImportedTypeDeclarations</key>
	<array>
		<dict>
			<key>UTTypeConformsTo</key>
			<array>
				<string>public.plain-text</string>
			</array>
			<key>UTTypeDescription</key>
			<string>Markdown</string>
			<key>UTTypeIcons</key>
			<dict/>
			<key>UTTypeIdentifier</key>
			<string>net.daringfireball.markdown</string>
			<key>UTTypeTagSpecification</key>
			<dict>
				<key>public.filename-extension</key>
				<array>
					<string>md</string>
					<string>mdown</string>
					<string>markdown</string>
					<string>text</string>
				</array>
				<key>public.mime-type</key>
				<array>
					<string>text/markdown</string>
					<string>text/x-markdown</string>
					<string>text/x-web-markdown</string>
				</array>
			</dict>
		</dict>
	</array>
	<key>CFBundleDocumentTypes</key>
	<array>
		<dict>
			<key>CFBundleTypeName</key>
			<string>Plain Text</string>
			<key>CFBundleTypeRole</key>
			<string>Editor</string>
			<key>LSHandlerRank</key>
			<string>Default</string>
			<key>LSItemContentTypes</key>
			<array>
				<string>public.plain-text</string>
			</array>
			<key>NSDocumentClass</key>
			<string>$(PRODUCT_MODULE_NAME).Document</string>
			<key>NSUbiquitousDocumentUserActivityType</key>
			<string>org.desairem.uFocus.OpenDocument</string>
		</dict>
		<dict>
			<key>CFBundleTypeName</key>
			<string>Markdown</string>
			<key>CFBundleTypeRole</key>
			<string>Editor</string>
			<key>LSHandlerRank</key>
			<string>Default</string>
			<key>LSItemContentTypes</key>
			<array>
				<string>net.daringfireball.markdown</string>
			</array>
			<key>NSDocumentClass</key>
			<string>$(PRODUCT_MODULE_NAME).Document</string>
			<key>NSUbiquitousDocumentUserActivityType</key>
			<string>org.desairem.uFocus.OpenDocument</string>
		</dict>
	</array>
</dict>
</plist>

After making sure that the document with the window title Markdown is active, press Command-S to open the default save panel: Plain Text is selected instead of Markdown.

Changing the order of the file formats in the Info.plist file is reflected in the save panel popup button, and putting Markdown before Plain Text solves this particular issue.

But it seems that regardless which file format is selected in the save panel, the produced file always gets the .md extension.

Force NSDocument save panel to select most specific type in format popup button
 
 
Q