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

# OpenDocumentAction

An action that presents an existing document.

```
@MainActor struct OpenDocumentAction
```

## Overview

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

For example, you can create a button that opens the document at the
specified URL:

```
struct OpenDocumentButton: View {
    var url: URL
    @Environment(\.openDocument) private var openDocument

    var body: some View {
        Button(url.deletingPathExtension().lastPathComponent) {
            Task {
                do {
                    try await openDocument(at: url)
                } catch {
                    // Handle error
                }
            }
        }
    }
}
```

The above example uses a `do-catch` statement to handle any errors
that the open document action might throw. It also places the action
inside a task and awaits the result because the action operates
asynchronously.

To present an existing document, your app must define a
[`DocumentGroup`](/documentation/SwiftUI/DocumentGroup) that handles the content type of the specified file.
For a document that’s already open, the system brings the existing
window to the front. Otherwise, the system opens a new window.

## Topics

### Calling the action

[`callAsFunction(at:)`](/documentation/SwiftUI/OpenDocumentAction/callAsFunction(at:))

Opens the document at the specified file URL.



---

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)