Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dropping Files onto Dock Icon in Cocoa

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?

like image 391
Tristan Avatar asked Mar 22 '10 04:03

Tristan


3 Answers

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.

like image 128
Josh Freeman Avatar answered Oct 13 '22 00:10

Josh Freeman


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: Project > Info > Document Types

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. Delete empty Document Content Type UTIs array from Info.plist

Your Info.plist should look something like this: Document Types Info.plist

like image 45
David Douglas Avatar answered Oct 13 '22 02:10

David Douglas


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.

like image 38
Marc Liyanage Avatar answered Oct 13 '22 02:10

Marc Liyanage