<!--
{
  "availability" : [
    "macOS: -"
  ],
  "documentType" : "symbol",
  "framework" : "AppKit",
  "identifier" : "/documentation/AppKit/NSDocumentController",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "AppKit"
    ],
    "preciseIdentifier" : "c:objc(cs)NSDocumentController"
  },
  "title" : "NSDocumentController"
}
-->

# NSDocumentController

An object that manages an app’s documents.

```
@MainActor class NSDocumentController
```

## Overview

As the first-responder target of New and Open menu commands, [`NSDocumentController`](/documentation/AppKit/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`](/documentation/AppKit/NSDocumentController) objects also maintain and manage the mappings of document types, extensions, and [`NSDocument`](/documentation/AppKit/NSDocument) subclasses as specified in the [CFBundleDocumentTypes](https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html#//apple_ref/doc/uid/20001431-101685) property loaded from the information property list (`Info.plist`).

You can use various [`NSDocumentController`](/documentation/AppKit/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`](/documentation/AppKit/NSDocumentController) in non-[`NSDocument`](/documentation/AppKit/NSDocument)-based apps to get some of its features. For example, the [`NSDocumentController`](/documentation/AppKit/NSDocumentController) management of the Open Recent menu is useful in apps that don’t use subclasses of [`NSDocument`](/documentation/AppKit/NSDocument).

## Topics

### Obtaining the Shared Document Controller

[`sharedDocumentController`](/documentation/AppKit/NSDocumentController/shared)

Returns the shared `NSDocumentController` instance.

### Initializing a New NSDocumentController

[`-  init`](/documentation/AppKit/NSDocumentController/init())

This method is the designated initializer for `NSDocumentController`.

[`-  initWithCoder:`](/documentation/AppKit/NSDocumentController/init(coder:))

This method initializes a new NSDocumentController from the coder.

### Creating and Opening Documents

[`-  documentForURL:`](/documentation/AppKit/NSDocumentController/document(for:)-i5zi)

Returns, for a given URL, the open document whose file or file package is located by the URL, or `nil` if there is no such open document.

[`-  duplicateDocumentWithContentsOfURL:copying:displayName:error:`](/documentation/AppKit/NSDocumentController/duplicateDocument(withContentsOf:copying:displayName:))

Creates a new document by reading the contents for the document from another URL, presents its user interface, and returns the document if successful.

[`-  openDocumentWithContentsOfURL:display:completionHandler:`](/documentation/AppKit/NSDocumentController/openDocument(withContentsOf:display:completionHandler:))

Opens a document located by a URL, optionally presents its user interface, and calls the passed-in completion handler.

[`-  openUntitledDocumentAndDisplay:error:`](/documentation/AppKit/NSDocumentController/openUntitledDocumentAndDisplay(_:))

Creates a new untitled document, presents its user interface if `displayDocument` is `true`, and returns the document if successful.

[`-  makeDocumentForURL:withContentsOfURL:ofType:error:`](/documentation/AppKit/NSDocumentController/makeDocument(for:withContentsOf:ofType:))

Instantiates a document located by a URL, of a specified type, but by reading the contents for the document from another URL, and returns it if successful.

[`-  makeDocumentWithContentsOfURL:ofType:error:`](/documentation/AppKit/NSDocumentController/makeDocument(withContentsOf:ofType:))

Instantiates a document located by a URL, of a specified type, and returns it if successful.

[`-  makeUntitledDocumentOfType:error:`](/documentation/AppKit/NSDocumentController/makeUntitledDocument(ofType:))

Instantiates a new untitled document of the specified type and returns it if successful.

[`-  reopenDocumentForURL:withContentsOfURL:display:completionHandler:`](/documentation/AppKit/NSDocumentController/reopenDocument(for:withContentsOf:display:completionHandler:))

Reopens a document, optionally located by a URL, by reading the contents for the document from another URL, optionally presents its user interface, and calls the passed-in completion handler.

### Managing Documents

[`documents`](/documentation/AppKit/NSDocumentController/documents)

The document objects managed by the receiver.

[`-  addDocument:`](/documentation/AppKit/NSDocumentController/addDocument(_:))

Adds the given document to the list of open documents.

[`currentDocument`](/documentation/AppKit/NSDocumentController/currentDocument)

The document object associated with the main window.

[`-  documentForWindow:`](/documentation/AppKit/NSDocumentController/document(for:)-a5yd)

Returns the document object whose window controller owns a specified window.

[`hasEditedDocuments`](/documentation/AppKit/NSDocumentController/hasEditedDocuments)

A Boolean value indicating whether the receiver has any documents with unsaved changes.

[`-  removeDocument:`](/documentation/AppKit/NSDocumentController/removeDocument(_:))

Removes the given document from the list of open documents.

### Managing Document Types

