【问题标题】:AppleScript Processing Files in Folders recursivelyAppleScript 递归处理文件夹中的文件
【发布时间】:2010-10-09 15:31:12
【问题描述】:

我有一个根文件夹,里面有子文件夹。它通常只有一个层次,但它可以更深。这些文件夹将包含不同的文件,包括一些 .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

【问题讨论】:

    标签: macos recursion applescript


    【解决方案1】:

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

    修复很简单

    在您对 process_folder 的递归调用前加上“my”:

    my process_folder(folderNameToProcess, TheFolderName & ":")
    

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

    【讨论】:

      【解决方案2】:

      尝试将递归调用更改为

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

      注意process_folder 调用之前的my。这使它对我有用。

      【讨论】:

        【解决方案3】:

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

        --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
        

        【讨论】:

          【解决方案4】:

          尝试为此使用 Python。如果有其他工具可以完成这项工作,真的没有理由浪费时间编写 Applescript。

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-10-04
            • 1970-01-01
            • 2013-08-07
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多