如何使用Cocoa对文件和文件夹进行徽章标记

6
我想要为文件和文件夹添加一些颜色(图像)标记。如何实现?
我尝试使用图标服务,它可以用于文件,但无法用于文件夹。
我看到Dropbox(10.4、10.5和10.6)可以实现这种行为——这是如何做到的?
博客文章Cocoa教程:自定义文件夹图标对我来说很接近,但它没有按预期工作。
除了图标服务,还有其他解决方案吗?

我找到了解决方案:在文件夹中添加一个标记图标的资源叉,并将其作为文件夹图标的标记加载。注意:资源叉的名称应以“\r”结尾。 - Girish Kolari
1
你介意添加一个答案并展示你所做的吗?我很感兴趣看看。谢谢! - Dave DeLong
@Dave DeLong,请查看我下面提供的解决方案,传递要添加徽章的文件夹路径和徽章图标的NSData。该代码仅在Intel中运行,如果您希望在PPC中运行,请使用与PPC相关的'NSUTF16LittleEndianStringEncoding'。希望您会发现这段代码有用。 - Girish Kolari
3个回答

4
以下函数是我为这个问题找到的解决方案。
BOOL AddBadgeToItem(NSString* path,NSData* tag)

{   
    FSCatalogInfo info;
    FSRef par;
    FSRef ref;
    Boolean dir = false;

    if (tag&&(FSPathMakeRef([path fileSystemRepresentation],&par,&dir)==noErr)) 
    {
        HFSUniStr255 fork = {0,{0}};
        sint16 refnum = kResFileNotOpened;
        FSGetResourceForkName(&fork);

        if (dir) 
        {

            NSString *name = @"Icon\r";
            memset(&info,0,sizeof(info));
            ((FileInfo*)(&info.finderInfo))->finderFlags = kIsInvisible;

            OSErr error = FSCreateResourceFile(&par,[name lengthOfBytesUsingEncoding:NSUTF16LittleEndianStringEncoding],(UniChar*)[name cStringUsingEncoding:NSUTF16LittleEndianStringEncoding],kFSCatInfoFinderXInfo,&info,fork.length, fork.unicode,&ref,NULL);

            if( error == dupFNErr )
            {
                // file already exists; prepare to try to open it
                const char *iconFileSystemPath = [[path stringByAppendingPathComponent:@"\000I\000c\000o\000n\000\r"] fileSystemRepresentation];

                OSStatus status = FSPathMakeRef((const UInt8 *)iconFileSystemPath, &ref, NULL);
                if (status != noErr)
                {
                    fprintf(stderr, "error: FSPathMakeRef() returned %d for file \"%s\"\n", (int)status, iconFileSystemPath);

                }
            }else if ( error != noErr)
            {
                return NO;
            }

        } 
        else 
        {
            BlockMoveData(&par,&ref,sizeof(FSRef));
            if (FSCreateResourceFork(&ref,fork.length,fork.unicode,0)!=noErr) 
            {
                //test

                if (FSOpenResourceFile(&ref,fork.length,fork.unicode,fsRdWrPerm,&refnum)!=noErr) {
                    return NO;
                }
                if (refnum!=kResFileNotOpened) {

                    UpdateResFile(refnum);
                    CloseResFile(refnum);

                    if (FSGetCatalogInfo(&par,kFSCatInfoFinderXInfo,&info,NULL,NULL,NULL)==noErr) {
                        ((ExtendedFileInfo*)(&info.extFinderInfo))->extendedFinderFlags = kExtendedFlagsAreInvalid;
                        FSSetCatalogInfo(&par,kFSCatInfoFinderXInfo,&info);
                    }
                }

                //Test end
                return NO;
            }
        }
        OSErr errorr = FSOpenResourceFile(&ref,fork.length,fork.unicode,fsRdWrPerm,&refnum);
        if (errorr!=noErr) {
            return NO;
        }
        if (refnum!=kResFileNotOpened) {
            CustomBadgeResource* cbr;

            int len = [tag length]; 
            Handle h = NewHandle(len);
            if (h) {
                BlockMoveData([tag bytes],*h,len);
                AddResource(h,kIconFamilyType,128,"\p");
                WriteResource(h);
                ReleaseResource(h);
            }

            h = NewHandle(sizeof(CustomBadgeResource)); 
            if (h) {
                cbr = (CustomBadgeResource*)*h;
                memset(cbr,0,sizeof(CustomBadgeResource));
                cbr->version = kCustomBadgeResourceVersion;
                cbr->customBadgeResourceID = 128;
                AddResource(h,kCustomBadgeResourceType,kCustomBadgeResourceID,"\p");
                WriteResource(h);
                ReleaseResource(h);
            }

            UpdateResFile(refnum);
            CloseResFile(refnum);

            if (FSGetCatalogInfo(&par,kFSCatInfoFinderXInfo,&info,NULL,NULL,NULL)==noErr) {
                ((ExtendedFileInfo*)(&info.extFinderInfo))->extendedFinderFlags = kExtendedFlagHasCustomBadge;
                FSSetCatalogInfo(&par,kFSCatInfoFinderXInfo,&info);
            }
        }
    }
    return NO;
}

1
嗨Girish,我正在尝试在Finder中使用附件图标徽章来标记文件夹,但是到目前为止没有成功。我尝试了这段代码,但结果是文件夹中出现了一个名为“Icon”的文件。你有什么想法我可能做错了什么吗?谢谢! - Joe Ricioppo
@Joe Ricioppo:图标文件资源叉创建为徽章文件夹的一部分。 - Girish Kolari
@JoeRicioppo 我也遇到了同样的问题。代码运行时没有任何错误,但是没有任何效果。你是如何解决这个问题的??? - ping localhost
还对此感到好奇吗?你们弄清楚它是如何工作的了吗? - Alfian Busyro

0

预期的方法是创建一个Finder Sync应用扩展。


0
你可以使用NSWorkspace中的-setIcon:forFile:options:方法来实现此功能,该方法让您只需指定一个NSImage即可将其应用于您提供的路径下的文件/文件夹。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接