Search code examples
macosfinder

How to obtain IconRef from Application Resources


I want to add custom place to Finder Favorites sidebar using LSSharedFileList API as described here. But I need to add a custom icon to this item. The icon should be in the IconRef type. I have not found a way to do it. Found only a mention of the fact that the need to use Icon Services. But how?


Solution

  • You can use this function of Scplugin.

    static IconRef IconRefFromIconFileNoCache(CFStringRef file)
    {
        IconRef iconRef;
        IconFamilyHandle iconFamily;
        FSRef fileRef;
        CFURLRef url;
        Boolean ok;
    
        url = CFURLCreateWithFileSystemPath(NULL,file,kCFURLPOSIXPathStyle,false);
        if (!url)
            return 0;
    
        ok = CFURLGetFSRef(url,&fileRef);
        CFRelease(url);
        if (!ok)
            return 0;
    
    
        if (ReadIconFromFSRef(&fileRef, &iconFamily))
            return nil;
    
        iconRef = 0;
        HLock((Handle)iconFamily);
        GetIconRefFromIconFamilyPtr(*iconFamily,(**iconFamily).resourceSize,&iconRef);
        DisposeHandle((Handle)iconFamily);
    
        return iconRef;
    }