Search code examples
cocoadrag-and-dropitunesnstableview

Drag rows out of iTunes into Cocoa App


How does one accept dragged rows from iTunes in a Cocoa Application ?

I have two objects in my application that accept Drag and Drop: an NSTableView and a custom view. Both recognize files dragged from the finder, but neither recognize rows dragged from iTunes.


Solution

  • Well, here's the answer. This will handle dragging things from iTunes, as well as dragging files from the finder. What you get is a list of filepaths. "sender" is the guy who has the dragging pasteboard. The short answer is that "com.apple.pasteboard.promised-file-url" is the key that you need from the iTunes paste.

    NSMutableArray *paths = [NSMutableArray arrayWithCapacity:1];
    NSArray *pasteboardTypes = [NSArray arrayWithObjects:@"com.apple.pasteboard.promised-file-url", @"public.file-url", nil];
    for(NSPasteboardItem *item in [[sender draggingPasteboard] pasteboardItems]) {
      NSString *urlString = nil;
      for(NSString *type in pasteboardTypes) {
        if([[item types] containsObject:type]) {
          urlString = [item stringForType:type];
          break;
        }
      }
      if(urlString) {
        NSString *path = [[NSURL URLWithString:urlString] path];
        [paths addObject:path];
      }
    }
    NSLog(@"Pasted Paths: %@", paths);