How to get the Finder label color of a file ?
27/08/05 21:57
How to read that information from the file system ?
Here is a NSWorspace category that will give the label colours.
Note that if you plan to use these colours to draw text instead of rounded boxes, you may want to replace the [NSColor clearColor] by [NSColor blackColor]
@implementation NSWorkspace(FinderColors)
static BOOL sFinderColorsInited=NO ;
static NSColor* sFinderColors[8] = { nil, nil, nil, nil, nil, nil, nil, nil } ;
static void initFinderColors(void)
{
sFinderColors[0] = [NSColor clearColor] ;
sFinderColors[1] = [NSColor colorWithCalibratedRed:(168.0/255.0) green:(168.0/255.0) blue:(168.0/255.0) alpha:1.0] ; // gray
sFinderColors[2] = [NSColor colorWithCalibratedRed:(174.0/255.0) green:(216.0/255.0) blue:( 65.0/255.0) alpha:1.0] ; // green
sFinderColors[3] = [NSColor colorWithCalibratedRed:(194.0/255.0) green:(138.0/255.0) blue:(217.0/255.0) alpha:1.0] ; // purple
sFinderColors[4] = [NSColor colorWithCalibratedRed:( 92.0/255.0) green:(160.0/255.0) blue:(255.0/255.0) alpha:1.0] ; // blue
sFinderColors[5] = [NSColor colorWithCalibratedRed:(235.0/255.0) green:(219.0/255.0) blue:( 65.0/255.0) alpha:1.0] ; // yellow
sFinderColors[6] = [NSColor colorWithCalibratedRed:(252.0/255.0) green:( 99.0/255.0) blue:( 89.0/255.0) alpha:1.0] ; // red
sFinderColors[7] = [NSColor colorWithCalibratedRed:(246.0/255.0) green:(170.0/255.0) blue:( 62.0/255.0) alpha:1.0] ; // orange
sFinderColorsInited = YES ;
}
- (NSColor *)getFinderLabelColorOfPath:(NSString*)inPath
{
CFURLRef url;
FSRef fsRef;
BOOL ret;
FSCatalogInfo cinfo;
if (!sFinderColorsInited) {
initFinderColors() ;
}
url = CFURLCreateWithFileSystemPath(NULL, (CFStringRef)inPath, kCFURLPOSIXPathStyle, FALSE);
if (!url)
return 0;
ret = CFURLGetFSRef(url, &fsRef);
CFRelease(url);
if (ret && (FSGetCatalogInfo(&fsRef, kFSCatInfoFinderInfo, &cinfo, NULL, NULL, NULL) == noErr)) {
return sFinderColors[ (((FileInfo*)&cinfo.finderInfo)->finderFlags & kColor) >> kIsOnDesk ] ;
}
return nil;
}
@end
Note that if you plan to use these colours to draw text instead of rounded boxes, you may want to replace the [NSColor clearColor] by [NSColor blackColor]
@implementation NSWorkspace(FinderColors)
static BOOL sFinderColorsInited=NO ;
static NSColor* sFinderColors[8] = { nil, nil, nil, nil, nil, nil, nil, nil } ;
static void initFinderColors(void)
{
sFinderColors[0] = [NSColor clearColor] ;
sFinderColors[1] = [NSColor colorWithCalibratedRed:(168.0/255.0) green:(168.0/255.0) blue:(168.0/255.0) alpha:1.0] ; // gray
sFinderColors[2] = [NSColor colorWithCalibratedRed:(174.0/255.0) green:(216.0/255.0) blue:( 65.0/255.0) alpha:1.0] ; // green
sFinderColors[3] = [NSColor colorWithCalibratedRed:(194.0/255.0) green:(138.0/255.0) blue:(217.0/255.0) alpha:1.0] ; // purple
sFinderColors[4] = [NSColor colorWithCalibratedRed:( 92.0/255.0) green:(160.0/255.0) blue:(255.0/255.0) alpha:1.0] ; // blue
sFinderColors[5] = [NSColor colorWithCalibratedRed:(235.0/255.0) green:(219.0/255.0) blue:( 65.0/255.0) alpha:1.0] ; // yellow
sFinderColors[6] = [NSColor colorWithCalibratedRed:(252.0/255.0) green:( 99.0/255.0) blue:( 89.0/255.0) alpha:1.0] ; // red
sFinderColors[7] = [NSColor colorWithCalibratedRed:(246.0/255.0) green:(170.0/255.0) blue:( 62.0/255.0) alpha:1.0] ; // orange
sFinderColorsInited = YES ;
}
- (NSColor *)getFinderLabelColorOfPath:(NSString*)inPath
{
CFURLRef url;
FSRef fsRef;
BOOL ret;
FSCatalogInfo cinfo;
if (!sFinderColorsInited) {
initFinderColors() ;
}
url = CFURLCreateWithFileSystemPath(NULL, (CFStringRef)inPath, kCFURLPOSIXPathStyle, FALSE);
if (!url)
return 0;
ret = CFURLGetFSRef(url, &fsRef);
CFRelease(url);
if (ret && (FSGetCatalogInfo(&fsRef, kFSCatInfoFinderInfo, &cinfo, NULL, NULL, NULL) == noErr)) {
return sFinderColors[ (((FileInfo*)&cinfo.finderInfo)->finderFlags & kColor) >> kIsOnDesk ] ;
}
return nil;
}
@end