如何徽章文件和文件夹使用可可 [英] How to badge file and folder using cocoa

查看:121
本文介绍了如何徽章文件和文件夹使用可可的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用一些颜色(图像)徽章文件和文件夹,任何想法如何可以实现
我试图与图标服务,它工作的文件,但它不工作的文件夹。



我看到此行为正常工作 Dropbox (10.4,10.5和10.6 ) - 任何想法如何可以做到这一点?



以下是非常接近一个为我,但它不能正常工作。
http://www.cimgf .com / 2008/06/16 / cocoa-tutorial-custom-folder-icons /



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

我很感激任何帮助。

解决方案

以下函数是我为问题找到的解决方案

  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)
{
//文件已存在;准备尝试打开它
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())返回%d的文件\%s\\\\
)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);
}
}

//测试结束
return NO;
}
}
OSErr errorr = FSOpenResourceFile(& ref,fork.length,fork.unicode,fsRdWrPerm,& refnum);
if(errorr!= noErr){
return NO;
}
如果(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;
}


I want to badge file and folder with some color(image), any idea how it can be achieved I tried with icon service, it work for files but it is not working with folders.

I saw this behavior working Dropbox (10.4, 10.5 and 10.6)- any idea how can this be done?

The following was very close one for me, but it is not working as expected. http://www.cimgf.com/2008/06/16/cocoa-tutorial-custom-folder-icons/

Is there any other solution other then icon service?

I appreciate any kind of help.

解决方案

The following function is the solution I found for the problem

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

这篇关于如何徽章文件和文件夹使用可可的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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