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

Community
  • 1
  • 1
Girish Kolari
  • 2,515
  • 2
  • 24
  • 34
  • To the person that voted to close, I believe the user is trying to do this programmatically, so it's not really a superuser.com question. The question is ambiguous but it is tagged "cocoa". – Rob Keniger May 06 '10 at 23:35
  • I got a solution for this using icon services. load the color as a icon and badge icon to the respective file or folder. Thanks for all help. – Girish Kolari Jul 14 '10 at 13:16

5 Answers5

11

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
  • 45,830
  • 6
  • 101
  • 134
  • This doesn't work with the new Tags introduced in 10.9, though, does it? I mean for adding or removing any amount of new custom Tags, as it's possible in the Finder. The LabelValue is only the index for the old FinderLabel, after all. – Thomas Tempelmann Jan 20 '15 at 18:36
  • 3
    @ThomasTempelmann you can use the `NSURLTagNamesKey` key for that. – Rob Keniger Jan 21 '15 at 01:06
3

For anybody still needing an answer to this here you go.

NSURL *fileURL = [NSURL fileURLWithPath:path_to_file];
NSError *error;
id labelColor = nil;

[fileURL setResourceValue:@2 forKey:NSURLLabelNumberKey error:&error]; //Set tag/label to green
[fileURL setResourceValue:@6 forKey:NSURLLabelNumberKey error:&error]; //Set tag/label to red

Garrett Hyde has the correct order.

//  0 none, 1 grey, 2 green, 3 purple, 4 blue, 5 yellow, 6 red, 7 orange

The above code has been tested using Xcode 4.6.3 and OSX 10.9.2 Mavericks.

Andy
  • 61
  • 5
0

I think the NSURLLabelNumberKey values are:

//  0 none, 1 grey, 2 green, 3 purple, 4 blue, 5 yellow, 6 red, 7 orange
Garrett Hyde
  • 5,409
  • 8
  • 49
  • 55
0

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
  • 34,103
  • 3
  • 70
  • 88
  • 1
    There is a public API in 10.6 and later, see my answer. Using AppleScript is the solution for earlier OS versions. – Rob Keniger May 07 '10 at 00:54
  • 1
    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 May 07 '10 at 00:58
  • Thanks Rob, it's always a pleasure to learn new APIs. I guess I need to read API deltas more carefully... – Yuji May 07 '10 at 01:22
  • @Peter: I guess a background app which checks Finder's frontmost window regularly and watches the directory by `FSEvents` would do the job... – Yuji May 07 '10 at 01:24
  • thanks for the help ... as a experiment I am trying to do this using SIMBL ... is there half done solution which I can have a look? – Girish Kolari Sep 06 '11 at 08:20
  • As mentioned by others - the answer is incorrect. And it was incorrect already when answered many years ago :-) Not only there is and was already public API to manage labels on Finder items but in case one insists on making Finder do the labelling (why?) then it could be done with much more compliant and future proof methods. – silverdr Jan 31 '21 at 23:05
0

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
  • 18,848
  • 5
  • 41
  • 49