获取NSPasteboardTypeFileURL

时间:2019-05-18 12:44:16

标签: cocoa uti nspasteboard nsworkspace

NSFilenamesPboardType已于10.14弃用,建议使用NSPasteboardTypeFileURL。这给我的文件名是

  

file:///..file/id=6571367.12885025918

NSWorkspace无法从其中获取UTI。

如何从NSPasteboardTypeFileURL获取UTI?

旧代码:

- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
{
    NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
    NSDragOperation resultingDragMask = NSDragOperationNone;
    NSPasteboard *pboard = [sender draggingPasteboard];

    if ([[pboard types] containsObject:NSFilenamesPboardType]) {
        NSArray <NSString *>*list = [pboard propertyListForType:NSFilenamesPboardType];
        NSString *firstItem = [list objectAtIndexedSubscript:0];
        NSError *error;
        NSString *UTI = [[NSWorkspace sharedWorkspace] typeOfFile:firstItem error:&error];
        if (!error) {
            if ([[NSImage imageTypes] containsObject:UTI]) {
                resultingDragMask = NSDragOperationCopy;
            }
        }

    } else if ([[pboard types] containsObject:NSPasteboardTypeTIFF]) {
        resultingDragMask = NSDragOperationCopy;
    }

    if (sourceDragMask & NSDragOperationCopy && resultingDragMask & NSDragOperationCopy) {
        return NSDragOperationCopy;
    }
    return NSDragOperationNone;
}

使用NSPasteboardTypeFileURL时出现错误,指出状态文件不存在。从粘贴板拖拽:

  

打印错误说明:   错误域= NSCocoaErrorDomain代码= 260“文件“ id = 6571367.12885025918”无法打开,因为没有这样的文件   文件。” UserInfo = {NSURL = file:/。file / id = 6571367.12885025918-   文件:///Users/xxx/Library/Containers/com.xxx.imageviewfix/Data/,   NSFilePath = /用户/ xxx /库/容器/com.xxx.imageviewfix/数据/文件:/。file / id = 6571367.12885025918,   NSUnderlyingError = 0x600000c70d80 {Error Domain = NSPOSIXErrorDomain   代码= 2“没有这样的文件或目录”}}

2 个答案:

答案 0 :(得分:1)

这个问题我来晚了,但是我发现这可行:

  NSArray<Class> *classes = @[[NSURL class]];
  NSDictionary *options = @{};
  NSArray<NSURL*> *files = [pboard readObjectsForClasses:classes options:options];

  for (NSURL *url in files)
  {
     NSString *str = [url path];

     // TODO: do something with str.
  }

与其他答案不同,使用此方法可以删除多个文件。

答案 1 :(得分:0)

获取NSURL对象的路径将获取原始文件名。唯一缺少的是NSString *fileURL = [[NSURL URLFromPasteboard:pboard] path]; 曾经提供的文件路径数组。

- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
{
    NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
    NSDragOperation resultingDragMask = NSDragOperationNone;
    NSPasteboard *pboard = [sender draggingPasteboard];

    if ([[pboard types] containsObject:NSPasteboardTypeFileURL]) {
        NSString *fileURL = [[NSURL URLFromPasteboard:pboard] path];
        NSError *error;
        NSString *UTI = [[NSWorkspace sharedWorkspace] typeOfFile:fileURL error:&error];
        if (!error) {
            if ([[NSImage imageTypes] containsObject:UTI]) {
                resultingDragMask = NSDragOperationCopy;
            }
        }

    } else if ([[pboard types] containsObject:NSPasteboardTypeTIFF]) {
        resultingDragMask = NSDragOperationCopy;
    }

    if (sourceDragMask & NSDragOperationCopy && resultingDragMask & NSDragOperationCopy) {
        return NSDragOperationCopy;
    }
    return NSDragOperationNone;
}

更新的代码:

fa6Q´ŠÀ‹‚«%b&ƒµ,{˜›î›öU½êîêîšQ÷¸4VÏü¾ƒ]SÕ]ÕÝRªêê©wî  €
àS  °|
  v€O Àð)  Ø>  ;À§