NSPasteboard:将承诺的文件数据复制到其他沙盒应用程序

时间:2019-01-04 11:43:26

标签: macos cocoa

我的沙盒Mac应用程序将图像数据作为“ public.jpeg”数据以及kPasteboardTypeFilePromiseContent放置到粘贴板上。

用于承诺文件的路径在我的沙盒应用程序的Container文件夹中的某个位置。

这非常有用,它允许拖动到Finder,但似乎会引起其他沙盒应用程序的问题,这些应用程序将承诺的文件数据优先于图像数据,例如苹果的Pages应用。

我找不到在哪里写承诺文件的文档,以便其他沙盒应用程序可以访问它。欢迎任何提示。

NSURL *url = [NSURL fileURLWithPath:self.libraryImage.filePath];
if (url)
{
     [pasteboardItem setDataProvider:dataProvider
                                   forTypes:@[(NSString *)kPasteboardTypeFilePromiseContent]];
}

更新:

我使用发布在这里的NSPasteBoard扩展程序来工作了:https://stackoverflow.com/a/18561956/581784

唯一缺少的是将文件写入放置在用户桌面上特定位置的位置。我当前的代码如下:

- (void)pasteboard:(nullable NSPasteboard *)pasteboard item:(NSPasteboardItem *)item provideDataForType:(NSString* /* NSPasteboardType */)type
{
    if ([type isEqualToString:(NSString *)kPasteboardTypeFileURLPromise])
    {
        NSURL *pasteURL = [pasteboard pasteLocation];
        if (pasteURL) {
            NSString        *listingImageUUID = [item stringForType:kGSListingImageUUIDPasteboardType];
            GSListingImage  *listingImage =  (GSListingImage*) [[[GSAppDelegate appDelegate] mainDatabaseContext] objectWithUUID:listingImageUUID ofClass:GSListingImage.class];
            NSString        *imageName = [listingImage.libraryImage.filePath lastPathComponent];
            NSURL           *destURL = [pasteURL URLByAppendingPathComponent:imageName];

            [[listingImage.libraryImage jpegData] writeToURL:destURL
                                                  atomically:YES];

            [pasteboard setPasteLocation:destURL];
        }
    }
}

1 个答案:

答案 0 :(得分:1)

  

我找不到在哪里写承诺文件的文档,所以其他   沙盒应用程序可以访问它

这不是它的工作方式。放置目标应用程序首先检查拖动类型是否包含NSFilesPromisePboardType

如果是,则目标应用使用-[NSDraggingInfo namesOfPromisedFilesDroppedAtDestination:]PasteboardSetPasteLocation()设置位置。

然后目标应用程序读取kPasteboardTypeFileURLPromise,这将触发承诺的文件提供程序应用程序将其写入目标应用程序指定的位置。

您需要在- (void)pasteboard:(nullable NSPasteboard *)pasteboard item:(NSPasteboardItem *)item provideDataForType:(NSPasteboardType)type;中进行此操作。

相关问题