title: Putting contents of a file on NSPasteboard using UTI not NSFileContentsPboardType?
body: <p>I have rich-text file with a few images and text. In my Cocoa app I'm trying to put the contents of that file on the <code>NSDragPboard</code> so that the user can drag the <em>contents</em> into other applications (TextEdit, Mail, etc.).</p>
  
  <p>One usecase is this: the rich-text file contains an email template and the user drags the contents into a new Mail message.</p>
  
  <p>I tried this without success:</p>
  
  <pre><code>NSPasteboard *pboard = [NSPasteboard pasteboardWithName:NSDragPboard];
  [pboard writeFileContents:filePath];
  </code></pre>
  
  <p>The Apple docs say this:</p>
  
  <blockquote>
    <p><em>Writes the contents of the file
    filename to the receiver and declares
    the data to be of type
    <strong>NSFileContentsPboardType</strong> and also of a
    type appropriate for the file’s
    extension (as returned by the
    NSCreateFileContentsPboardType
    function when passed the files
    extension), if it has one.</em></p>
  </blockquote>
  
  <p>In the ClipboardViewer app I can see a few data types, one being <code>NXFileContentsPboardType</code>. However, apps such as Mail and TextEdit don't allow to drop that data type.</p>
  
  <p>When I copy the rich-text content to the clipboard by hand (Control+C), I see all the different data types I want:</p>
  
  <ul>
  <li><code>public.rtf</code></li>
  <li><code>public.utf8-plain-text</code></li>
  <li><code>NSStringPboardType</code></li>
  <li>...</li>
  </ul>
  
  <p>So how can I do that programmatically myself? Do I have to figure out all the various UTI types and set the according data? I thought that's what <code>[pboard writeFileContents:path]</code> was for...</p>
  
  <p>Thanks, Mark.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Making progress... This works for <code>.rtf</code> files, but not for <code>.txt</code> files:</p>
  
  <pre><code>NSString *uti = // helper code to get UTI from NSURL resource key...
  NSPasteboard *pboard = [NSPasteboard pasteboardWithName:NSDragPboard];
  [pboard declareTypes:[NSArray arrayWithObject:uti] owner:nil];
  [pboard setData:[NSData dataWithContentsOfFile:filePath] forType:uti];
  </code></pre>
  
  <p>For <code>.txt</code> files the data type on the pasteboard is <code>public.text</code>. However, TextEdit and Mail.app won't allow dropping that type. <code>public.rtf</code> in case of rich text works fine...</p>