【问题标题】:Setting a custom label to a file with AppleScript [closed]使用 AppleScript 将自定义标签设置为文件 [关闭]
【发布时间】:2018-01-31 16:02:17
【问题描述】:

这是我得到的,但我有一个带有“活动项目”的自定义标签。 我该如何分配?

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

【问题讨论】:

标签: applescript finder


【解决方案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 方法抛出的错误

【讨论】:

  • 谢谢!我得到预期的“结束”,但发现“开始”。
  • 您使用的是哪个 OS X 版本?错误发生在哪一行?
  • 我的错,已修复!非常感谢!!!
猜你喜欢
  • 2011-09-09
  • 1970-01-01
  • 2019-11-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-07
  • 1970-01-01
  • 2022-12-03
  • 1970-01-01
相关资源
最近更新 更多