【问题标题】:Use Automator to copy files to a new destination using a txt file with filenames, skip non existent files使用 Automator 使用带有文件名的 txt 文件将文件复制到新目标,跳过不存在的文件
【发布时间】:2021-11-09 12:59:48
【问题描述】:

here 描述的问题类似,我有一个带有文件名的文本文件,以及一个包含需要复制到另一个目的地的图像文件的文件夹。由于这些文件经常有数百个,因此自动执行此任务会很有帮助。

挑战在于文本文件中的某些文件可能不存在,而我拥有的当前版本的脚本无法解决这种情况并引发错误。

总之,我想使用 Automator & Apple Script 来:

  1. 请求包含原始文件的源文件夹
  2. 询问应将它们复制到的目标文件夹
  3. 请求一个带有文件名的文本文件
  4. 从文本文件中读取名称
  5. [new] 检查是否存在匹配文件并将其复制到目标位置
  6. [new] 如果没有匹配的文件,则跳过该行并继续下一行

这是我目前所拥有的——只要文本文件中的每一行都有一个实际的图像,它就可以工作:

Automator Actions

还有 AppleScript:

on run {input, parameters}
    
    set imgDestination to input's item 2
    set imgSource to input's item 1
    
    set imgNameFile to choose file with prompt {"Select file of image filenames:"}
    
    set imageList to every paragraph of (read imgNameFile)
    
    repeat with eachName in imageList
        tell application "Finder"
            set targetImageFile to item 1 of (get every file in folder imgSource whose name = (eachName as text))
            duplicate targetImageFile to folder imgDestination
        end tell
    end repeat
    return input
end run

不幸的是,我的技能不足以为 5 和 6 提出 if/else 方案,因此我们将不胜感激。

【问题讨论】:

    标签: macos automation applescript automator finder


    【解决方案1】:

    你必须检查文件是否存在。

    您的脚本效率很低,因为 Finder 中的 whose 子句非常昂贵,尽管您可以避免循环

    tell application "Finder"
        duplicate (get every file in folder imgSource whose name is in imageList) to folder imgDestination
    end tell
    

    更有效的方法是检索文件names一次并检查列表是否包含当前名称。

    如果文本文件采用 UTF8 编码(这是一种准标准),则可能会出现意外行为。如果是这样,您必须read 文本文件as «class utf8»

    最后,如果 input 中的源和目标引用是 AppleScript 别名说明符,您必须删除所有出现的 folder 关键字。

    on run {input, parameters}
        
        set imgDestination to input's item 2
        set imgSource to input's item 1
        
        set imgNameFile to choose file with prompt {"Select file of image filenames:"}
        set imageList to every paragraph of (read imgNameFile as «class utf8»)
        
        tell application "Finder" to set fileNames to name of every file in folder imgSource
        
        repeat with eachName in imageList
            if fileNames contains eachName then
                tell application "Finder" to duplicate file eachName of folder imgSource to folder imgDestination
            end if
        end repeat
        return input
    end run
    

    【讨论】:

    • 谢谢 - 我测试了两个版本:它们都工作正常,但正如你所说,第二个更高效,甚至可以在我的旧机器上运行。这让我的生活变得如此轻松,非常有责任感。
    【解决方案2】:

    如果未找到该文件,则将 targetImageFile 变量设置为 {}(即一个空列表)。 duplicate 命令会引发错误,因为它无法复制此空列表。解决问题的方法是:仅当targetImageFile值不是{}时才尝试复制。

    on run {input, parameters}
        
        set imgDestination to input's item 2
        set imgSource to input's item 1
        set imgNameFile to choose file with prompt {"Select file of image filenames:"}
        set imageList to paragraphs of (read imgNameFile)
        
        repeat with eachName in imageList
            tell application "Finder"
                set targetImageFile to (first file in folder imgSource whose name = eachName)
                if targetImageFile is not {} then duplicate targetImageFile to folder imgDestination
            end tell
        end repeat
        
        return input
    end run
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多