<!--
{
  "availability" : [
    "macOS: 10.7.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "AppKit",
  "identifier" : "/documentation/AppKit/NSDocumentController/duplicateDocument(withContentsOf:copying:displayName:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "AppKit"
    ],
    "preciseIdentifier" : "c:objc(cs)NSDocumentController(im)duplicateDocumentWithContentsOfURL:copying:displayName:error:"
  },
  "title" : "duplicateDocument(withContentsOf:copying:displayName:)"
}
-->

# 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.

```
func duplicateDocument(withContentsOf url: URL, copying duplicateByCopying: Bool, displayName displayNameOrNil: String?) throws -> NSDocument
```

## Parameters

`url`

The URL locating the document from which contents of the new document are copied.

`duplicateByCopying`

If <doc://com.apple.documentation/documentation/Swift/true>, the contents located at the passed-in URL are copied into a file located in the directory used for the autosaved contents of untitled documents.

`displayNameOrNil`

If not `nil` then this value is used to derive a display name for the new document that does not match one that is already in use by an open document.

## Return Value

The newly created [`NSDocument`](/documentation/AppKit/NSDocument) object, or `nil` if the document could not be created.

## Discussion

The default implementation of this method copies the file if specified, determines the type of the document, calls [`makeDocument(for:withContentsOf:ofType:)`](/documentation/AppKit/NSDocumentController/makeDocument(for:withContentsOf:ofType:)) to instantiate it, sends the document `setDisplayName:` to name it if `displayNameOrNil` is not `nil`, calls [`addDocument(_:)`](/documentation/AppKit/NSDocumentController/addDocument(_:)) to record its opening, and sends the document [`makeWindowControllers()`](/documentation/AppKit/NSDocument/makeWindowControllers()) and [`showWindows()`](/documentation/AppKit/NSDocument/showWindows()) messages.

The default implementation of this method uses the file coordination mechanism introduced in OS X v10.7. It passes the document to the `NSFileCoordinator` method <doc://com.apple.documentation/documentation/Foundation/NSFileCoordinator/addFilePresenter(_:)> immediately after calling the [`addDocument(_:)`](/documentation/AppKit/NSDocumentController/addDocument(_:)) method. (The balancing invocation of the <doc://com.apple.documentation/documentation/Foundation/NSFileCoordinator> method <doc://com.apple.documentation/documentation/Foundation/NSFileCoordinator/removeFilePresenter(_:)> is in the [`NSDocument`](/documentation/AppKit/NSDocument) method [`close()`](/documentation/AppKit/NSDocument/close()).)

You can override this method to customize how documents are duplicated. It is called by the [`NSDocument`](/documentation/AppKit/NSDocument) method [`duplicate()`](/documentation/AppKit/NSDocument/duplicate()). It may also be called from other places in AppKit.

In most cases, an app does not need to call this method directly.

> Handling Errors in Swift:
> In Swift, this method returns a nonoptional result and is marked with the `throws` keyword to indicate that it throws an error in cases of failure.
> 
> You call this method in a `try` expression and handle any errors in the `catch` clauses of a `do` statement, as described in [Error Handling](https://docs.swift.org/swift-book/LanguageGuide/ErrorHandling.html) in [The Swift Programming Language](https://docs.swift.org/swift-book/) and `About Imported Cocoa Error Parameters`.

---

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)