获取 NSPasteboardTypeFileURL 的 UTI
Get UTI of NSPasteboardTypeFileURL
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
我收到错误消息,指出文件不存在。从粘贴板中拖出:
Printing description of error:
Error Domain=NSCocoaErrorDomain Code=260 "The file “id=6571367.12885025918” couldn’t be opened because there is no such
file." UserInfo={NSURL=file:/.file/id=6571367.12885025918 --
file:///Users/xxx/Library/Containers/com.xxx.imageviewfix/Data/,
NSFilePath=/Users/xxx/Library/Containers/com.xxx.imageviewfix/Data/file:/.file/id=6571367.12885025918,
NSUnderlyingError=0x600000c70d80 {Error Domain=NSPOSIXErrorDomain
Code=2 "No such file or directory"}}
获取NSURL对象的路径获取原始文件名。唯一缺少的是 NSFilenamesPboardType
曾经提供的文件路径数组。
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;
}
我迟到了这个问题,但我发现这个有效:
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.
}
与其他答案不同,删除多个文件适用于此方法。
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
我收到错误消息,指出文件不存在。从粘贴板中拖出:
Printing description of error: Error Domain=NSCocoaErrorDomain Code=260 "The file “id=6571367.12885025918” couldn’t be opened because there is no such file." UserInfo={NSURL=file:/.file/id=6571367.12885025918 -- file:///Users/xxx/Library/Containers/com.xxx.imageviewfix/Data/, NSFilePath=/Users/xxx/Library/Containers/com.xxx.imageviewfix/Data/file:/.file/id=6571367.12885025918, NSUnderlyingError=0x600000c70d80 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}
获取NSURL对象的路径获取原始文件名。唯一缺少的是 NSFilenamesPboardType
曾经提供的文件路径数组。
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;
}
我迟到了这个问题,但我发现这个有效:
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.
}
与其他答案不同,删除多个文件适用于此方法。