Search code examples
objective-ccocoartfnspasteboard

NSRTFPboardType & paste into gmail browser window?


I have a NSTextView which contains some text with attributes (syntax highlighting). I'm trying to have a copy option which keeps the syntax highlighting so that I can paste it into a gmail text window. Currently the highlighting does not appear when I copy paste it, however if I were to copy the following section directly from this stackoverflow page:

- (void) copyAsRTF
{
    NSPasteboard *pateboard = [NSPasteboard generalPasteboard];
    NSData * rtfData = [[self textStorage] RTFFromRange: [self selectedRange]
                                     documentAttributes: nil];

    if (rtfData)
    {
        NSString * test = [[NSString alloc] initWithData: rtfData
                                                encoding: NSUTF8StringEncoding];

        [pateboard declareTypes: @[NSRTFPboardType]
                          owner: self];

        [pateboard setData: rtfData
                   forType: NSRTFPboardType];
    } // End of we had data
} // End of copyAsRTF

And paste it into gmail, it will paste with full syntax highlight no problem. The above code is what I use for generating my RTF code and I can confirm that it does generate proper RTF as I have a test variable being generated.

Any ideas what I am doing wrong here? To my understanding this SHOULD work.

(I should note that I have tried in multiple browsers - Chrome, Safari and Firefox).


Solution

  • As discovered in comments, in order to be working in chrome, you need to copy your text as html (public.html pasteboard type) or probably both html an rtf types.