【问题标题】:Applpescript: Reading Text file for List of FilesApplpescript:读取文件列表的文本文件
【发布时间】:2015-04-21 22:42:00
【问题描述】:

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

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

error "Finder 出错:无法将 1 设置为 5。"从 1 开始的数字 -10006

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

这可能是文件名的问题吗?如果是,我该如何让脚本更宽容。

这是脚本:

    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

【问题讨论】:

    标签: automation applescript text-files


    【解决方案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" {} \;
    

    【讨论】:

    • 感谢您的回复。我仍然收到相同的错误,但现在我的计算机实际上是用文本到语音的语音说出文本文档中第一个文件的名称,然后我得到相同的错误代码。这是我的文本文件中失败的文件列表。它可能在文件名中。 1030172.tif 1080074 3.tif 1080074 2.tif 1080069_A1.tif
    • 所以如果你注释掉tell application finder这一行,像-- tell applcation finder...这样在开头添加两个减号,它是否正确地说明了文本文档中所有文件的名称?另外,在end repeat 行上方添加一个beep
    • 请点击您的问题下方的edit 并将文件的内容粘贴到那里 - 在每行前面有 4 个空格。
    • 另外,为了以防万一,我通过在一个大文件夹中的文件名中搜索某些模式来生成文件。 9000 多个文件。然后我复制了这些文件并将它们粘贴到纯文本文档中。这会生成带有完整路径的文件名。然后我进行了查找/更改以删除路径。
    • 马克,如果我按照您的要求注释掉该行,它将读取文件正常。并发出哔哔声。
    【解决方案2】:

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

    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
    

    【讨论】:

    • 跟进:有没有办法从我们选择的文本文件中获取目录的完整路径,而不是文件名路径,而只是它所在的目录?例如,我们将调用字符串 justPath。然后你的脚本可以修改为:set label index of (justpath&thispath as POSIX file as alias) to 5
    • 或者是 `set label index of (thispath of (justpath) as POSIX file as alias) 到 5 类似于 discussions.apple.com/thread/4574615?tstart=0
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    • 2013-01-26
    相关资源
    最近更新 更多