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?
-
I don't understand the question. Is the user dragging a file onto your Dock icon, or do you want to open the file programmatically? – Peter Hosey Mar 22 '10 at 04:25
-
I mean to actually get the path to the file that was dropped onto the dock icon. – Tristan Mar 22 '10 at 04:43
5 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.

- 1
- 1

- 2,262
- 16
- 14
-
3I have set up my CFBundleDocumentTypes array properly, and wrote the method, but The dock icon will simply not accept my file. – Tristan Mar 22 '10 at 14:03
-
1Tristian Seifert: One possible cause of that problem is that you specified a UTI in your plist, but it was the wrong one: http://boredzo.org/blog/archives/2007-07-23/how-to-make-your-apps-dock-tile-highlight You also should make sure to import any UTIs you mention in the plist: http://developer.apple.com/mac/library/documentation/FileManagement/Conceptual/understanding_utis/ – Peter Hosey Mar 22 '10 at 14:32
-
After reading these two documents, I still do not quite understand how to add UTI's to my existing PList. Here is a small section of my Info.plist:
CFBundleTypeIconFile Draft.icns CFBundleTypeExtensions podraft CFBundleTypeName Post Office Draft -
Finder opens the Files with my app automatically, but the dock still won't after I deleted the build folder and re-built. – Tristan Mar 22 '10 at 23:04
-
Note that if you implement both openFile: and openFiles: in your delegate, then openFiles: will always be the method called, even if there is just one file dragged over the icon. – Chris Markle Sep 12 '12 at 23:22
-
1This didn't work for me before I made a release build and placed the app in the Applications folder. After that it started working for building and running debug builds in XCode too. – borisgolovnev Jul 16 '18 at 19:18
-
2
-
What @borisgolovnev said is a big point. It won't work until you place your app package in an `Applications` folder. It worked once I copied my app package into `Applications` folder. And I'm not sure what will happen if I delete the app package from the `Applications` folder. – eonil Jul 19 '20 at 03:19
-
-
and seemingly SwiftUI doesn't support the traditional way of accessing the dropped file paths via `application(_:openFiles:)` method: https://stackoverflow.com/questions/65313313/how-do-i-handle-dragging-onto-the-dock-icon-in-swiftui – nishanthshanmugham Mar 07 '22 at 15:44
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:

- 10,377
- 2
- 55
- 53
-
Note that this will make your application accept *any* file, not just a given extension, because CFBundleTypeExtensions is set to `*`. If you only support certain file extensions, specify them there instead. Otherwise the OS will offer to open random files with your application just for your app to fail then. – uliwitness May 25 '17 at 10:57
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,601
- 2
- 28
- 28
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.

- 11
- 1
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.

- 95,783
- 15
- 211
- 370
-
It is not a document based App, It is an Email reader, and I would like to be able to drag drafts, mailboxes, etc. onto my dock icon. – Tristan Mar 22 '10 at 14:02