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

时间:2015-04-21 22:42:00

标签: automation applescript text-files

我一直在尝试拼凑一个脚本来从文本文档中获取文件列表,然后逐行浏览列表,然后对文件执行某些操作。在这种情况下,它正在更改文件上的标签,但我们会在其他实例中使用它来移动文件等。

它适用于我的桌面上的测试文件,文件被标记为紫色标签,但是当尝试在文件夹中运行它时我实际需要它失败并显示以下错误消息:

  

错误“Finder收到错误:无法设置1到5”。数字-10006从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

2 个答案:

答案 0 :(得分:1)

你似乎有一个虚假的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”,“来自Freddy the Frog.php的文件”)

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

答案 1 :(得分:0)

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

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