Daniel
Daniel

Reputation: 1057

How to copy multiple URLs to the clipboard?

I have an array of URLs that I want to copy to the clipboard:

let urls = [URL(string: "https://www.apple.com")!,  URL(string: "https://www.microsoft.com")!]

I've tried the following:

NSPasteboard.general.writeObjects(urls as [NSURL])

However, that only copies the first URL to the clipboard.

This also only copies the first url:

for url in urls {
    NSPasteboard.general.writeObjects([url as NSPasteboardWriting])
}

The following code works, but only copies the strings. If other apps read the pasteboard, they will find strings, but not URLs:

let urlStrings = urls.compactMap { url in
     url.absoluteString
}
for urlStrig in urlStrings {
      NSPasteboard.general.writeObjects([urlStrig as NSPasteboardWriting])
}

I've tried NSPasteboard.general.prepareForNewContents() and NSPasteboard.general.clearContents before writing to the pasteboard.

What am I doing wrong?

Upvotes: 1

Views: 235

Answers (0)

Related Questions