The current version of Xcode (version 12.5.1) provides a template for a Document Based App for macOS providing the following document model:
struct MyDocument: FileDocument {
var text: String
init(text: String = "Hello, world!") {
self.text = text
}
static var readableContentTypes: [UTType] { [.exampleText] }
init(configuration: ReadConfiguration) throws {
guard let data = configuration.file.regularFileContents,
let string = String(data: data, encoding: .utf8)
else {
throw CocoaError(.fileReadCorruptFile)
}
text = string
}
func fileWrapper(configuration: WriteConfiguration) throws -> FileWrapper {
let data = text.data(using: .utf8)!
return .init(regularFileWithContents: data)
}
}
I want to add a method to this struct that passes my document to an external program, also saving the document before doing so:
func passMyDocumentToProgram() {
// Save document
// Pass document to external program
}
The problem is I don't know how to save a document like this.
The resulting app (built from the template) provides functionality (in the menu bar) to save a document, so I should be calling this existing functionality somehow.
From my understanding, the fileWrapper
method in MyDocument
returns a FileWrapper
that has a write()
method that can be used to save the document; however, the fileWrapper
method requires a WriteConfiguration
, and I don't how to create this. The documentation for WriteConfiguration
is quite sparse and I have not been able to find anything fruitful online.
Update. The better question is how do I trigger my Document App to auto-save?
I figured out I can save my document with something like FileWrapper(regularFileWithContents: data).write( ... )
, but this is a bad idea because your app will give you an error saying an external program modified the file.
SwiftUI Document Apps written with the FileDocument
protocol auto-save their documents on certain events, like (un)focusing a window, so I'd like to know if there is a way I can trigger such an auto-save programatically.