2010-05-06 28 views
3

我想要的顏色的徽章文件,並根據取景器中的某些條件的文件夾,什麼是在Mac OS X實現這一目標的方法10.6如何在OS X Finder中有條件地着色文件和文件夾?

我已經檢查了這個問題:在取景器 這只是說說上下文菜單Finder Plugin in Snow Leopard

我甚至檢查過:http://scplugin.tigris.org/即使他們沒有在10.6中做彩色徽章,這是未決任務。

在此先感謝您的幫助

+0

對於投票結束的人,我相信用戶正在嘗試以編程方式執行此操作,所以這不是真正的superuser.com問題。問題不明確,但它被標記爲「可可」。 – 2010-05-06 23:35:56

+0

我使用圖標服務得到了一個解決方案。將顏色作爲圖標和徽章圖標加載到相應的文件或文件夾。 感謝您的幫助。 – 2010-07-14 13:16:31

回答

1

不幸的是,沒有公開的API。您需要在Finder中注入代碼並對其進行修補。

在10.6之前,通過僅使用InputManager s就可以很容易地將代碼注入Cocoa應用程序。這不再是事實,但您可以使用OSAX來實現,請參閱this blog postSIMBL自動執行此操作。

但你必須弄清楚Finder裏面發生了什麼,看看如何修補東西。 要探索Finder裏面,F-Script anywhere將幫助你。

玩得開心,祝你好運!

+0

10.6及更高版本中有一個公共API,請參閱我的答案。使用AppleScript是早期操作系統版本的解決方案。 – 2010-05-07 00:54:11

+1

Rob Keniger:有一個公共API來設置來自另一個應用程序的標籤顏色,但如果目標是讓Finder自己完成工作,則需要SIMBL或mach_inject將代碼注入Finder進程。 – 2010-05-07 00:58:15

+0

感謝羅布,學習新的API總是很高興。我想我需要更仔細地閱讀API變化... – Yuji 2010-05-07 01:22:41

1

你需要applescript。因此,您可以使用腳本橋或NSApplescript在可可中編寫Finder腳本。這裏有一個簡單的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 
10

您可以使用在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]); 
} 

您可以同時使用NSURLLabelNumberKey得到Finder的分配標籤或NSURLLabelColorKey的數量來獲得實際的顏色。

您可以通過使用相應的方法來設置標籤值:

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

這不適用於10.9中引入的新標籤,但是,是嗎?我的意思是添加或刪除任何數量的新自定義標籤,因爲它可能在Finder中。畢竟,LabelValue只是舊FinderLabel的索引。 – 2015-01-20 18:36:14

+3

@ThomasTempelmann你可以使用'NSURLTagNamesKey'鍵。 – 2015-01-21 01:06:43

0

我覺得NSURLLabelNumberKey值:

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

任何人尚需一個答案在這裏你去。

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 

蓋瑞特海德有正確的順序。

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

上述代碼已經使用Xcode 4.6.3和OSX 10.9.2 Mavericks進行了測試。

相關問題