[`documentClassNames`](/documentation/AppKit/NSDocumentController/documentClassNames)

An array of strings representing the custom document classes supported by this app.

[`defaultType`](/documentation/AppKit/NSDocumentController/defaultType)

Returns the name of the document type that should be used when creating new documents.

[`-  documentClassForType:`](/documentation/AppKit/NSDocumentController/documentClass(forType:))

Returns the `NSDocument` subclass associated with a given document type.

[`-  displayNameForType:`](/documentation/AppKit/NSDocumentController/displayName(forType:))

Returns the descriptive name for the specified document type, which is used in the File Format pop-up menu of the Save As dialog.

[`-  typeForContentsOfURL:error:`](/documentation/AppKit/NSDocumentController/typeForContents(of:))

Returns, for a specified URL, the document type identifier to use when opening the document at that location, if successful.

### Autosaving

[`autosavingDelay`](/documentation/AppKit/NSDocumentController/autosavingDelay)

The time interval (in seconds) for periodic autosaving.

### Closing Documents

[`-  closeAllDocumentsWithDelegate:didCloseAllSelector:contextInfo:`](/documentation/AppKit/NSDocumentController/closeAllDocuments(withDelegate:didCloseAllSelector:contextInfo:))

Iterates through all the open documents and tries to close them one by one using the specified delegate.

[`-  reviewUnsavedDocumentsWithAlertTitle:cancellable:delegate:didReviewAllSelector:contextInfo:`](/documentation/AppKit/NSDocumentController/reviewUnsavedDocuments(withAlertTitle:cancellable:delegate:didReviewAllSelector:contextInfo:))

Displays an alert asking if the user wants to review unsaved documents, quit regardless of unsaved documents, or cancel the save operation.

### Responding to Action Messages

[`-  newDocument:`](/documentation/AppKit/NSDocumentController/newDocument(_:))

An action method called by the New menu command, this method creates a new `NSDocument` object and adds it to the list of such objects managed by the document controller.

[`-  openDocument:`](/documentation/AppKit/NSDocumentController/openDocument(_:))

An action method called by the Open menu command, it runs the modal Open panel and, based on the selected filenames, creates one or more `NSDocument` objects from the contents of the files.

[`-  saveAllDocuments:`](/documentation/AppKit/NSDocumentController/saveAllDocuments(_:))

As the action method called by the Save All command, saves all open documents of the application that need to be saved.

### Managing the Open Dialog

[`-  beginOpenPanelWithCompletionHandler:`](/documentation/AppKit/NSDocumentController/beginOpenPanel(completionHandler:))

Presents an Open dialog and delivers the results to a completion handler as an array of URLs for the chosen files, or nil.

[`-  beginOpenPanel:forTypes:completionHandler:`](/documentation/AppKit/NSDocumentController/beginOpenPanel(_:forTypes:completionHandler:))

Presents a nonmodal Open dialog that displays files you can open from a list of UTIs.

[`-  runModalOpenPanel:forTypes:`](/documentation/AppKit/NSDocumentController/runModalOpenPanel(_:forTypes:))

Presents a modal Open dialog and limits selection to specific file types.

[`currentDirectory`](/documentation/AppKit/NSDocumentController/currentDirectory)

The directory path to use as the starting point in the Open dialog.

[`-  URLsFromRunningOpenPanel`](/documentation/AppKit/NSDocumentController/urlsFromRunningOpenPanel())

An array of URLs that correspond to the selected files in a running Open dialog.

### Managing the Open Recent Menu

[`maximumRecentDocumentCount`](/documentation/AppKit/NSDocumentController/maximumRecentDocumentCount)

The maximum number of items that may be presented in the standard Open Recent menu.

[`-  clearRecentDocuments:`](/documentation/AppKit/NSDocumentController/clearRecentDocuments(_:))

Empties the recent documents list for the application.

[`-  noteNewRecentDocumentURL:`](/documentation/AppKit/NSDocumentController/noteNewRecentDocumentURL(_:))

Adds or replaces an Open Recent menu item corresponding to the data located by the URL.

[`-  noteNewRecentDocument:`](/documentation/AppKit/NSDocumentController/noteNewRecentDocument(_:))

Adds or replaces an Open Recent menu item corresponding to the document.

[`recentDocumentURLs`](/documentation/AppKit/NSDocumentController/recentDocumentURLs)

The list of recent-document URLs.

### Validating User Interface Items

[`-  validateUserInterfaceItem:`](/documentation/AppKit/NSDocumentController/validateUserInterfaceItem(_:))

Returns a Boolean value that indicates whether a given user interface item should be enabled.

### Sharing

[`allowsAutomaticShareMenu`](/documentation/AppKit/NSDocumentController/allowsAutomaticShareMenu)

