Class

NSDocument

An abstract class that defines the interface for macOS documents.

Declaration

@interface NSDocument : NSObject

Overview

A document is an object that can internally represent data displayed in a window and that can read data from and write data to a file or file package. Documents create and manage one or more window controllers and are in turn managed by a document controller. Documents respond to first-responder action messages to save, revert, and print their data.

Conceptually, a document is a container for a body of information identified by a name under which it is stored in a disk file. In this sense, however, the document is not the same as the file but is an object in memory that owns and manages the document data. In the context of AppKit, a document is an instance of a custom NSDocument subclass that knows how to represent internally, in one or more formats, persistent data that is displayed in windows.

A document can read that data from a file and write it to a file. It is also the first-responder target for many menu commands related to documents, such as Save, Revert, and Print. A document manages its window’s edited status and is set up to perform undo and redo operations. When a window is closing, the document is asked before the window delegate to approve the closing.

NSDocument is one of the triad of AppKit classes that establish an architectural basis for document-based apps (the others being NSDocumentController and NSWindowController).

For more information about using NSDocument in a document-based app, see Developing a Document-Based App.

Subclassing NSDocument

The NSDocument class is designed to be subclassed. That is, the NSDocument class is abstract, and your app must create at least one NSDocument subclass in order to use the document architecture. To create a useful NSDocument subclass, you must override some methods, and you can optionally override others.

The NSDocument class itself knows how to handle document data as undifferentiated lumps; although it understands that these lumps are typed, it knows nothing about particular types. In their overrides of the data-based reading and writing methods, subclasses must add the knowledge of particular types and how data of the document’s native type is structured internally. Subclasses are also responsible for the creation of the window controllers that manage document windows and for the implementation of undo and redo. The NSDocument class takes care of much of the rest, including generally managing the state of the document.

See Document-Based App Programming Guide for Mac for more information about creating subclasses of NSDocument, particularly the list of primitive methods that subclasses must override and those that you can optionally override.

Document Saving Behavior

The NSDocument class implements document saving in a way that preserves, when possible, various attributes of each document, including:

  • Creation date

  • Permissions/privileges

  • Location of the document’s icon in its parent folder’s Icon View Finder window

  • Value of the document’s Show Extension setting

Care is also taken to save documents in a way that does not break any user-created aliases that may point to documents. As a result, some methods in any class of NSDocument may be invoked with parameters that do not have the same meaning as they did in early releases of macOS. It is important that overrides of writeToURL:ofType:error: and writeToURL:ofType:forSaveOperation:originalContentsURL:error: make no assumptions about the file paths passed as parameters, including:

  • The location to which the file is being written. This location might be a hidden temporary directory.

  • The name of the file being written. It is possible that this file has no obvious relation to the document name.

  • The relation of any file being passed, including the original file, to the value in fileURL.

When updating your app to link against OS X v10.5, keep in mind that it is usually more appropriate to invoke in your app code one of the NSDocument save... methods than one of the write... methods. The write... methods are there primarily for you to override. The saveToURL:ofType:forSaveOperation:error: method that is meant always to be invoked during document saving, sets the fileModificationDate property with the file’s new modification date after it has been written (for NSSaveOperation and NSSaveAsOperation only).

Likewise, it’s usually more appropriate to invoke in your app code one of the NSDocument revert... methods than one of the read... methods. The read... methods are there primarily for you to override. The revertToContentsOfURL:ofType:error: method that is meant always to be invoked during rereading of an open document, sets the fileModificationDate property with the file’s modification date after it has been read.

iCloud Support

The NSDocument class implements the file coordination support that is required for an iCloud-enabled, document-based Mac app (see How iCloud Document Storage Works in iCloud Design Guide). In addition, this class’s methods for moving and renaming documents, new in OS X v10.8, ensure that these operations are performed in a safe manner for iCloud-enabled apps.

Multicore Considerations

In macOS 10.6 and later, NSDocument supports the ability to open multiple documents concurrently. However, this support requires the cooperation of the document object. If your document subclass is able to read specific document types independently of other similar documents, you should override the canConcurrentlyReadDocumentsOfType: class method and return YES for the appropriate document types. If specific document types rely on shared state information, however, you should return NO for those types.

Topics

Creating a Document Object

- init

Initializes and returns an empty document object.

- initWithContentsOfURL:ofType:error:

Initializes a document located by a URL of a specified type.

- initForURL:withContentsOfURL:ofType:error:

Initializes a document with the specified contents, and places the resulting document's file at the designated location.

