![](/img/trans.png)
[英]How to use NSPasteboard with kPasteboardTypeFileURLPromise for copy/paste?
[英]Cut & paste of files with NSPasteboard
我們如何使用NSPasteboard
剪切和粘貼文件? 目前,我通過編寫和讀取文件URL實現了復制和粘貼。 切割的問題是,在我將URL寫入粘貼板后,我必須刪除該文件。 當我嘗試粘貼文件時,它不再存在,我無法復制它。 我應該在粘貼板上寫一些其他內容嗎? 我還想過將文件復制到臨時隱藏位置,但這似乎有點低效。 還有其他解決方案嗎?
您可以使用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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.