A Boolean value that the system uses to insert a Share menu in the File menu.

[`-  standardShareMenuItem`](/documentation/AppKit/NSDocumentController/standardShareMenuItem())

Returns a menu item that your app uses for sharing the current document.

### Handling Errors

[`-  presentError:`](/documentation/AppKit/NSDocumentController/presentError(_:))

Presents an error alert to the user as a modal panel.

[`-  presentError:modalForWindow:delegate:didPresentSelector:contextInfo:`](/documentation/AppKit/NSDocumentController/presentError(_:modalFor:delegate:didPresent:contextInfo:))

Presents an error alert to the user as a modal panel.

[`-  willPresentError:`](/documentation/AppKit/NSDocumentController/willPresentError(_:))

Indicates an error condition and provides the opportunity to return the same or a different error.

### Deprecated Methods

[`-  documentForFileName:`](/documentation/AppKit/NSDocumentController/documentForFileName:)

Returns the document object for the file in which the document data is stored.

[`-  fileExtensionsFromType:`](/documentation/AppKit/NSDocumentController/fileExtensionsFromType:)

Returns the allowable file extensions for the given document type.

[`-  fileNamesFromRunningOpenPanel`](/documentation/AppKit/NSDocumentController/fileNamesFromRunningOpenPanel)

Returns a selection of files chosen by the user in the Open panel.

[`-  makeUntitledDocumentOfType:`](/documentation/AppKit/NSDocumentController/makeUntitledDocumentOfType:)

Creates and returns a document object for document type.

[`-  makeDocumentWithContentsOfURL:ofType:`](/documentation/AppKit/NSDocumentController/makeDocumentWithContentsOfURL:ofType:)

Creates and returns a document object for the given document type from the contents of a given URL.

[`-  makeDocumentWithContentsOfFile:ofType:`](/documentation/AppKit/NSDocumentController/makeDocumentWithContentsOfFile:ofType:)

Creates and returns a document object of a given document type from the contents of a file.

[`-  openDocumentWithContentsOfFile:display:`](/documentation/AppKit/NSDocumentController/openDocumentWithContentsOfFile:display:)

Returns a document object created from the contents of a given file and optionally displays it.

[`-  openDocumentWithContentsOfURL:display:`](/documentation/AppKit/NSDocumentController/openDocumentWithContentsOfURL:display:)

Returns a document object created from the contents of a given URL and optionally displays it.

[`-  openDocumentWithContentsOfURL:display:error:`](/documentation/AppKit/NSDocumentController/openDocumentWithContentsOfURL:display:error:)

Opens a document located by the given URL presents its user interface if requested, and returns the document if successful.

[`-  openUntitledDocumentOfType:display:`](/documentation/AppKit/NSDocumentController/openUntitledDocumentOfType:display:)

Returns a document object instantiated from the subclass of the given document type and optionally displays it.

[`-  reopenDocumentForURL:withContentsOfURL:error:`](/documentation/AppKit/NSDocumentController/reopenDocumentForURL:withContentsOfURL:error:)

Reopens an autosaved document located by a URL, by reading the contents for the document from another URL, presents its user interface, and returns `true` if successful.

[`-  setShouldCreateUI:`](/documentation/AppKit/NSDocumentController/setShouldCreateUI:)

Sets whether the window controllers of a document should be created when the document is created.

[`-  shouldCreateUI`](/documentation/AppKit/NSDocumentController/shouldCreateUI)

Returns a Boolean value that indicates whether the window controllers of a document should be created when the document is created.

[`-  typeFromFileExtension:`](/documentation/AppKit/NSDocumentController/typeFromFileExtension:)

Returns the document type associated with files having extension `fileExtensionOrHFSFileType`.

## Relationships

### Conforms To

[`NSMenuItemValidation`](/documentation/AppKit/NSMenuItemValidation)

[`CustomDebugStringConvertible`](/documentation/Swift/CustomDebugStringConvertible)

[`Escapable`](/documentation/Swift/Escapable)

[`CVarArg`](/documentation/Swift/CVarArg)

[`Sendable`](/documentation/Swift/Sendable)

[`NSObjectProtocol`](/documentation/ObjectiveC/NSObjectProtocol)

[`NSUserInterfaceValidations`](/documentation/AppKit/NSUserInterfaceValidations)

[`Copyable`](/documentation/Swift/Copyable)

[`NSWindowRestoration`](/documentation/AppKit/NSWindowRestoration)

[`Hashable`](/documentation/Swift/Hashable)

[`CustomStringConvertible`](/documentation/Swift/CustomStringConvertible)

[`Equatable`](/documentation/Swift/Equatable)

[`NSCoding`](/documentation/Foundation/NSCoding)

### Inherits From

[`NSObject-swift.class`](/documentation/ObjectiveC/NSObject-swift.class)

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)