<!--
{
  "availability" : [
    "macOS: 10.7.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "AppKit",
  "identifier" : "/documentation/AppKit/NSTextViewDelegate/textView(_:urlForContentsOf:at:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "AppKit"
    ],
    "preciseIdentifier" : "c:objc(pl)NSTextViewDelegate(im)textView:URLForContentsOfTextAttachment:atIndex:"
  },
  "title" : "textView(_:urlForContentsOf:at:)"
}
-->

# textView(_:urlForContentsOf:at:)

Returns a URL representing the document contents for a text attachment.

```
@MainActor optional func textView(_ textView: NSTextView, urlForContentsOf textAttachment: NSTextAttachment, at charIndex: Int) -> URL?
```

## Parameters

`textView`

The text view sending the message.

`textAttachment`

The text attachment object containing an `NSFileWrapper` object that holds the contents of the attached file.

`charIndex`

The character index of the text attachment.

## Return Value

The absolute URL for the document contents represented by `textAttachment`.

## Discussion

The returned `NSURL` object is used by the text view to provide default behaviors involving text attachments such as Quick Look and double-clicking. For example, the `NSTextView` method [`quickLookPreviewableItems(inRanges:)`](/documentation/AppKit/NSTextView/quickLookPreviewableItems(inRanges:)) uses this method for mapping text attachments to their corresponding document URLs, and `NSTextView` invokes the `NSWorkspace` method [`open(_:)`](/documentation/AppKit/NSWorkspace/open(_:)) with the URL returned from this method when the delegate has no [`textView(_:doubleClickedOn:in:at:)`](/documentation/AppKit/NSTextViewDelegate/textView(_:doubleClickedOn:in:at:)) implementation.

## See Also

[`textView(_:doubleClickedOn:in:at:)`](/documentation/AppKit/NSTextViewDelegate/textView(_:doubleClickedOn:in:at:))

Sent when the user double-clicks a cell.

[`quickLookPreviewableItems(inRanges:)`](/documentation/AppKit/NSTextView/quickLookPreviewableItems(inRanges:))

Returns an array of URLs for items that can be displayed by QuickLook in the specified ranges.



---

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)