【发布时间】:2018-01-31 16:02:17
【问题描述】:
这是我得到的,但我有一个带有“活动项目”的自定义标签。 我该如何分配?
告诉应用程序“Finder”将文件的标签索引设置为 5
【问题讨论】:
-
您使用的是哪个版本的 OS X/macO,是 tag 还是 label?
标签: applescript finder
这是我得到的,但我有一个带有“活动项目”的自定义标签。 我该如何分配?
告诉应用程序“Finder”将文件的标签索引设置为 5
【问题讨论】:
标签: applescript finder
你不能用普通的 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 方法抛出的错误
【讨论】: