使用AppleScript将自定义标签设置为文件

时间:2018-01-31 16:02:18

标签: applescript finder

这是我得到的,但我有一个带有“Active Projects”的自定义标签。 我如何分配?

告诉应用程序“Finder”将文件的标签索引设置为5

1 个答案:

答案 0 :(得分:1)

你不能用香草AppleScript做到这一点。 Finder词典不支持添加标签。

但是,您可以使用AppleScriptObjC来访问Foundation框架

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

use framework "Foundation"

on addTagToPath(theTag, thePath)
    set theURL to current application's NSURL's fileURLWithPath:thePath
    set {success, tagArray, theError} to theURL's getResourceValue:(reference) forKey:(current application's NSURLTagNamesKey) |error|:(reference)
    if theError is not missing value then error theError's localizedDescription() as text
    if tagArray is not missing value and (tagArray's containsObject:theTag) as boolean is true then return
    if tagArray is missing value then set tagArray to current application's NSMutableArray's array()
    tagArray's addObject:theTag
    set {success, theError} to theURL's setResourceValue:tagArray forKey:(current application's NSURLTagNamesKey) |error|:(reference)
    if theError is not missing value then error theError's localizedDescription() as text
end addTagToPath

并使用它

try
    addTagToPath("MyTag", "/Users/myUser/path/to/file.ext")
on error e
    log e
end try

try阻止了NSURL方法

引发的错误