Re: Finder Icon Badging...
Re: Finder Icon Badging...
- Subject: Re: Finder Icon Badging...
- From: Rainer Brockerhoff <email@hidden>
- Date: Mon, 2 Feb 2004 22:28:12 -0200
At 16:01 -0800 02/02/2004, email@hidden wrote:
>
From: Peter Jaros <email@hidden>
>
Subject: Re: Finder Icon Badging...
>
Date: Mon, 2 Feb 2004 18:03:56 -0500
>
To: Christopher Pavicich <email@hidden>
>
>
On Feb 1, 2004, at 4:39 PM, Christopher Pavicich wrote:
>
>
> Hi List:
>
>
>
> I am working on an application that would be much more ~compelling~ if
>
> I were able to badge Icons in the Finder?
>
>
>
> Is this possible? If so, does anyone have some sample code or a
>
> resource to point me at?
Sorry, I missed this question first time around.
Peter, badging is a way of overlaying another icon over a file's or folders current icon. I use this to overlay a little "XRay" tag over .txt or .rtf reports generated by XRay.
Here's the code I use, it seems to work well. Of course this has little to do with Cocoa, you must do
#include <Carbon/Carbon.h>
This little function returns YES if the badging was successful. The first argument is the full path to the item which can be a file or folder. The second argument is the badge icon in NSData format. You could generate it by reading a .icns file into NSData.
If it's a file, it assumes there's no resource fork, and it will create one; if it's a folder or package it will generate the badge as a custom icon file, and assumes there is none at the outset.
Changing this to accept existing resource forks or custom icon files is left as an exercise to the reader ;-).
BOOL AddBadgeToItem(NSString* path,NSData* tag) {
FSCatalogInfo info;
FSRef par;
FSRef ref;
Boolean dir = false;
if (tag&&Jaguar&&(FSPathMakeRef([path fileSystemRepresentation],&par,&dir)==noErr)) {
HFSUniStr255 fork = {0,{0}};
SInt16 refnum = kResFileNotOpened;
FSGetResourceForkName(&fork);
if (dir) {
memset(&info,0,sizeof(info));
((FileInfo*)(&info.finderInfo))->finderFlags = kIsInvisible;
if (FSCreateResourceFile(&par,5,(UniChar*)"\000I\000c\000o\000n\000\r",kFSCatInfoFinderInfo,&info,fork.length,fork.unicode,&ref,NULL)!=noErr) {
return NO;
}
} else {
BlockMoveData(&par,&ref,sizeof(FSRef));
if (FSCreateResourceFork(&ref,fork.length,fork.unicode,0)!=noErr) {
return NO;
}
}
if (FSOpenResourceFile(&ref,fork.length,fork.unicode,fsRdWrPerm,&refnum)!=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;
}
Does this do what you want?
--
Rainer Brockerhoff <email@hidden>
Belo Horizonte, Brazil
"It's extremely unlucky to be superstitious, for no other reason
than it is always unlucky to be colossally stupid." (Stephen Fry)
Weblog:
http://www.brockerhoff.net/bb/viewtopic.php
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.