Contents

detectedMetadata(for:)

Determines available metadata from the specified metadata types for this pasteboard item, without notifying the person using the app.

Declaration

func detectedMetadata(for keyPaths: Set<PartialKeyPath<NSPasteboardItem.DetectedMetadata>>) async throws -> NSPasteboardItem.DetectedMetadata

Parameters

  • keyPaths:

    The metadata types to detect on the pasteboard item.

Return Value

An NSPasteboard.DetectedMetadata instance containing the metadata for the metadata types detected on the pasteboard item.

Discussion

This method only gives access to limited types of metadata and doesn’t allow the app to access the item’s contents. As a result, the system doesn’t notify the person using the app about reading the contents of the pasteboard.

For details about the metadata returned for each type, see NSPasteboard.DetectedMetadata.

The following example shows how to iterate over each pasteboard item and, if the item is a URL that points to a file, get its content type with this method.

guard let pasteboardItems = NSPasteboard.general.pasteboardItems else { return }
for (index, item) in pasteboardItems.enumerated() {
    do {
        let metadataResults = try await item.detectedMetadata(for: [\.contentType])
        if let contentType = metadataResults.contentType {
            print("Item \(index) - Content type is: \(contentType.identifier).")
        } else {
            print("Item \(index) - Couldn't get content type.")
        }
    } catch {
        print("Item \(index) - Error: \(error).")
    }
}

See Also

Detecting patterns and metadata in pasteboard items