views:

266

answers:

3

I want to color badge files and folders based on the some condition in finder, what is the approach to achieve this in Mac OS X 10.6

I have checked this question: This only talk about the context menu in finder http://stackoverflow.com/questions/1651075/finder-plugin-in-snow-leopard

I have even checked: http://scplugin.tigris.org/ even they don't do color badging in 10.6 which is pending task.

Thanks in advance for your all help

A: 

Unfortunately there is no public API for that. You need to inject the code inside Finder and patch it.

Before 10.6, it was quite easy to inject codes into Cocoa app by just using InputManagers. This is no longer true but you can do that using OSAX, see this blog post. SIMBL does that automatically.

But you have to figure out what's going on inside Finder to see how to patch things. To explore the inside of Finder, F-Script anywhere will help you.

Have fun and good luck!

Yuji
There is a public API in 10.6 and later, see my answer. Using AppleScript is the solution for earlier OS versions.
Rob Keniger
Rob Keniger: There is a public API to set the label color from another application, but one would need SIMBL or mach_inject to inject code into the Finder process if the goal is to make Finder do the job itself.
Peter Hosey
Thanks Rob, it's always a pleasure to learn new APIs. I guess I need to read API deltas more carefully...
Yuji
@Peter: I guess a background app which checks Finder's frontmost window regularly and watches the directory by `FSEvents` would do the job...
Yuji
+1  A: 

You need applescript. So you can use the scripting bridge or NSApplescript to script the Finder in cocoa. Here's a simple applescript to show how to do it.

set a to (choose file)
tell application "Finder"
    -- label colors
    -- 0 none, 1 orange, 2 red, 3 yellow, 4 blue, 5 purple, 6 green, 7 grey
    set label index of a to 6
end tell
regulus6633
+2  A: 

You can use the URL Resource API, which was introduced in Mac OS X 10.6.

NSURL* fileURL = [NSURL fileURLWithPath:@"/Path/to/file"];

id labelValue = nil;
NSError* error;
if([fileURL getResourceValue:&labelValue forKey:NSURLLabelNumberKey error:&error])
{
    NSLog(@"The label value is %@",labelValue);
}
else
{
    NSLog(@"An error occurred: %@",[error localizedDescription]);
}

You can use both the NSURLLabelNumberKey to get the number of the Finder's assigned label or the NSURLLabelColorKey to get the actual color.

You can set the label values by using the corresponding method:

- (BOOL)setResourceValue:(id)value forKey:(NSString *)key error:(NSError **)error
Rob Keniger