Teja Kantamneni
Teja Kantamneni

Reputation: 17492

AppleScript Processing Files in Folders recursively

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

Upvotes: 1

Views: 13753

Answers (5)

Robert Kniazidis
Robert Kniazidis

Reputation: 1878

Just like when you switch from an old car to a new Ferrari, you will learn what speed is by using the following AsObjC script that I wrote for myself just for this task:

-- Get the Files in Entire Contents of Folder by Extension, then Sort them (AsObjC)

use AppleScript version "2.4"
use framework "Foundation"
use scripting additions

property |⌘| : a reference to current application
property NSPredicate : a reference to NSPredicate of |⌘|
property NSFileManager : a reference to NSFileManager of |⌘|
property |NSURL| : a reference to |NSURL| of |⌘|
property NSMutableArray : a reference to NSMutableArray of |⌘|
property NSURLIsRegularFileKey : a reference to NSURLIsRegularFileKey of |⌘|
property NSSortDescriptor : a reference to NSSortDescriptor of |⌘|
property NSDirectoryEnumerationSkipsPackageDescendants : a reference to 2
property NSDirectoryEnumerationSkipsHiddenFiles : a reference to 4

set sourceFolder to POSIX path of (path to desktop folder) & "Madhu Babu"
set sourceURL to |NSURL|'s URLWithString:sourceFolder

set fileManager to NSFileManager's |defaultManager|()
set fileKey to NSURLIsRegularFileKey
set searchOptions to (NSDirectoryEnumerationSkipsPackageDescendants) + (NSDirectoryEnumerationSkipsHiddenFiles)

-- Get entire contents of folder, includung contents of subfolders, without packages and hidden files
set entireContents to (fileManager's enumeratorAtURL:(sourceURL) includingPropertiesForKeys:({fileKey}) options:(searchOptions) errorHandler:(missing value))'s allObjects()

-- Filter case-insensitively for items with "rar" extensions.
set thePredicate to NSPredicate's predicateWithFormat:("pathExtension ==[c] 'rar'")
set urlArray to entireContents's filteredArrayUsingPredicate:(thePredicate)

-- The result is probably just the files we want, but check for any folders among them while getting their paths.
set theFiles to NSMutableArray's new()
set AsObjCTrue to current application's NSNumber's numberWithBool:true
repeat with theURL in urlArray
    if ((theURL's getResourceValue:(reference) forKey:(fileKey) |error|:(missing value))'s end) is AsObjCTrue then
        tell theFiles to addObject:(theURL)
    end if
end repeat

-- Sort the remaining URLs on their paths.
set sortDescriptor to NSSortDescriptor's sortDescriptorWithKey:("path") ascending:(true) selector:("localizedStandardCompare:")
theFiles's sortUsingDescriptors:({sortDescriptor})

set theFiles to theFiles as list

Upvotes: 2

Agnar Renolen
Agnar Renolen

Reputation: 41

The problem is that you try to do recursion from within a tell block. Your script is trying to call a "process_folder" in "Finder" which of course does not exist.

The fix is very simple

prepend your recursive call to process_folder with "my":

my process_folder(folderNameToProcess, TheFolderName & ":")

That will cause the the application to look for a "process_folder" handler in your own scope.

Upvotes: 4

Brandon
Brandon

Reputation: 1968

Try using Python for this. There is really no reason to waste time writing Applescript if there are any other tools to do the job.

I recommend Script Debugger for debugging Applescript since it has many tools for understanding what is actually happening, but recursively traversing a directory in Applescript is very slow so if you have a big tree it will bog down.

Upvotes: 0

Teja Kantamneni
Teja Kantamneni

Reputation: 17492

Here is the solution I got which is working...

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

Upvotes: 0

Lizzan
Lizzan

Reputation: 1062

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.

Upvotes: 0

Related Questions