Applescript:选定文件夹的递归文件夹

问题描述

我使用applescript,试图在一组并行文件夹中为目标文件夹及其子文件夹中的每个.recipe文件创建一个.patch文件。这还包括在脚本执行操作时创建并行文件夹结构。我的步骤正确,但是详细语法的某些部分是错误的,我无法弄清楚该如何解决。到目前为止,我所了解的调试信息并没有帮助我,尽管我确信这是一个不正确的applescript语法的简单问题。这是代码

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

on run
    set patchFile to (choose file with prompt "Select patch file...")
    set assetsFolder to (choose folder with prompt "Select recipe assets folder...")
    set dumpFolder to (choose folder with prompt "Select dump folder...")
    
    makePatches(patchFile,assetsFolder,dumpFolder)
end run

on makePatches(patchFile,targetFolder,dumpFolder)
    tell application "Finder"
        set recipeCount to count (files in folder (targetFolder) whose name ends with ".recipe")
        if (recipeCount > 0) then
            set recipeFiles to files in folder (targetFolder) whose name ends with ".recipe"
            repeat with eachFile in recipeFiles
                set recipePatch to duplicate patchFile to folder dumpFolder
                set recipePatch's name to eachFile's name & ".patch"
            end repeat
        end if
        
        set folderCount to (folders in folder (targetFolder) whose visible is true)
        if (folderCount > 0) then
            set subFolders to folders in folder (targetFolder)
            repeat with eachFolder in subFolders
                set newName to folder eachFolder's name
                set newFolder to make new folder at folder dumpFolder
                set newFolder's name to newName
                my makePatches(patchFile,eachFolder,newFolder)
            end repeat
        end if
    end tell
    return
end makePatches

请注意,如果我删除了遍历文件夹的代码,只是让它为文件夹中的每个.recipe文件创建了补丁,则该代码将起作用。或至少,据我所知。因此,问题应该出在folderCount处或之后。需要递归,以及使用带有提示的选择的能力。希望这不是问题。迭代部分的正确语法是什么?

解决方法

在半盲地调整代码并查看了我能找到的每个相关示例之后,我想到了一些完全可以满足需要的代码。但是我真的不太了解原始代码出了什么问题,因此尽管我发布自己编写的工作代码,但我仍然想听听所解释的原始代码有什么问题。

on run
    set patchFile to (choose file with prompt "Select patch file...")
    set assetsFolder to (choose folder with prompt "Select recipe assets folder...")
    set dumpFolder to (choose folder with prompt "Select dump folder...")
    
    makePatches(patchFile,assetsFolder,dumpFolder)
end run

on makePatches(patchFile,targetFolder,dumpFolder)
    tell application "Finder"
        set recipeFiles to files of targetFolder whose name ends with ".recipe"
        repeat with eachFile in recipeFiles
            set recipePatch to duplicate patchFile to dumpFolder
            set recipePatch's name to eachFile's name & ".patch"
        end repeat
    end tell
    
    tell application "Finder" to set theseSubFolders to folders of targetFolder
    repeat with tSubF in theseSubFolders
        set newName to tSubF's name
        tell application "Finder" to set newFolder to (make new folder at dumpFolder with properties {name:newName})
        
        my makePatches(patchFile,tSubF,newFolder)
    end repeat
end makePatches