0

How are we supposed to cut & paste files using NSPasteboard? Currently I implemented copy and paste by writing and reading file URLs. The problem with cut is, that after I wrote the URL to the pasteboard, I have to remove the file. And when I try to paste the file it doesn't exist anymore and I can't copy it. Should I write something else onto the pasteboard? I also thought about copying the file to a temporary hidden location, but that seems to be a bit inefficient. Is there any other solution?

DrummerB
  • 39,814
  • 12
  • 105
  • 142
  • you can check this out (read comments here http://stackoverflow.com/questions/9409852/whats-the-best-strategy-for-implementing-cut-and-paste-in-cocoa ) how like that comment say cut is probably not available via the api. Is there any way to get confirmation after the copy is finished? because you could safely delete the file at that point – owen gerig May 23 '12 at 20:33
  • I've already read that and it provides no solution. – DrummerB May 23 '12 at 20:36

1 Answers1

2

You can use kPasteboardTypeFilePromiseContent. In this case, the dragging source is responsible to write the file to the destination, so you can move the file instead of duplicating it.

Docs from Pasteboard.h:

/*
 *  Pasteboard File Promising
 *  
 *  Summary:
 *    With the FSSpec type being deprecated and removed for 64 bit it is necessary
 *    to introduce a replacement for kDragFlavorTypePromiseHFS. The replacement comes
 *    in the form of two new Uniform Type Identifiers specifically for use with the
 *    pasteboard and promised files. Like the old HFS promise mechanism, the new UTI
 *    based method still requires a multistage handshake between sender and receiver
 *    but the process is somewhat simplified.
 *    
 *    Order of operations on copy or drag
 *    
 *    1) The sender promises kPasteboardTypeFileURLPromise for a file yet to be created.
 *    2) The sender adds kPasteboardTypeFilePromiseContent containing the UTI describing
 *          the file's content.
 *    
 *    Order of operations on paste or drop
 *    
 *    3) The receiver asks for kPasteboardTypeFilePromiseContent to decide if it wants the file.
 *    4) The receiver sets the paste location with PasteboardSetPasteLocation.
 *    5) The receiver asks for kPasteboardTypeFileURLPromise.
 *    6) The sender's promise callback for kPasteboardTypeFileURLPromise is called.
 *    7) The sender uses PasteboardCopyPasteLocation to retrieve the paste location, creates the file
 *          and keeps its kPasteboardTypeFileURLPromise promise.
 *
 *    Automatic translation support has been added so clients operating in the modern
 *    kPasteboardTypeFileURLPromise and kPasteboardTypeFilePromiseContent world can continue
 *    to communicate properly with clients using the traditional kDragFlavorTypePromiseHFS and
 *    kDragPromisedFlavor model.
 */

Sample:

@implementation NSPasteboard (DestinationFolder)

- (NSURL*)pasteLocation
{
    NSURL* fileURL = nil;
    PasteboardRef pboardRef = NULL;
    PasteboardCreate((CFStringRef)[self name], &pboardRef);
    if (pboardRef != NULL) {
        PasteboardSynchronize(pboardRef);
        PasteboardCopyPasteLocation(pboardRef, (CFURLRef*)&fileURL);
        CFRelease(pboardRef);
    }
    return [fileURL autorelease];
}

- (void)setPasteLocation:(NSURL *)url
{
    PasteboardRef pboardRef = NULL;
    PasteboardCreate((CFStringRef)[self name], &pboardRef);
    if (pboardRef != NULL) {
        PasteboardSynchronize(pboardRef);
        PasteboardSetPasteLocation(pboardRef, (CFURLRef)url);
        CFRelease(pboardRef);
    }
}

@end
catlan
  • 25,100
  • 8
  • 67
  • 78
Yoav
  • 5,962
  • 5
  • 39
  • 61