2

enter image description here

If you drag and drop a selection of text onto a folder, you will get a file with an extension of textClipping. TextEdit's document window accepts a text selection. Many applications do accept textClipping. How do you receive a text selection on an NSImageView drop box? The regular operation of performDragOperation doesn't appear to accept a selection of text.

- (BOOL)performDragOperation:(id<NSDraggingInfo>)sender {
NSPasteboard *pboard = [sender draggingPasteboard];
NSArray *urls;
    if ([[pboard types] containsObject:NSURLPboardType]) {
        urls = [pboard readObjectsForClasses:@[[NSURL class]] options:nil];
    }

    AppDelegate *appDelegate = (AppDelegate *)[NSApp delegate];
    ...
    ...

    return YES;
}

These lines of code let me accept files, but not textClipping. What is the secret of accepting textClipping? Maybe, you can't accept it with NSImageView? Running a search with 'Objective-C textClipping' turns up nothing.

Thank you for your advice.

El Tomato
  • 6,479
  • 6
  • 46
  • 75

1 Answers1

2

Text clippings are either strings or attributed strings (if the contents contain rich text).
To read those objects from the pasteboard you have to search for NSStringPboardType or NSRTFPboardType respectively.

NSStringPboardType can be read as NSString.
NSRTFPboardType can be read as NSAttributedString.

- (BOOL)performDragOperation:(id<NSDraggingInfo>)sender
{
    NSPasteboard* pboard = [sender draggingPasteboard];
    NSArray* pboardContents = nil;
    if ([[pboard types] containsObject:NSURLPboardType])
    {
        pboardContents = [pboard readObjectsForClasses:@[[NSURL class]] options:nil];
    }
    if ([[pboard types] containsObject:NSStringPboardType])
    {
        pboardContents = [pboard readObjectsForClasses:@[[NSString class]] options:nil];
    }
    if ([[pboard types] containsObject:NSRTFPboardType])
    {
        pboardContents = [pboard readObjectsForClasses:@[[NSAttributedString class]] options:nil];
    }
    NSLog(@"Pasteboard contents:%@", pboardContents);
    return YES;
}
Thomas Zoechling
  • 34,177
  • 3
  • 81
  • 112
  • Thank you, weichsel. An NSImageView drop box with this code doesn't appear to accept a text drop. What am I doing wrong, do you know? – El Tomato May 27 '13 at 09:13
  • Actually, it seems that I can just use an NSTextView control to accept a text drop. – El Tomato May 27 '13 at 09:17