2

I posted this question about dragging content from OS X Finder into an NSTableView. This all works nicely now. However, if I want to drag URLs from a browser address bar into my app, I first need to drag them to the desktop (where they appear as a .webloc file) and then drag them into my app.

Is there a way to directly drag them from the browser address bar into my app, without having to drag them to the desktop first?

I tried registering kUTTypeURL but this doesn't seem to work as dragged URLs bounce back to their origin:

[[self sourcesTableView] registerForDraggedTypes:[NSArray arrayWithObjects: (NSString*)kUTTypeFileURL, (NSString*)kUTTypeURL, nil]];
Community
  • 1
  • 1
Roger
  • 4,737
  • 4
  • 43
  • 68

1 Answers1

0

In my accepted answer to your other question, the code I provided specifically restricts the URLs that your app can accept to file URLs:

NSArray* urls = [pb readObjectsForClasses:[NSArray arrayWithObject:[NSURL class]]
 options:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] 
                                     forKey: NSPasteboardURLReadingFileURLsOnlyKey]];

Note the options dictionary containing a boolean YES for the NSPasteboardURLReadingFileURLsOnlyKey.

If you want to accept any URL, just do this:

NSArray* urls = [pb readObjectsForClasses:[NSArray arrayWithObject:[NSURL class]]
                                  options:nil];

Or even better, you can require that you'll accept any URL as long as it is of a particular type, in this case an image:

NSArray* acceptedTypes = [NSArray arrayWithObject:(NSString*)kUTTypeImage];
NSArray* urls = [pb readObjectsForClasses:[NSArray arrayWithObject:[NSURL class]]
                                  options:[NSDictionary dictionaryWithObject:acceptedTypes 
                                   forKey:NSPasteboardURLReadingContentsConformToTypesKey]];
Community
  • 1
  • 1
Rob Keniger
  • 45,830
  • 6
  • 101
  • 134
  • Fair point. I was not paying attention to the pasteboard side of the drag operation. Thanks for pointing that out. – Roger Apr 26 '12 at 00:08
  • That's fine. I've simplified the answer on your original question to use the `NSPasteboardURLReadingContentsConformToTypesKey`, which I wasn't aware of myself until today, so we both learned something :-) – Rob Keniger Apr 26 '12 at 00:12