如何使用Swift在OS X上阅读Finder图标(左侧源列表)

pat*_*mar 4 macos cocoa swift

我尝试阅读有关左侧源列表中的finder中显示的图标的信息.我已尝试NSFileManager使用以下选项

  • NSURLEffectiveIconKey icon read与finder中的图标不同
  • NSURLCustomIconKey - 返回零
  • NSURLThumbnailKey - 返回零
  • NSThumbnail1024x1024SizeKey - 返回零

我设法使用NSFileManager读取所有已安装的设备,但我不知道如何读取与设备连接的图标?也许有人有任何想法或暗示.

我也试过用

var image: NSImage = NSWorkspace.sharedWorkspace().iconForFile((url as! NSURL).path!)
Run Code Online (Sandbox Code Playgroud)

但它返回相同的图像 NSURLEffectiveIconKey

谢谢!

Ken*_*ses 7

首先,查询Finder侧边栏中显示哪些卷的正确方法是使用LSSharedFileList API.该API还提供了查询图标的方法:

LSSharedFileListRef list = LSSharedFileListCreate(NULL, kLSSharedFileListFavoriteVolumes, NULL);
UInt32 seed;
NSArray* items = CFBridgingRelease(LSSharedFileListCopySnapshot(list, &seed));
CFRelease(list);
for (id item in items)
{
    IconRef icon = LSSharedFileListItemCopyIconRef((__bridge LSSharedFileListItemRef)item);
    NSImage* image = [[NSImage alloc] initWithIconRef:icon];

    // Do something with this item and icon

    ReleaseIconRef(icon);
}
Run Code Online (Sandbox Code Playgroud)

您可以查询使用的物品等性能LSSharedFileListItemCopyDisplayName(),LSSharedFileListItemCopyResolvedURLLSSharedFileListItemCopyProperty().