使用AppleScript更改Finder标签

时间:2014-07-06 20:15:45

标签: tags applescript finder

我想使用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?感谢。

1 个答案:

答案 0 :(得分:2)

这是一个路径问题,您必须将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