How do I handle multiple file drag/drop from Finder in Mac OS X 10.5?

南楼画角 提交于 2019-12-21 04:05:03

问题


I need to get the URLs of all files dragged/dropped into my application from Finder.

I have a Cocoa app running on 10.6 which does this by using the new 10.6 NSPasteboard APIs which handle multiple items on the pasteboard. I'm trying to backport this app to 10.5. How do I handle this on 10.5?

If I do something like below, I only get the first URL:

    NSArray *pasteTypes = [NSArray arrayWithObjects: NSURLPboardType, nil];
    NSString *bestType = [pboard availableTypeFromArray:pasteTypes]; 
    if (bestType != nil) {
        NSURL *url = [NSURL URLFromPasteboard:pboard];
    }        

回答1:


The IKImageKit programming topics outline a way to do this like so (paraphrased):

   NSData *data = [pasteboard dataForType:NSFilenamesPboardType];
   NSArray *filenames = [NSPropertyListSerialization
        propertyListFromData:data
            mutabilityOption:kCFPropertyListImmutable
                      format:nil
            errorDescription:&errorDescription];

See here: Image Kit Programming Guide: Supporting Drag and Drop




回答2:


Getting multiple filenames is easy: (While getting multiple URLs is not with 10.5)

  1. Register your view for NSFilenamesPboardType
  2. In performDragOperation: do the following to get an array of file paths:

NSPasteboard* pboard = [sender draggingPasteboard];
NSArray* filenames = [pboard propertyListForType:NSFilenamesPboardType];



回答3:


The NSURLPboardType just handles one URL.

To get a list of files you need to create a NSArray from a NSFilenamesPboardType.

Apple's docs on drag and drop are pretty good, even if it's older stuff.




回答4:


How do I handle [multiple items on a pasteboard] on 10.5?

Try the Pasteboard Manager.

The tricky part is that you're handling a drop, which means you're receiving an NSPasteboard already created for you, and there's no way to convert between NSPasteboard objects and PasteboardRefs. You'll have to ask the NSPasteboard for its name, then pass the same name to PasteboardCreate, and that may not work.



来源:https://stackoverflow.com/questions/1998158/how-do-i-handle-multiple-file-drag-drop-from-finder-in-mac-os-x-10-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!