开发者

Drag rows out of iTunes into Cocoa App

How does one accept dr开发者_运维百科agged 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.


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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