NSDocumentController
An object that manages an app’s documents.
Declaration
@MainActor class NSDocumentControllerOverview
As the first-responder target of New and Open menu commands, NSDocumentController creates and opens documents and tracks them throughout a session of the app. When opening documents, a document controller runs and manages the modal Open panel. NSDocumentController objects also maintain and manage the mappings of document types, extensions, and NSDocument subclasses as specified in the CFBundleDocumentTypes property loaded from the information property list (Info.plist).
You can use various NSDocumentController methods to get a list of the current documents, get the current document (which is the document whose window is currently key), get documents based on a given filename or window, and find out about a document’s extension, type, display name, and document class.
In some situations, it’s worthwhile to subclass NSDocumentController in non-NSDocument-based apps to get some of its features. For example, the NSDocumentController management of the Open Recent menu is useful in apps that don’t use subclasses of NSDocument.
Topics
Obtaining the Shared Document Controller
Initializing a New NSDocumentController
Creating and Opening Documents
document(for:)duplicateDocument(withContentsOf:copying:displayName:)openDocument(withContentsOf:display:completionHandler:)openUntitledDocumentAndDisplay(_:)makeDocument(for:withContentsOf:ofType:)makeDocument(withContentsOf:ofType:)makeUntitledDocument(ofType:)reopenDocument(for:withContentsOf:display:completionHandler:)
Managing Documents
Managing Document Types
Autosaving
Closing Documents
closeAllDocuments(withDelegate:didCloseAllSelector:contextInfo:)reviewUnsavedDocuments(withAlertTitle:cancellable:delegate:didReviewAllSelector:contextInfo:)
Responding to Action Messages
Managing the Open Dialog
beginOpenPanel(completionHandler:)beginOpenPanel(_:forTypes:completionHandler:)runModalOpenPanel(_:forTypes:)currentDirectoryurlsFromRunningOpenPanel()
Managing the Open Recent Menu
maximumRecentDocumentCountclearRecentDocuments(_:)noteNewRecentDocumentURL(_:)noteNewRecentDocument(_:)recentDocumentURLs