Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drag and Drop - Is it possible to get the URL?

I implemented a simple drag and drop. The main purpose is for users to drag and drop images from a browser. Is it possible for me get the URL that this image was dragged from?

So lets say, I am on SO and I drag and drop the logo. Is there a way for me to know that this was from http://stackoverflow.com ? Thanks

like image 778
0xSina Avatar asked Apr 18 '12 05:04

0xSina


1 Answers

For images which aren't also links, the following code will log the URL a dragged image came from. This works for me in Safari & Firefox.

@implementation DragView

- (void)awakeFromNib {
    [self registerForDraggedTypes:[NSArray arrayWithObject:NSURLPboardType]];
}

- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender {
    return NSDragOperationCopy;
}

- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender {
    NSPasteboard *pboard;    
    pboard = [sender draggingPasteboard];

    NSLog(@"types: %@", [pboard types]);
    NSLog(@"url: %@", [NSURL URLFromPasteboard:pboard]);

    return YES;
}

@end

If the image is also a link, the logged URL is a href of that link. It's also possible to get the "where from" URL from a file (as seen in the Finder's Get Info panel) using the kMDItemWhereFroms key of the extended attributes.

like image 148
mrwalker Avatar answered Sep 21 '22 15:09

mrwalker