- initWithType:error:

Initializes a document of a specified type.

Reading the Document's Content

+ canConcurrentlyReadDocumentsOfType:

Returns a Boolean value that indicates whether the receiver reads multiple documents of the given type concurrently.

- readFromURL:ofType:error:

Sets the contents of this document by reading from a file or file package, of a specified type, located by a URL.

- readFromFileWrapper:ofType:error:

Sets the contents of this document by reading from a file wrapper of a specified type.

- readFromData:ofType:error:

Sets the contents of this document by reading from data of a specified type.

Writing the Document's Content

- canAsynchronouslyWriteToURL:ofType:forSaveOperation:

Returns whether the receiver can concurrently write to a file or file package located by a URL, that is formatted for a specific type, for a specific kind of save operation.

- unblockUserInteraction

Unblocks the main thread during asynchronous saving.

- writeToURL:ofType:error:

Writes the contents of the document to a file or file package located by a URL, that is formatted to a specified type.

- writeSafelyToURL:ofType:forSaveOperation:error:

Writes the contents of the document to a file or file package located by a URL.

- fileWrapperOfType:error:

Creates and returns a file wrapper that contains the contents of the document, formatted to the specified type.

- dataOfType:error:

Creates and returns a data object that contains the contents of the document, formatted to a specified type.

- writeToURL:ofType:forSaveOperation:originalContentsURL:error:

Writes the contents of the document to a file or file package located by a URL.

- saveToURL:ofType:forSaveOperation:delegate:didSaveSelector:contextInfo:

Saves the contents of the document to a file or file package located by a URL, that is formatted to a specified type, for a particular kind of save operation.

- saveToURL:ofType:forSaveOperation:completionHandler:

Saves the contents of the document to a file or file package located by a URL, that is formatted to a specified type, for a particular kind of save operation, and invokes the passed-in completion handler.

- fileAttributesToWriteToURL:ofType:forSaveOperation:originalContentsURL:error:

Returns the attributes to write to the file or file package at the specified URL, and targeting the specified type of save operation.

NSSaveOperationType

Constants for specifying the type of document-save operation to perform.

Getting Document Metadata

fileURL

The location of the document’s on-disk representation.

entireFileLoaded

A Boolean value that indicates whether the document’s file is completely loaded into memory.

fileModificationDate

The last-known modification date of the document’s on-disk representation.

keepBackupFile

A Boolean value that indicates whether the document archives previously saved versions of the document.

draft

A Boolean value that indicates whether the document is a draft that the user has not yet saved.

fileType

The name of the document type, as specified in the app’s information property-list file.

documentEdited

A Boolean value that indicates whether the document has unsaved changes.

inViewingMode

A Boolean value that indicates whether the document is in read-only mode.

Managing File Type Information

readableTypes

Returns the types of data the receiver can read natively and any types filterable to that native type.

writableTypes

Returns the types of data the receiver can write natively and any types filterable to that native type.

+ isNativeType:

Returns a Boolean value that indicates whether the document can read and write the data natively.

- writableTypesForSaveOperation:

Returns the names of the types to which this document can be saved for a specified kind of save operation.

- fileNameExtensionForType:saveOperation:

Returns a filename extension that can be appended to a base filename, for a specified file type and kind of save operation.

Creating and Managing Window Controllers

- makeWindowControllers

Creates the window controller objects that the document uses to display its content.

- addWindowController:

Adds the specified window controller to the current document.

- removeWindowController:

Removes the specified window controller from the receiver’s array of window controllers.

windowControllers

The document’s current window controllers.

windowNibName

The name of the document’s sole nib file.

- windowControllerDidLoadNib:

Called after one of the document's window controllers loads its nib file.

- windowControllerWillLoadNib:

Called before one of the document's window controllers loads its nib file.

- shouldCloseWindowController:delegate:shouldCloseSelector:contextInfo:

Determines whether the system should close the document and its associated window.

Managing Document Windows

- showWindows

Displays all of the document’s windows, bringing them to the front and making them main or key as necessary.

- setWindow:

Sets the window outlet of this document to the specified value.

windowForSheet

Returns the document window to use as the parent of a document-modal sheet.

displayName

The name of the document as displayed in the title bars of the document’s windows and in alert dialogs related to the document.

- setDisplayName:

Sets the name of this document for presentation to the user.

- defaultDraftName

Returns the default draft name for the document subclass.

- encodeRestorableStateWithCoder:backgroundQueue:

Saves the interface-related state of the document.

