【问题标题】:Changing Finder tags with AppleScript使用 AppleScript 更改 Finder 标签
【发布时间】:2014-07-06 20:15:45
【问题描述】:

我想使用 AppleScript 和 tag 为 Finder (OS X 10.9.4) 中的选定文件分配特定标签,但在将文件路径传递给标签时遇到问题。

tell application "Finder"
    try
        repeat with currentFile in items of (get selection)
            if label index of currentFile is 0 then
                do shell script ("/usr/local/bin/tag -a 'foo' " & currentFile)
            else
                set label index of currentFile to 0
            end if
        end repeat
    on error e
        return e
    end try
end tell

如果我在 Finder 中选择了/Users/fort/bar.txt,我会收到以下错误:

"tag: The file “/Users/fort/bar.txt” couldn’t be opened because there is no such file."

但是,以下代码确实将指定文件的标签更改为foo

set myFile to "/Users/fort/bar.txt" do shell script ("/usr/local/bin/tag -a 'foo' " & myFile)

知道为什么currentFile 没有以它可以解析的方式传递给tag 吗?谢谢。

堡垒

【问题讨论】:

  • 这是你得到的确切错误吗?
  • 我错误地添加了引号,并且我的 Mac HD 的名称(即 MBA)应该被添加到路径前。而且,显然,文件名是组成的。因此,确切的错误如下所示:tag: The file “MBA/Users/fort/bar.txt” couldn’t be opened because there is no such file.

标签: tags applescript finder


【解决方案1】:

是路径问题,必须将Finder项转成字符串,并将HFS路径转成posix路径

试试这个

tell application "Finder"
    repeat with currentFile in (get selection)
        tell currentFile
            if label index is 0 then
                my tagCmd(it as text) -- convert Finder item e.g. file "bar.txt" of folder "fort" of.... -->  "MBA:Users:fort:bar.txt” (path with colon)
            else
                set label index to 0
            end if
        end tell
    end repeat
end tell

on tagCmd(f)
    do shell script "/usr/local/bin/tag -a 'foo' " & quoted form of POSIX path of f -- posix path convert path with colon to use in shell
end tagCmd

【讨论】:

  • 成功了,谢谢@jackjr300!唯一不起作用的是标签切换,但这没关系,因为我制作了另一个删除所有标签的脚本。
猜你喜欢
  • 2013-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-13
相关资源
最近更新 更多