'Can I have two different DocumentGroup elements for different document types in a SwiftUI app?

According to the documentation,

Your app can support multiple document types by adding additional document group scenes

So I have two different document types (a bare document and a bundle of such documents with metadata) that are editable, so I have two DocumentGroup elements:

    var body: some Scene {
    DocumentGroup {
        BundledDocument()
    } editor: { file in
        BundleView(document: file.document)
    }
    .commands {
        ...Commands...
    }

    DocumentGroup {
        BareDocument()
    } editor: { file in
        BareDocumentView(document: file.document)
    }

    WindowGroup("otherWindow") {
        OtherWindow()
    }

The problem is that the File menu has a New submenu, with only one New Document entry, and it only creates the first document type listed (I've tried both orders).

So, is there a way to allow the user to specify one document type or the other to create a new document?

I tried adding

.handlesExternalEvents(matching: Set(arrayLiteral: "newDoc"))

to the second DocumentGroup and adding a menu command to use openURL with the "newDoc" URL, but it then opens a new window of the "otherWindow" type. If I then add another similar line to that WindowGroup, then the menu command doesn't open either type of window.

The second DocumentGroup does apparently work for opening a file of the correct type, so it is of some use. However, it looks to me as though I cannot make it produce a new document this way.

The only other alternatives I've come across are to create the window myself, or to create a new document in a temporary file and then feed the URL for that file. Neither is particularly attractive, but possible, I suppose.

Update: I implemented the option of creating a temporary file and then using NSWorkspace.shared.open to open it, which works, but leaves a file probably not where the user wants it, as the app is sandboxed, and it ends up in the container. But it works, and I can't do better at this stage of SwiftUI's development, it seems.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source