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

前端 未结 5 1682
太阳男子
太阳男子 2020-12-17 05:05

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

相关标签:
5条回答
  • 2020-12-17 05:52

    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
    
    0 讨论(0)
  • 2020-12-17 05:57

    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.

    0 讨论(0)
  • 2020-12-17 05:58

    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
    
    0 讨论(0)
  • 2020-12-17 06:07

    I think the NSURLLabelNumberKey values are:

    //  0 none, 1 grey, 2 green, 3 purple, 4 blue, 5 yellow, 6 red, 7 orange
    
    0 讨论(0)
  • 2020-12-17 06:08

    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!

    0 讨论(0)
提交回复
热议问题