2014-07-06 66 views
0

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

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 

如果我有/Users/fort/bar.txt在Finder中选定的,我得到以下错误:

"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可以解析吗?谢谢。

+0

是确切的错误你是得到? – mcgrailm

+0

我错误地添加了引号,并且我的Mac HD(这是MBA)的名称应该被预置为该路径。显然,文件名是由它组成的。所以,确切的错误应该是这样的:'tag:文件“MBA/Users/fort/bar.txt”无法打开,因为没有这样的文件。“ – fort

回答

2

这是一个路径问题,就必须查找项目转换为字符串,并转换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 
+0

工作,谢谢@ jackjr300!唯一不起作用的是标签切换,但这并不重要,因为我制作了另一个删除所有标签的脚本。 – fort