<!--
{
  "availability" : [
    "macCatalyst: -",
    "macOS: 10.0.0 - 10.4.0"
  ],
  "documentType" : "symbol",
  "framework" : "AppKit",
  "identifier" : "/documentation/AppKit/NSDocumentController/openDocumentWithContentsOfFile:display:",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "AppKit"
    ],
    "preciseIdentifier" : "c:objc(cs)NSDocumentController(im)openDocumentWithContentsOfFile:display:"
  },
  "title" : "openDocumentWithContentsOfFile:display:"
}
-->

# openDocumentWithContentsOfFile:display:

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

```
- (id) openDocumentWithContentsOfFile:(NSString *) fileName display:(BOOL) display;
```

## Discussion

Returns an `NSDocument` object created from the contents of the file `fileName` (an absolute path) and displays it if `flag` is <doc://com.apple.documentation/documentation/Swift/true>. The returned object is not retained, but is added to the receiver’s list of managed documents. Returns `nil` if the object could not be created, typically because `fileName` does not point to a valid file or because there is no `NSDocument` subclass for the document type (as indicated by the file extension or HFS file type). Even if `flag` is <doc://com.apple.documentation/documentation/Swift/true>, the document is not displayed if [`shouldCreateUI`](/documentation/AppKit/NSDocumentController/shouldCreateUI) returns <doc://com.apple.documentation/documentation/Swift/false>. This method calls [`makeDocumentWithContentsOfFile:ofType:`](/documentation/AppKit/NSDocumentController/makeDocumentWithContentsOfFile:ofType:) to obtain the created NSDocument object. If you override this method, your implementation should be prepared to handle either <doc://com.apple.documentation/documentation/Swift/true> or <doc://com.apple.documentation/documentation/Swift/false>.

To handle an Open Documents Apple event, the Application Kit’s built-in Apple event handling automatically calls this method with the path to the file to open and a display argument.

Called with a display argument of <doc://com.apple.documentation/documentation/Swift/true> instead of <doc://com.apple.documentation/documentation/Swift/false> when a Print Documents Apple event is handled. This may have been handled differently in versions of macOS prior to version 10.3.

---

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)