我如何在OS X Finder中有条件的颜色文件和文件夹?

时间:2022-10-05 21:17:56

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

我想根据finder中的某些条件给badge文件和文件夹上色,如何在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.

我甚至检查过:http://scplugin.tigris.org/甚至他们在10.6中都没有进行颜色标记,这是一个悬而未决的任务。

Thanks in advance for your all help

预先感谢您的一切帮助。

5 个解决方案

#1


1  

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

不幸的是,没有公共API来实现这一点。您需要在Finder中注入代码并对其进行补丁。

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.

在10.6之前,只需使用InputManagers就可以很容易地将代码注入到Cocoa应用程序中。这不再是事实,但你可以使用OSAX,看看这个博客文章。自动SIMBL呢。

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.

但是你必须弄清楚在Finder里面发生了什么才能知道如何修补程序。要探索Finder的内部,F-Script anywhere将帮助您。

Have fun and good luck!

祝你玩得开心,好运!

#2


10  

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

您可以使用Mac OS X 10.6中引入的URL资源API。

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.

您可以使用NSURLLabelNumberKey获取查找者指定的标签的数量,也可以使用NSURLLabelColorKey获取实际的颜色。

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

您可以使用相应的方法设置标签值:

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

#3


2  

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.

上面的代码已经使用Xcode 4.6.3和OSX 10.9.2 Mavericks进行了测试。

#4


1  

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.

你需要applescript。因此,您可以使用脚本桥接程序或NSApplescript在cocoa中编写查找程序脚本。下面是一个简单的applescript示例,演示了如何实现它。

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

#5


0  

I think the NSURLLabelNumberKey values are:

我认为NSURLLabelNumberKey值是:

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

#1


1  

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

不幸的是,没有公共API来实现这一点。您需要在Finder中注入代码并对其进行补丁。

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.

在10.6之前,只需使用InputManagers就可以很容易地将代码注入到Cocoa应用程序中。这不再是事实,但你可以使用OSAX,看看这个博客文章。自动SIMBL呢。

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.

但是你必须弄清楚在Finder里面发生了什么才能知道如何修补程序。要探索Finder的内部,F-Script anywhere将帮助您。

Have fun and good luck!

祝你玩得开心,好运!

#2


10  

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

您可以使用Mac OS X 10.6中引入的URL资源API。

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.

您可以使用NSURLLabelNumberKey获取查找者指定的标签的数量,也可以使用NSURLLabelColorKey获取实际的颜色。

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

您可以使用相应的方法设置标签值:

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

#3


2  

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.

上面的代码已经使用Xcode 4.6.3和OSX 10.9.2 Mavericks进行了测试。

#4


1  

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.

你需要applescript。因此,您可以使用脚本桥接程序或NSApplescript在cocoa中编写查找程序脚本。下面是一个简单的applescript示例,演示了如何实现它。

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

#5


0  

I think the NSURLLabelNumberKey values are:

我认为NSURLLabelNumberKey值是:

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