Dropping Files onto Dock Icon in Cocoa

痞子三分冷 提交于 2019-11-27 02:12:42

问题


How can I drop a file(or select to open it in Finder) of a type specified in the Info.plist onto my dock icon and then calling a method with the full path of the file?


回答1:


If you've set up your Info.plist's CFBundleDocumentTypes array properly (either 'LSItemContentTypes' or 'CFBundleTypeExtensions'), then you just need to set up an NSApplication delegate and implement the delegate method, application:openFile:.

If you're expecting multiple files to be dropped at once, implement application:openFiles:.

For promised files (NSFilesPromisePboardType/kPasteboardTypeFileURLPromise) see Dropping promised files on to application icon in Dock.




回答2:


Here's an updated solution for Xcode 5. In AppDelegate.m

-(BOOL)application:(NSApplication *)sender openFile:(NSString *)filename
{
    NSLog(@"%@", filename);
    return YES;
}

And in Xcode setup Document Types under Project > Targets > Info:

Check settings in Info.plist in case you have an empty 'Document Content Type UTIs' array which should be filled out properly or else deleted.

Your Info.plist should look something like this:




回答3:


On current systems you can use a UTI instead of the old-style four-char types (such as fold above). In Xcode's document type editor, make a new type with:

  • Name: Folder
  • Identifier: public.folder

public.folder is a subtype of public.directory. public.folder matches directories that appear as such to the user, i.e. not packages like .app wrappers.




回答4:


Select your application in the target group of the side pane and use get info. Then in the new window select the properties tab to add a new document type. Name it "Folder" for convenience and the OS Types needs to be "fold"; the store type and role you can leave as is.




回答5:


If you're actually making a document-based app, setting it up to give you the path will have you doing far more work than you need to. Simply use the document-based application template. The document controller will create an instance of the right class for you; you need only write that class.

An application you create this way will handle file drops (by opening them as documents) for free.



来源:https://stackoverflow.com/questions/2489961/dropping-files-onto-dock-icon-in-cocoa

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