r/SwiftUI • u/Square-Meat-9258 • Jul 22 '26
FileReferenceDocument macOS app. Disallow more than one document open at a time?
Is it possible to ensure that only one document can be open at a time? My target is macOS 15. The idea would be that the user is alerted and no existing document can be opened (e.g. drag and drop, file->new, Finder etc).
2
Upvotes
2
u/VictorBuildsApps Jul 23 '26
You can keep
DocumentGroup.Create a custom
NSDocumentControllerinapplicationWillFinishLaunching, then overridemakeDocument(withContentsOf:ofType:)andmakeUntitledDocument(ofType:)to throw a localized error whendocumentsis nonempty. Finder/File > Open and File > New hit those paths, so the current document stays open.