0

を使用してファイルをカット アンド ペーストするにはどうすればよいNSPasteboardですか? 現在、ファイル URL の書き込みと読み取りによるコピー アンド ペーストを実装しています。カットの問題は、URL をペーストボードに書き込んだ後で、ファイルを削除しなければならないことです。ファイルを貼り付けようとすると、もう存在せず、コピーできません。ペーストボードに何か他のものを書く必要がありますか? ファイルを一時的な隠し場所にコピーすることも考えましたが、それは少し効率が悪いようです。他の解決策はありますか?

4

1 に答える 1

2

使用できますkPasteboardTypeFilePromiseContent。この場合、ドラッグ元がファイルを宛先に書き込む責任があるため、ファイルを複製する代わりに移動できます。

からのドキュメント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.
 */

サンプル:

@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
于 2013-09-01T19:14:27.610 に答える