Configuring the Autosave Behavior

autosavesInPlace

Returns whether the receiver supports autosaving in place.

autosavesDrafts

Returns whether the document subclass supports autosaving of drafts.

preservesVersions

Returns whether the document subclass supports version management.

autosavedContentsFileURL

The location of the most recently autosaved document contents.

autosavingFileType

Returns the document type to use for an autosave operation.

autosavingIsImplicitlyCancellable

Returns a Boolean value that indicates whether you can cancel an in-progress autosave operation.

Autosaving the Document

- checkAutosavingSafetyAndReturnError:

Returns a Boolean value that indicates whether it is safe to autosave document changes.

hasUnautosavedChanges

A Boolean value that indicates whether the document has changes that have not been autosaved.

- scheduleAutosaving

Schedules periodic autosaving for the purpose of crash protection.

- autosaveDocumentWithDelegate:didAutosaveSelector:contextInfo:

Autosaves the document’s contents to an appropriate location in the file system.

- autosaveWithImplicitCancellability:completionHandler:

Autosaves the document’s contents to an appropriate file-system location, as needed.

backupFileURL

The URL for the document’s backup file that was created during an autosave operation.

Browsing Document Versions

- browseDocumentVersions:

Opens the Versions browser in the document’s main window.

browsingVersions

A Boolean value that indicates whether the document is currently displaying the Versions browser.

- stopBrowsingVersionsWithCompletionHandler:

Dismiss the Versions browser for the current document.

Storing Documents in iCloud

- moveDocumentToUbiquityContainer:

Moves the document to the user’s iCloud storage.

usesUbiquitousStorage

Returns whether the document object stores its contents in the user's iCloud document storage.

Managing Undo and Redo Actions

undoManager

The object that the document uses to support undo/redo operations.

hasUndoManager

A Boolean value that indicates whether the document owns an undo manager object.

Updating the Document Change Count

- updateChangeCountWithToken:forSaveOperation:

Updates the document's change count settings after a successful save operation.

- updateChangeCount:

Updates the receiver’s change count according to the given change type.

NSDocumentChangeType

Values that indicate a document’s edit status.

- changeCountTokenForSaveOperation:

Returns an object that encapsulates the current record of document changes at the beginning of a save operation.

Handling Window Restoration

- encodeRestorableStateWithCoder:

Saves the interface-related state of the document.

- restoreStateWithCoder:

Restores the interface-related state of the document.

restorableStateKeyPaths

Returns an array of key paths that represent the restorable attributes of the document.

- invalidateRestorableState

Marks the document’s interface-related state as dirty.

- restoreDocumentWindowWithIdentifier:state:completionHandler:

Restores a window that was associated with a document, after that document is reopened.

Presenting a Save Panel

- runModalSavePanelForSaveOperation:delegate:didSaveSelector:contextInfo:

Presents a modal Save panel to the user, then tries to save the document if the user approves the operation.

- prepareSavePanel:

Tells the document to customize the specified Save panel.

shouldRunSavePanelWithAccessoryView

A Boolean value that indicates whether the document’s Save panel displays a list of supported writable document types.

fileTypeFromLastRunSavePanel

The file type that was last selected in the Save panel.

fileNameExtensionWasHiddenInLastRunSavePanel

A Boolean value that indicates whether the user chose to hide the document's filename extension.

Supporting User Activities

userActivity

An object that encapsulates a user activity supported by this document.

- updateUserActivityState:

Updates the state of the given user activity.

NSUserActivityDocumentURLKey

The key that identifies the document associated with a user activity.

Validating User Interface Items

- validateUserInterfaceItem:

Validates the specified user interface item that the receiver manages.

Performing Tasks Serially

- performSynchronousFileAccessUsingBlock:

Waits for any scheduled file access to complete, then invokes the passed-in block.

- performAsynchronousFileAccessUsingBlock:

Waits for any scheduled file access to complete but without blocking the main thread, then invokes the passed-in block.

- performActivityWithSynchronousWaiting:usingBlock:

Waits for any work scheduled by previous invocations of this method to complete, then invokes the passed-in block.

- continueActivityUsingBlock:

Continues to perform the task for a user activity object using a different block.

- continueAsynchronousWorkOnMainThreadUsingBlock:

Invokes the passed-in block on the main thread.

Handling User Actions

- printDocument:

Prints the receiver in response to the user choosing the Print menu command.

- runPageLayout:

The action method invoked in the receiver as first responder when the user chooses the Page Setup menu command.

- revertDocumentToSaved:

