While implementing drag&drop capability i was searching for a way to drag a file from a custom view without having any data to write to the current pasteboard. In fact the data has a name but no content at the moment of drag.
In iScrape I added the functionlity of dragging from the preview view to any directory in finder. When the user releases the mouse the data isn´t available yet but must be downloaded and written in the target location. Apple’s API offers a smart solution for this issue.
Main steps are:
1. Register your view as a dragging source by registering for every filetype it can provide to a drop destination. Since we don´t provide any certain data but files which will be created in future we must register for NSFilenamesPboardType.The best place to do this is init method:
[self registerForDraggedTypes:[NSArray arrayWithObjects:NSFilenamesPboardType, nil]];
2. implement two delegate methods which get called one after the other:
– (void)draggedImage:(NSImage *)anImage endedAt:(NSPoint)aPoint operation (NSDragOperation)operation;
3. Initiate a drag in mouseDown: method using dragPromisedFilesofType: fromRect: source: slideBack: event: where source is the object which implements the second method in 2.