【问题标题】:Applescript, Automator - Find & Copy imgs in Dir/sub-dirApplescript, Automator - 在目录/子目录中查找和复制图像
【发布时间】:2016-07-13 12:58:19
【问题描述】:

我正在寻找可以使用名称列表(电子表格或 .csv)来搜索目录及其子目录的自动化工作流程或 applescript(但我还不熟悉该语言)特定文件名(具有不同的扩展名)并将该图像复制到为这些图像创建的文件夹中。

我找到了一个似乎与我需要的有点相似的脚本,但它不在子目录中搜索,所以我实际上还没有找到任何带有它的图像。

经过广泛的研究,我发现 2 个不同的脚本似乎是我需要的,但它们似乎都没有搜索子目录。以下是我尝试过的 2 个脚本。如果有人可以帮助我让这些搜索子目录,我将不胜感激!

脚本 1:

set thePhotos to paragraphs of (read (choose file with prompt "Choose a text file"))
set theSourceFolder to (choose folder with prompt "Choose source folder")
set theDestination to (choose folder with prompt "Choose destination folder")
set dupeList to {}
repeat with theName in thePhotos
    try
        set end of dupeList to alias ((theSourceFolder as text) & theName)
    end try
end repeat

tell application "Finder" to duplicate dupeList to theDestination with replacing

set theCount1 to (count of dupeList) as text
set theCount2 to (count of thePhotos) as text
display dialog (theCount1 & " of " & theCount2 & " items copied to " & (theDestination as text)) buttons {"OK"}

脚本 2

set fileContents to read (choose file with prompt "Choose a comma-delimited text file")
set theText to result
set AppleScript's text item delimiters to ","
set theTextItems to text items of theText
set AppleScript's text item delimiters to {""}
theTextItems
set theSourceFolder to (choose folder with prompt "Choose source folder") as string
set theDestination to (choose folder with prompt "Choose destination folder")
repeat with theEPSName in theTextItems
    tell application "Finder"
        set theEPSFile to theSourceFolder & theEPSName
        move file theEPSFile to folder theDestination with replacing
    end tell
end repeat

所以它让我选择要使用的 .csv,选择要保存的目录并选择要搜索的目录。我怎样才能让这个脚本在子目录中搜索?对于了解 Applescript 的人来说,这看起来是否可以按需运行?

提前感谢您!我非常感谢任何帮助,因为这是我的第一次 Applescript 体验,但我很高兴学习它!

【问题讨论】:

    标签: macos applescript automator


    【解决方案1】:

    这是一个使用 shell 和 Spotlight 搜索的非常快速的解决方案。

    脚本创建一个搜索条件字符串

    'kMDItemFSName == picture1.png || kMDItemFSName == picture2.jpg || ...'
    

    然后通过将找到的项目传递给cp 命令来执行搜索并复制所有文件。

    如果名称列表太长并且超过了 shell 参数的最大长度,可能需要注意。

    set theNames to paragraphs of (read (choose file with prompt "Choose a text file" of type "txt"))
    set sourceFolder to quoted form of POSIX path of (choose folder with prompt "Choose source folder")
    set destinationFolder to quoted form of POSIX path of (choose folder with prompt "Choose destination folder")
    
    set nameList to {}
    repeat with aName in theNames
        set end of nameList to "kMDItemFSName == " & quoted form of aName
    end repeat
    set {TID, text item delimiters} to {text item delimiters, " || "}
    set nameFilter to quoted form of (nameList as text)
    set text item delimiters to TID
    do shell script "mdfind -onlyin " & sourceFolder & " -0 " & nameFilter & " | xargs -0 -J {} cp {} " & destinationFolder
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-13
      • 2011-10-09
      • 2020-05-31
      • 1970-01-01
      相关资源
      最近更新 更多