The accepted answer from nonresident alien can be simplified, the DocumentGroup closure needs a binding to the document to initialize the ContentView with, so declare a func on the document that grabs the source URL & returns the config, which can then supply the document binding:
struct FileOpenDocument: FileDocument {
var sourceURL: URL?
mutating func setSourceURL(config: FileDocumentConfiguration<FileOpenDocument>) -> FileDocumentConfiguration<FileOpenDocument> {
sourceURL = config.fileURL
return config
}
}
The DocumentGroup initializer then becomes:
@main
struct FileOpenApp: App {
var body: some Scene {
DocumentGroup(newDocument: FileOpenDocument()) { file in
ContentView(document: file.document.setSourceURL(file).$document)
}
}
}
No modifications to ContentView necessary.