Detecting if a custom file icon was set in Cocoa

半腔热情 提交于 2020-01-05 10:30:58

问题


I wrote an app that sets a custom icon for some files, but as the creation of such an icon is quite expensive I'd like to test if a custom icon was already set before. With custom icon I mean an icon that isn't the default icon set by OS X. In particular, I can have different icons for different files having the same type.

I already tried checking [NSURL resourceValuesForKeys:[NSArray arrayWithObjects:NSURLCustomIconKey,NSURLEffectiveIconKey,nil] error:nil], but the object associated with NSURLEffectiveIconKey is always non-nil and NSURLCustomIconKey seems to be nil even if I call [NSURL setResourceValue:myNonNilImage forKey:NSURLCustomIconKey error:nil].

Calling [[NSWorkspace sharedWorkspace] setIcon:myImage forFile:myFilename options:0] by the way seems the only way to change the icon displayed in the Finder.


回答1:


NSURLCustomIconKey always returns nil because support for this key is not implemented. This essential bit of information is mentioned in the header but not in the NSURL documentation. Until it is supported one way to get this information is through deprecated File Manager methods:

- (BOOL)fileHasCustomIcon:(NSString *)path {
    FSRef ref;
    FSCatalogInfo info;

    if (FSPathMakeRef((const UInt8 *)[path fileSystemRepresentation], &ref, NULL) == noErr) {
        if (FSGetCatalogInfo(&ref, kFSCatInfoFinderInfo, &info, NULL, NULL, NULL) == noErr) {
            FileInfo *fileInfo = (FileInfo *)(&info.finderInfo);
            return (fileInfo->finderFlags & kHasCustomIcon) != 0;
        }
    }

    return NO;
}



回答2:


The documentation for NSWorkspace says that setIcon:forFile:options: returns YES if it was successful.

With that information you can simply switch a NSUserDefault boolean. Here some code I used:

if (![[NSUserDefaults standardUserDefaults] boolForKey:@"iconImageSaved"]) {
    if ([[NSWorkspace sharedWorkspace] setIcon:myImage forFile:myFilename options:0])
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"iconImageSaved"];
}

Hope this helps



来源:https://stackoverflow.com/questions/16017927/detecting-if-a-custom-file-icon-was-set-in-cocoa

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!