Applpescript:读取文件列表的文本文件

Applpescript: Reading Text file for List of Files

我一直在尝试拼凑一个脚本以从文本文档中获取文件列表,并让 applescript 逐行浏览列表,然后对文件进行处理。在这种情况下,它正在更改文件上的标签,但我们会在其他情况下使用它来移动文件等。

它与我桌面上的一个测试文件一起工作,文件被标记为紫色标签,但是当我尝试 运行 它在我实际需要的文件夹中时,它失败并显示此错误消息:

error "Finder got an error: Can’t set 1 to 5." number -10006 from 1

除了内容的长度外,文本文件是相同的。

这会不会是文件名的问题,如果是的话,我该如何使脚本更宽容。

这是脚本:

    set textFileContents to POSIX path of (choose file)
set dataList to paragraphs of (read textFileContents as «class utf8»)
tell application "System Events"

    repeat with thisFileName in dataList


        tell application "Finder" to set (label index of files whose name is thisFileName) to 5
    end repeat

end tell

如有任何帮助,我们将不胜感激。

1080074 3.tif
1080074 2.tif
1080069_A1.tif

这是解决此问题的最终代码以及我所做的一些进一步工作。

感谢@Mark Setchell 和@jackjr300 的耐心帮助。


set justpath to POSIX path of (choose folder with prompt "Select the Folder with Files You Want to Use") set textFileContents to (choose file with prompt "Select the list of files") set dataList to paragraphs of (read textFileContents as «class utf8») tell application "Finder" repeat with FileName in dataList try -- need a try block to ignore error, in case that the file has been moved or deleted set label index of (justpath & FileName as POSIX file as alias) to 5 end try end repeat end tell

你似乎有一个虚假的 tell application "System Events" 在那里。它是这样工作的:

set FileName to POSIX path of (choose file)
set FileRecords to paragraphs of (read FileName)
repeat with ThisFileName in FileRecords
   say ThisFileName
   tell application "Finder" to set (label index of files whose name is thisFileName) to 5
end repeat

请注意,我的测试文件不是 UTF8。

更新

顺便说一句,如果您只想在某些文件上设置标签颜色,那么从终端执行此操作可能更容易,而不必担心使用 Applescript。假设您启动了终端,然后像这样转到您的桌面

cd Desktop

然后您可以更改桌面上(以及任何子目录中)名称包含 "Freddy" 后跟 "Frog" 的所有文件的标签(即 "fileForFreddyFrog.txt"、"file from Freddy the Frog.php")

find . -name "*Freddy*Frog*" -exec xattr -wx com.apple.FinderInfo "0000000000000000000700000000000000000000000000000000000000000000" {} \;

您需要指定文件夹,因为查找器没有当前文件夹,如果您不指定文件夹,桌面除外。

set textFileContents to choose file
set dataList to paragraphs of (read textFileContents as «class utf8»)
set theFolder to "/Users/jack/Desktop/xxx/yyy" as POSIX file as alias -- change it to the path of your folder
tell application "Finder"
    repeat with thisFileName in dataList
        try -- need a try block to ignore error, in case that the file has been moved or deleted
            set label index of file thisFileName of theFolder to 5
        end try
    end repeat
end tell

更改此脚本第三行中的路径

--

如果文本文件包含文件的完整路径,则可以使用此脚本。

set textFileContents to choose file
set dataList to paragraphs of (read textFileContents as «class utf8»)
tell application "Finder"
    repeat with thisPath in dataList
        try -- need a try block to ignore error, in case that the file has been moved or deleted
            set label index of (thisPath as POSIX file as alias) to 5
        end try
    end repeat
end tell