title: Cocoa: pasting formatted text in the current font?
body: <p>When I put formatted text into the pasteboard (NSRTFPboardType), it pastes with all formatting preserved.  But what I'd really like is to discard the font face and size information, while preserving the weight, color, etc.</p>
  
  <p>According to the docs, an NSAttributedString with no font information will default to Helvetica 12, so that seems like a dead end.</p>
  
  <p>I can also generate the text on demand, so if I could find out the font in the current UI element, I could modify the text before it goes into the pasteboard.  I was hoping the accessibility API could help with this, but none of the attributes I can find in the UIElementInspector seem to deal with formatting.</p>
  
  <p>Any ideas?</p>
  
  <p>Here's a test case.  It pastes in Helvetica 12 even though the only attribute is a green color:</p>
  
  <pre><code>// Create the string with no attributes, and strip the font just in case.
  NSMutableAttributedString *s = [[[NSMutableAttributedString alloc] initWithString:@"Hello green world!"] autorelease];
  [s removeAttribute:NSFontAttributeName range:NSMakeRange(0, [s length])];
  
  // Add a test attribute
  [s addAttribute:NSForegroundColorAttributeName value:[NSColor greenColor] range:NSMakeRange(6, 5)];
  
  // Generate RTF data 
  NSData *rtf = [s RTFFromRange:NSMakeRange(0, [s length]) documentAttributes:nil];
  
  // Copy to pasteboard
  NSPasteboard *pb = [NSPasteboard generalPasteboard];
  [pb declareTypes:[NSArray arrayWithObject:NSRTFPboardType] owner:nil];
  [pb setData:rtf forType:NSRTFPboardType];
  </code></pre>
  
  <p>Here's something interesting.  If I try and generate the plainest raw RTF data I can, with <i>absolutely no font information</i>, it still pastes in Helvetica 12!</p>
  
  <pre><code>char *rawrtf = "{\\rtf1\\ansi\\ansicpg1252\n"
                 "Hello world.}";
  NSData *rtf = [NSData dataWithBytes:rawrtf length:strlen(rawrtf)];
  </code></pre>
  
  <p>So if this is possible at all, I think it's only possible by querying the currently running application about the current font.</p>