AppleScript的处理文件夹中的递归 [英] AppleScript Processing Files in Folders recursively

查看:473
本文介绍了AppleScript的处理文件夹中的递归的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个根文件夹,也有它的子文件夹。它是通常只有一个水平,但它可以是更深。这些文件夹也会有不同的文件,包括一些 .rar程序文件。我想创建一个递归函数横过文件夹,请检查该文件是一个RAR文件并打开/解压缩。在code正致力于一级带出什么问题。但递归调用不能正常工作和苹果脚本的错误处理是可怕的。这里是code这是我迄今所做的。

 设置文件夹名来的Macintosh HD:用户:Teja公司:桌面:马杜巴布process_folder(,文件夹名)在process_folder(根,folderNameToProcess)
    设置fileExt为{.rar程序}
    告诉应用程序发现者
        设置theItems到文件夹中的所有文件(根&安培; folderNameToProcess)
        与theFile重复theItems
            复制theFile的名字作为字符串文件名
            在fileExt EXT重复
                如果文件名以内线然后结束
                    开放theFile
                    删除theFile
                万一
            重复结束
        重复结束
        设置theFolders到文件夹的文件夹名称(根&安培; folderNameToProcess)
        与theFolder重复theFolders
            复制theFolder作为字符串TheFolderName
            显示对话框(folderNameToProcess&安培; TheFolderName&放大器;)
            尝试
                process_folder(folderNameToProcess,TheFolderName&放大器;:)
            在错误errStr号errorNumber
                显示对话框errStr
            年底试
        重复结束
    告诉结束
结束process_folder


解决方案

试着改变你的递归调用

 尝试
    我的process_folder(folderNameToProcess,TheFolderName&放大器;:)
在错误errStr号errorNumber
    显示对话框errStr
年底试

请注意在我的 process_folder 呼叫。这使得它为我工作。

I have a root folder and there are sub folders in it. It is generally one level only but it can be deeper. These folders will have different files including some .rar files. I want to create a recursive function which traverses the folders, check if the file is a rar file and open/extract it. The code is working to first level with out any problem. But the recursive call is not working and apple script's error handling is horrible. Here is the code which I have done so far.

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

解决方案

Try changing your recursive call to

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

Note the my before the process_folder call. This made it work for me.

这篇关于AppleScript的处理文件夹中的递归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