简体   繁体   中英

Allow freeform extension for document types in NSSavePanel

I have a document-based application with 3 document types (Type A, Type B, Type C) configured as editor in the info.plist. How can I make it so that Type A will allow the user to set whatever file name they want when saving, without forcing any particular extension, while still being able to choose from the 3 different formats?

What I've tried:

  1. Setting the NSSavePanel's allowsOtherFileTypes to true in my NSDocument's prepareSavePanel() function. This isn't sufficient because it only works if the user enters an extension that isn't currently known by the system (eg “My document.foo” will still save as “My Document.foo.typea”).
  2. Not setting a type identifier on the document type in the info.plist. This actually worked reasonably well up until macOS 13, where it logs an error about a UTI that wasn't exported and won't offer that type in the save panel.
  3. Setting a blank extension on the exported type in the info.plist. This almost works but if the user enters a known extension it shows a prompt like this:

You have used the extension “.txt” at the end of the name. The standard extension is “.”.

  1. Not setting any extension on the exported type in the info.plist. This seemed to work great on macOS 13, but on macOS 12 it would show a prompt similar to the above but with “.(null)” rather than “.”.

So, without completely overriding the entire save process, I'm now out of ideas.

I had another idea which seems to be working on both macOS 12 and 13:

Set the type identifier of the document type to public.data in the info.plist.

On macOS 12 this logs a warning about not having a valid extension, but it doesn't show any confusing prompts to the user. I'm still open to other solutions though, if anyone has any.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM