从POSIX路径字符串中获取Mac Applescript文件对象

时间:2012-06-13 04:59:58

标签: macos applescript

我有一个存储在名为tempfile.txt的文本文件中的文件列表。例如,tempfile.txt的内容如下:

/path/to/my/file1.txt
/path/to/my/file2.txt
/path/to/my/file3.txt

我想使用Applescript在Finder中更改这些文本文件的标签颜色,但无法获取实际的文件对象(而不是文件名和路径作为字符串)。以下是我到目前为止的情况:

-- some work has already been done to set tempfile to tempfile.txt

set files_to_update to paragraphs of (read tempfile) -- this works fine
repeat with myfile in files_to_update
    set selected_file to POSIX path of myfile -- this works fine
    set label index of selected_file to 1 -- trying to set this file to orange fails with error "A property can't go after this identifier"
end repeat

任何帮助?

2 个答案:

答案 0 :(得分:4)

set input to "/Users/username/Desktop/untitled folder/
/Users/username/Desktop/Untitled.txt"

repeat with f in paragraphs of input
    set f to POSIX file f
    tell application "Finder" to set label index of (f as alias) to 1
end repeat

使用POSIX file获取文本路径的文件对象。 (POSIX path of用于获取文件或别名的文本路径。)label index是Finder项的属性。

答案 1 :(得分:-1)

上面的答案是正确的,但我被一些简单的东西绊了一下,所以我想我会提到它:

当使用路径执行Unix-y时,通常会获得quoted form of POSIX路径,以避免路径中存在空格等时出现问题。但是as alias不理解引用的形式,事情就会出错。 (也就是说,会抛出错误。)

如果您有引用的路径,请冒险进入别名世界时删除引号:

set label index of ((text 2 thru -2 of f) as alias) to 1