检测是否在Cocoa中设置了自定义文件图标 [英] Detecting if a custom file icon was set in Cocoa

查看:118
本文介绍了检测是否在Cocoa中设置了自定义文件图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个应用程序,它为某些文件设置了自定义图标,但是由于创建这样的图标非常昂贵,因此我想测试一下是否已经设置了自定义图标。对于自定义图标,我的意思是不是OS X设置的默认图标。特别是,对于具有相同类型的不同文件,我可以具有不同的图标。

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.

我已经尝试检查 [NSURL resourceValuesForKeys:[NSArray arrayWithObjects:NSURLCustomIconKey,NSURLEffectiveIconKey,nil]错误:无] ,但是对象与 NSURLEffectiveIconKey 始终为非零,并且即使我调用 [NSURL setResourceValue:myNonNilImage forKey:NSURLCustomIconKey错误:nil,但 NSURLCustomIconKey 似乎为零。 ]

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].

调用 [[NSWorkspace sharedWorkspace] setIcon:myImage forFile:myFilename选项:0] 似乎是更改Finder中显示的图标的唯一方法。

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

推荐答案

NSURLCustomIconKey始终返回nil,因为支持该键未实现。标头中提到了这一重要信息,但NSURL文档中未提及。在获得支持之前,一种获取此信息的方法是通过不建议使用的文件管理器方法:

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

这篇关于检测是否在Cocoa中设置了自定义文件图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