Home > Enterprise >  How to handle different document types in a document based macOS app?
How to handle different document types in a document based macOS app?

Time:04-24

How do I handle two different custom document types in one macOS document app?

Starting from the macOS Document App template I define two types, which are also registered in the info.plist :

extension UTType {
    static var test1: UTType {
        UTType(exportedAs: "com.exapmple.test1")
    }
}

extension UTType {
    static var test2: UTType {
        UTType(exportedAs: "com.example.test2")
    }
}

Apple enter image description here

Obviously I would need two different New... menu items for the two doc types. Any ideas how I can achieve this?

CodePudding user response:

It is still NSDocumentController based, so pros&cons are also the same - we have automatic handling for default document type, for everything else - back to coding.

So all you've done is correct, the only left is to add, programmatically, creation of new document of other (non default) type.

Here is main part of approach:

Button("New Document2") {
  let dc = NSDocumentController.shared
  if let newDocument = try? dc.makeUntitledDocument(ofType: "com.example2.plain-text") {
    dc.addDocument(newDocument)
    newDocument.makeWindowControllers()
    newDocument.showWindows()
  }
}

Complete findings and code is here

  • Related