在递归处理中更深入地钻取



我有一个三级深的文件夹,每层包含大量的文件。我可以通过下面的脚本深入两个级别,但是第三个级别带来了一些挑战。有人介意提供一些指导,说明我应该如何更深入地了解吗?使用python或其他语言是不可接受的,因为我正在尝试了解它如何与AppleScript配合使用。

set sourceFolder to (choose folder)
tell application "Finder"
    my changeFileNameCase(sourceFolder, "upper")
    repeat with subFolder in (get every folder of folder sourceFolder)
        my changeFileNameCase(subFolder as alias, "upper")
        #This Is No Good
        repeat with theFolder in (get every folder of folder subFolder)
            my changeFileNameCase(theFolder, "upper")
        end repeat
    end repeat
end tell
on changeFileNameCase(targetFolder, caseToSwitchTo)
    tell application "Finder"
        set fileList to every file of folder targetFolder
        repeat with theFile in fileList
            set oldName to name of theFile
            set newName to my changeCaseOfText(oldName, caseToSwitchTo)
            set the name of theFile to newName
        end repeat
    end tell
end changeFileNameCase

要使处理程序递归,您必须获取文件夹的所有项目并检查每个项目的类。

  • 如果类folder调用传递文件夹的处理程序
  • 如果类file重命名

set sourceFolder to (choose folder)
changeFileNameCase(sourceFolder, "upper")
on changeFileNameCase(targetFolder, caseToSwitchTo)
    tell application "Finder"
        set theList to every item of targetFolder
        repeat with i from 1 to count theList
            set theItem to item i of theList
            if class of theItem is folder then
                my changeFileNameCase(theItem, caseToSwitchTo)
            else
                set oldName to name of theItem
                set newName to my changeCaseOfText(oldName, caseToSwitchTo)
                set name of theItem to newName
            end if
        end repeat
    end tell
end changeFileNameCase

最新更新