2013-11-05 32 views
4

我想從文件夾的所有內容遞歸刪除所有標籤(OS X 10.9的新功能之一)。由於文件夾中有很多文件(以及包含更多文件的文件夾),我想嘗試使用Applescript來簡化過程。我在網上查找並沒有發現任何有用的東西。用蘋果腳本修改文件標籤

此外,我無法找到Finder或Standard Additions字典中的任何內容,它可以幫助我。

可能的東西是這樣的:

set folder to "folder_path" 
set files to (all files of folder) 
for each file: 
    check for tag (optional) 
    remove all tags from file 

PS。上面的代碼應該是腳本的指導,而不是確切的代碼。

回答

2

這將遞歸從文件夾中的文件刪除所有標籤:

set targetFolder to POSIX path of (choose folder with prompt "Remove all tags from this folder..." default location path to desktop) 

do shell script "xattr -rd com.apple.metadata:_kMDItemUserTags " & quoted form of targetFolder 
+0

使用'mdls〜/ Desktop/Folder/test.rtf'時,可以看到'kMDItemUserTags'屬性,但是當我嘗試使用'xattr -rd com.apple.metadata ...'時,出現錯誤說'沒有這樣的xattr:com.apple.metatdata:_kMDItemUserTags' – Hacker

+0

你能發佈一個示例文件嗎? – adayzdone

0

我想你所遇到的問題是,你的文件實際上並沒有對他們的標籤 - 他們可能只是有標籤。

雖然小牛隊中的新標籤系統是通過將標籤顯示爲標籤來構建舊標籤系統,但在小牛隊之前的Mac OS X版本中標記爲黃色的文件實際上可能沒有適當的標籤。他們只有老牌的標籤,小牛表現爲標籤。如果文件上的標籤是黃色和藍色的標籤,那麼這些標籤可能就是標籤。這可能是你試圖用xattr刪除它們時看到錯誤的原因。

因此,如果您的文件只有標籤,刪除標籤的方式與您仍在運行Mountain Lion的方式相同。您要求Finder將文件的標籤索引設置爲0.

此AppleScript會要求您選擇一個文件夾,然後循環該文件夾中的所有文件,並且如果文件上有標籤,則標籤已移除。

tell application "Finder" 
    activate 
    set theFolder to (choose folder with prompt "Choose a folder to remove labels from the files within:") 
    set theFiles to every file of theFolder 
    repeat with theFile in theFiles 
     if the label index of theFile is not equal to 0 then 
      set the label index of theFile to 0 
     end if 
    end repeat 
    open theFolder 
end tell 

需要明確的是:上述的AppleScript只刪除已如果您手動標記在小牛的文件通過開放Mac OS X的,因爲之前存在的7個標準標記/標籤顏色獲取的信息窗口,鍵入項目名稱或類似的東西作爲標籤,那麼該標籤將不得不通過shell腳本刪除,如上面adayzdone的回覆中所述。