Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get path of dropped file in cocoa application

I wondering how you would retrieve the path of the file that a user has dragged and dropped into a cocoa application. For example: User drags a file named test from his/her desktop. Then the cocoa application would say: Users/currentusername/Desktop/test

Thanks for the help!


1 Answers

I just downloaded Apple's "CocoaDragAndDrop" sample code and tried it out.

When I drag in a PNG file from the Finder into the running app, the title of the window changes to the path of the image that was dragged in.

Looking inside the sample code, I can see a file URL is included in the Pasteboard:

 //if the drag comes from a file, set the window title to the filename
 fileURL=[NSURL URLFromPasteboard: [sender draggingPasteboard]];
 [[self window] setTitle: fileURL!=NULL ? [fileURL absoluteString] : @"(no name)"];

Try this technique in your own code and modify it for taste.

like image 181
Michael Dautermann Avatar answered Sep 19 '25 19:09

Michael Dautermann