如何将超链接放入其他应用程序可识别的 NSPasteboard 中?

问题描述 投票:0回答:1

我已经尝试过这个不起作用:

    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);

我也尝试过这个不起作用:

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);

我只得到一个要粘贴的字符串。

objective-c cocoa
1个回答
0
投票

一些观察:

  1. 添加 URL(不带“显示文本”)的简单方法是将

    NSURL
    写为对象并使用它。

    [pasteboard writeObjects:@[url]];
    
  2. 如果您使用

    NSPasteboardTypeURL
    ,则会是
    setPropertyList
    ,而不是
    setString

  3. 我知道有消息来源建议您可以使用显示文本设置

    setString
    ,但根据我的经验,拾取简单字符串表示形式的应用程序通常不会同时拾取 URL。对于仅获取字符串的应用程序,URL 通常比“单击此处”文本更重要,因此我倾向于将简单的字符串表示形式作为 URL 而不是显示文本。

  4. 我认为支持“单击此处”显示文本的最佳方式是使用 RTF 和 HTML 表示形式。我想这取决于您打算将其粘贴到哪里。

因此,也许:

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];

// File URL
if (url.isFileURL) {
    [item setPropertyList:url.path forType:NSPasteboardTypeFileURL];
}

// Write the `NSPasteboardItem`
[pasteboard writeObjects:@[item]];

当我使用 ClipboardViewer(附加的 Xcode 开发工具之一)查看此内容时,结果会显示它具有 URL、RTF 和 HTML 表示形式。

© www.soinside.com 2019 - 2024. All rights reserved.