I’ve created an app that has a NSTableView which represents a series of files. From this, I want to be able to drag a row (i.e. a filename) from my NSTableView to Finder, and the file be created in that folder. However, the bit I can’t work out is that I need to modify the contents of the original file, before it gets copied to Finder.
I’ve added the following line so I can drag outside of my NSTableView:
[self.tableView setDraggingSourceOperationMask:NSDragOperationCopy forLocal:NO];
And I can get it to copy the actual file, provided I add a current files location to the pasteboard, using:
- (BOOL)tableView:(NSTableView *)aTableView writeRowsWithIndexes:(NSIndexSet *)rowIndexes toPasteboard:(NSPasteboard *)pboard {
[pboard declareTypes:[NSArray arrayWithObject:NSFilenamesPboardType] owner:nil];
NSMutableArray* dragfiles = [[NSMutableArray alloc] init];
NSString* file = [self.files objectAtIndex:row];
NSString* filepath = [[[self.pathControl URL] path] stringByAppendingPathComponent:file];
[dragfiles addObject:filepath];
[pboard setPropertyList:dragfiles forType: NSFilenamesPboardType];
[dragfiles release];
}
But, because I want to modify the contents of the file, I don’t want to put filepaths onto the pasteboard. I’ve tried using NSFileWrapper, but Finder doesnt seem to accept this as a valid format.
I’ve checked Google, and I’ve found several suggestions that you can create a temporary file and use that filepath. But, this feels ugly.
Is it possible to send data to Finder? Is there a way to solve this?
You will most likely want to use promise files, or
NSFilesPromisePboardTyperather thanNSFilenamesPboardType. (Note: the promise file methodsdragPromisedFilesOfTypes:fromRect:source:slideBack:event:andnamesOfPromisedFilesDroppedAtDestination:that that documentation talks about are the genericNSViewmethods.NSTableViewdefines more convenient methods that you’ll use instead of the generic ones. That said, that should still provide info on how promise file drags work).NSTableViewusestableView:namesOfPromisedFilesDroppedAtDestination:forDraggedRowsWithIndexes:, which is where you can do the processing of your files. In yourtableView:writeRowsWithIndexes:toPasteboard:method, you’ll declareNSFilesPromisePboardType, then set an array of filename extensions for the types of files you plan to write. The following is pseudo-code that outlines how you might approach it:Then in your names-of-promisedFiles method:
You should be able to calculate the original file path (not sure how you’re determining it, so I left it
nilin the code above) and the destination file path (using something like in the code above).