I have tried this which doesn't work:
NSString *string = @"Click Here";
NSURL *url = [NSURL URLWithString:@"https://google.com"];
// Get the shared pasteboard
NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
// Create a new pasteboard item
NSPasteboardItem *item = [[NSPasteboardItem alloc] init];
[item setString:string forType:NSPasteboardTypeString];
[item setString:url.absoluteString forType: @"org.chromium.source-url"];
// Clear the pasteboard and write the new item
[pasteboard clearContents];
[pasteboard writeObjects:@[item]];
NSLog(@"Hyperlink copied to pasteboard: %@ -> %@", string, url);
I have also tried this which doesn't work:
NSString *string = @"Click Here";
NSURL *url = [NSURL URLWithString:@"https://google.com"];
// Get the shared pasteboard
NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
// Create a new pasteboard item
NSPasteboardItem *item = [[NSPasteboardItem alloc] init];
[item setString:string forType:NSPasteboardTypeString];
[item setString:url.absoluteString forType:NSPasteboardTypeURL];
// Clear the pasteboard and write the new item
[pasteboard clearContents];
[pasteboard writeObjects:@[item]];
NSLog(@"Hyperlink copied to pasteboard: %@ -> %@", string, url);
I only get a string to paste.
A few observations:
The simple way to add a URL (without the “display text”) is to just write the NSURL
as an object and be done with it.
[pasteboard writeObjects:@[url]];
If you use NSPasteboardTypeURL
, it would be setPropertyList
, not setString
.
I know there are sources that suggest that you can set setString
with the display text, but in my experience, apps that pick up the simple string representation do not generally also simultaneously pick up the URL. And for apps that only pick up the string, the URL is generally more important than the “Click here” text, so I would be inclined to make the simple string representation the URL rather than the display text.
I think the best way to support the “Click here” display text is with the RTF and HTML representations. The RTF representation has greater utility than the HTML, but it simply depends upon where you plan on pasting this. Different apps offer differing level of support for different representations.
Anyway, perhaps:
NSURL *url = [NSURL URLWithString:@"https://example.com"];
NSString *displayText = @"Click here";
NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
[pasteboard clearContents];
NSPasteboardItem *item = [[NSPasteboardItem alloc] init];
// Add the URL as the string representation
[item setString:url.absoluteString forType:NSPasteboardTypeString];
// Add RTF representation
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:displayText];
NSRange range = NSMakeRange(0, displayText.length);
[attributedString addAttribute:NSLinkAttributeName value:url range:range];
NSData *rtf = [attributedString RTFFromRange:range documentAttributes:@{}];
[item setData:rtf forType:NSPasteboardTypeRTF];
// Add HTML representation
NSString *htmlString = [NSString stringWithFormat:@"<a href=\"%@\">%@</a>", url.absoluteString, displayText];
[item setString:htmlString forType:NSPasteboardTypeHTML];
// Add the URL
[item setPropertyList:url.absoluteString forType:NSPasteboardTypeURL];
// Write the `NSPasteboardItem`
[pasteboard writeObjects:@[item]];
When I view this with a ClipboardViewer (one of the additional Xcode developer tools), this results in it having a URL, RTF, and HTML representations.