gpt4 book ai didi

objective-c - 使用 NSPasteboard 剪切和粘贴文件

转载 作者:行者123 更新时间:2023-12-03 17:41:56 31 4
gpt4 key购买 nike

我们应该如何使用NSPasteboard剪切和粘贴文件?目前我通过写入和读取文件 URL 来实现复制和粘贴。 cut 的问题是,在我将 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

关于objective-c - 使用 NSPasteboard 剪切和粘贴文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10727354/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com