<!--
{
  "availability" : [
    "macOS: 13.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "SwiftUI",
  "identifier" : "/documentation/SwiftUI/NewDocumentAction",
  "metadataVersion" : "0.1.0",
  "role" : "Structure",
  "symbol" : {
    "kind" : "Structure",
    "modules" : [
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI17NewDocumentActionV"
  },
  "title" : "NewDocumentAction"
}
-->

# NewDocumentAction

An action that presents a new document.

```
@MainActor @preconcurrency struct NewDocumentAction
```

## Overview

Use the [`newDocument`](/documentation/SwiftUI/EnvironmentValues/newDocument) environment value to get the
instance of this structure for a given [`Environment`](/documentation/SwiftUI/Environment). Then call the
instance to present a new document. You call the instance directly
because it defines a [`callAsFunction(_:)`](/documentation/SwiftUI/NewDocumentAction/callAsFunction(_:))
method that Swift calls when you call the instance.

For example, you can define a button that creates a new document from
the selected text:

```
struct NewDocumentFromSelection: View {
    @FocusedBinding(\.selectedText) private var selectedText: String?
    @Environment(\.newDocument) private var newDocument

    var body: some View {
        Button("New Document With Selection") {
            newDocument(TextDocument(text: selectedText))
        }
        .disabled(selectedText?.isEmpty != false)
    }
}
```

The above example assumes that you define a `TextDocument` that
conforms to the [`FileDocument`](/documentation/SwiftUI/FileDocument) or [`ReferenceFileDocument`](/documentation/SwiftUI/ReferenceFileDocument)
protocol, and a [`DocumentGroup`](/documentation/SwiftUI/DocumentGroup) that handles the associated file type.

## Topics

### Calling the action

[`func   callAsFunction ( _ :)`](/documentation/SwiftUI/NewDocumentAction/callAsFunction(_:))

Presents a new document window.

[`callAsFunction(contentType:)`](/documentation/SwiftUI/NewDocumentAction/callAsFunction(contentType:))

Presents a new document window.

[`callAsFunction(contentType:prepareDocument:)`](/documentation/SwiftUI/NewDocumentAction/callAsFunction(contentType:prepareDocument:))

Presents a new document window with preset contents.

## Relationships

### Conforms To

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

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

---

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)