The action of the File menu item Revert in a document-based app.

- saveDocument:

The action method invoked in the receiver as first responder when the user chooses the Save menu command.

- saveDocumentAs:

The action method invoked in the receiver as first responder when the user chooses the Save As menu command.

- saveDocumentTo:

The action method invoked in the receiver as first responder when the user chooses the Save To menu command.

- saveDocumentWithDelegate:didSaveSelector:contextInfo:

Saves the document and delivers the results to the provided delegate object.

Closing the Document

- canCloseDocumentWithDelegate:shouldCloseSelector:contextInfo:

Determines whether to close the document, prompting the user as needed to choose a course of action.

- close

Closes all of the document's windows and removes the document from its document controller.

Reverting the Document Contents

- revertToContentsOfURL:ofType:error:

Discards all unsaved document modifications and replaces the document’s contents by reading a file or file package located by a URL of a specified type.

Duplicating the Document

- duplicateAndReturnError:

Creates a new document whose contents are the same as the receiver and returns an error object if unsuccessful.

- duplicateDocument:

Creates a copy of the receiving document in response to the user choosing Duplicate from the File menu.

- duplicateDocumentWithDelegate:didDuplicateSelector:contextInfo:

Creates a new document whose contents are the same as the current document.

Renaming the Document

- renameDocument:

Renames the current document in response to the user choosing the Rename menu item.

Moving the Document

- moveDocument:

Moves the document to a new location in response to the user choosing the Move To… menu item.

- moveDocumentWithCompletionHandler:

Moves the document to a user-selected location.

- moveToURL:completionHandler:

Moves the document’s file to the given URL.

Locking the Document

- lockDocument:

Locks the document in response to the user choosing the Lock menu item.

- unlockDocument:

Unlocks the document in response to the user choosing the Unlock menu item.

- lockDocumentWithCompletionHandler:

Prevents the user from making further changes to the document.

- lockWithCompletionHandler:

Prevents the user from making changes to the document's file.

- unlockDocumentWithCompletionHandler:

Allows the user to make modifications to the document.

- unlockWithCompletionHandler:

Allows the user to make modifications to the document's file.

locked

A Boolean value that indicates whether or not the file can be written to.

Printing the Document

printInfo

The printing information associated with the document.

- preparePageLayout:

Adds document-specific content to the Page Layout panel.

- runModalPageLayoutWithPrintInfo:delegate:didRunSelector:contextInfo:

Runs the modal page layout panel with the receiver’s printing information object.

- runModalPrintOperation:delegate:didRunSelector:contextInfo:

Runs the specified print operation modally.

- shouldChangePrintInfo:

Returns a Boolean value that indicates whether the document allows changes to the default printing information.

- printDocumentWithSettings:showPrintPanel:delegate:didPrintSelector:contextInfo:

Prints the document's contents, optionally displaying a print panel to the user.

- printOperationWithSettings:error:

Creates and returns a print operation for the document's contents.

PDFPrintOperation

A print operation you can use to create a PDF representation of the document’s current contents.

- saveDocumentToPDF:

Exports a PDF representation of the document’s current contents.

Sharing the Document

allowsDocumentSharing

A Boolean value that indicates whether the document is shareable from the standard Share menu.

- prepareSharingServicePicker:

Perform any custom setup associated with a sharing service picker.

- shareDocumentWithSharingService:completionHandler:

Share the document's file using the specified sharing service.

Handling Script Commands

- handleCloseScriptCommand:

Handles the Close AppleScript command by attempting to close the document.

- handlePrintScriptCommand:

Handles the Print AppleScript command by attempting to print the document.

- handleSaveScriptCommand:

Handles the Save AppleScript command by attempting to save the document.

objectSpecifier

Returns the object specifier that represents the document.

lastComponentOfFileName

The name of the document seen by the user in AppleScript.

Displaying Errors to the User

- presentError:modalForWindow:delegate:didPresentSelector:contextInfo:

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

- presentError:

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

- willPresentError:

Called when the receiver is about to present an error.

- willNotPresentError:

Confirms that the error object is not to be presented to the user and the error cannot be recovered from, so cleanup can be done.

Deprecated

Avoid using deprecated classes and protocols in your apps.

Deprecated Symbols

Review symbols that are no longer supported, and find replacements.

See Also

Documents

Developing a Document-Based App

Write an app that creates, manages, edits, and saves text documents.

NSDocumentController

An object that manages an app's documents.

NSPersistentDocument

A document object that can integrate with Core Data.