繁体   English   中英

如何使用Cocoa标记文件和文件夹

[英]How to badge file and folder using Cocoa

我想用一些颜色(图像)标记文件和文件夹。 怎么能实现这一目标?

我尝试使用图标服务,它适用于文件,但它不适用于文件夹。

我看到这种行为正在使用Dropbox (10.4,10.5和10.6) - 如何做到这一点?

博客文章Cocoa Tutorial:Custom Folder Icons对我来说非常接近,但它没有按预期工作。

除了图标服务之外还有其他解决方案吗?

以下功能是我找到的解决问题的方法

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

您可以使用-setIcon:forFile:options:方法执行此操作,该方法允许您在指定路径中指定要应用于文件/文件夹的NSImage。

预期的方法是创建Finder Sync应用程序扩展

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM