AppleScript以递归方式处理文件夹中的文件

时间:2010-10-09 15:31:13

标签: macos recursion applescript

我有一个根文件夹,里面有子文件夹。它通常只有一个级别,但它可以更深。这些文件夹将包含不同的文件,包括一些.rar个文件。我想创建一个遍历文件夹的递归函数,检查文件是否是rar文件并打开/解压缩。代码正在第一级解决任何问题。但递归调用不起作用,苹果脚本的错误处理是可怕的。这是我到目前为止所做的代码。

set folderName to "Macintosh HD:Users:Teja:Desktop:Madhu Babu:"

process_folder("", folderName)

on process_folder(root, folderNameToProcess)
    set fileExt to {".rar"}
    tell application "Finder"
        set theItems to every file of folder (root & folderNameToProcess)
        repeat with theFile in theItems
            copy name of theFile as string to FileName
            repeat with ext in fileExt
                if FileName ends with ext then
                    open theFile
                    delete theFile
                end if
            end repeat
        end repeat
        set theFolders to name of folders of folder (root & folderNameToProcess)
        repeat with theFolder in theFolders
            copy theFolder as string to TheFolderName
            display dialog (folderNameToProcess & TheFolderName & ":")
            try
                process_folder(folderNameToProcess, TheFolderName & ":")
            on error errStr number errorNumber
                display dialog errStr
            end try
        end repeat
    end tell
end process_folder

4 个答案:

答案 0 :(得分:4)

问题是你尝试从tell块中进行递归。你的脚本试图在“Finder”中调用“process_folder”,这当然不存在。

修复很简单

使用“my”前缀对process_folder的递归调用:

my process_folder(folderNameToProcess, TheFolderName & ":")

这将导致应用程序在您自己的范围内查找“process_folder”处理程序。

答案 1 :(得分:0)

尝试将递归调用更改为

try
    my process_folder(folderNameToProcess, TheFolderName & ":")
on error errStr number errorNumber
    display dialog errStr
end try

请注意my来电之前的process_folder。这使它对我有用。

答案 2 :(得分:0)

这是我得到的解决方案......

--find . -name "*.rar" -type f -delete

set folderToProcess to (choose folder with prompt "Choose Folder::")

tell application "Finder"
    activate
    set fileExt to {".rar"}
    set theTopFolder to (folderToProcess as alias)
    repeat with EachFile in (get every file of folder (folderToProcess as alias))
        try
            copy name of EachFile as string to FileName
            repeat with ext in fileExt
                if FileName ends with ext then
                    set result to (open EachFile)

                    --delete Eachfile
                    msg(result)
                end if
            end repeat
        end try
    end repeat
    --display dialog (theTopFolder as text)
    repeat with EachSubDir in (get every folder of folder theTopFolder)
        try
            --display dialog (EachSubDir as text)
            repeat with EachFile in (get every file of folder (EachSubDir as alias))
                try
                    copy name of EachFile as string to FileName
                    --display dialog FileName
                    --move Eachfile to theTopFolder
                    repeat with ext in fileExt
                        if FileName ends with ext then
                            --display dialog FileName
                            set result to (open EachFile)
                            --delete Eachfile
                            msg(result)
                        end if
                    end repeat
                end try
            end repeat
            --delete folder (EachSubDir as alias)
        end try
    end repeat
end tell

答案 3 :(得分:0)

尝试使用Python。如果有任何其他工具可以完成这项工作,那么没有理由浪费时间编写Applescript。

我建议使用Script Debugger来调试Applescript,因为它有许多工具可以理解实际发生的事情,但是在Applescript中递归遍历目录非常慢,所以如果你有一棵大树它会陷入困境。