Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read Finder icons (left source list) on OS X using Swift

Tags:

macos

swift

cocoa

I try to read information about icons that are shown in finder on left source list. I tried already NSFileManager with following options

  • NSURLEffectiveIconKey icon read is not the same as in finder
  • NSURLCustomIconKey - returns nil
  • NSURLThumbnailKey - returns nil
  • NSThumbnail1024x1024SizeKey - returns nil

I managed to read all mounted devices using NSFileManager but I have no clue how to read icons connected with devices? Maybe someone has any idea or a hint.

I also tried to use

var image: NSImage = NSWorkspace.sharedWorkspace().iconForFile((url as! NSURL).path!)

but it returns the same image as NSURLEffectiveIconKey

Thanks!

like image 505
patmar Avatar asked Dec 24 '22 16:12

patmar


1 Answers

First, the proper way to query which volumes are shown in the Finder's sidebar is using the LSSharedFileList API. That API also provides a way to query the icon:

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);
}

You can query other properties of the items using LSSharedFileListItemCopyDisplayName(), LSSharedFileListItemCopyResolvedURL, and LSSharedFileListItemCopyProperty().

like image 151
Ken Thomases Avatar answered Dec 27 '22 06:12

Ken Thomases