简体   繁体   中英

How can I conditionally color files and folders in the OS X Finder?

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

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

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.

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 InputManager s. 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!

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

我认为NSURLLabelNumberKey值是:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM