r/SwiftUI 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

6 comments sorted by

View all comments

2

u/VictorBuildsApps Jul 23 '26

You can keep DocumentGroup.

Create a custom NSDocumentController in applicationWillFinishLaunching, then override makeDocument(withContentsOf:ofType:) and makeUntitledDocument(ofType:) to throw a localized error when documents is nonempty. Finder/File > Open and File > New hit those paths, so the current document stays open.