Hello.
The speed of this script makes watching paint dry entertaining.
But the reason for it is to make it as simple as possible to do some stuff on the files and/or folders in a subtree.
Below is a working example, edit it after your needs. If you either don’t need to do something with files or folders,
then just pass missing value where the script object with that handler should have been passed at the initial call.
The example deletes every file in the subtree, leaving the folders intact.
Enjoy!
-- generic construct for operations on a subtree can do actions on folders in subtree or on files, or both
-- the idea is to just wrap another handler which takes a file as an argument into the script object and
-- just call up the script object with that handler.
-- The Idea and implementation and any faults is totally mine. © McUsr 2010 and put in the Public Domain.
-- The usually guarrantees about nothing what so ever applies, use it at your own risk.
-- Read the documentation.
-- You are not allowed to post this code elsewhere, but may of course refer to the post at macscripter.net.
-- macscripter.net/viewtopic.php?id=33507
(*
TERMS OF USE.
This applies only to posting code, as long as you don't post it, you are welcome to do
whatever you want to do with it without any further permission.
Except for the following: Selling the code as is, or removing copyright statmentents and the embedded link in the code (without the http:// part) from the code.
You must also state what you eventually have done with the original source. This obviously doesn't matter if you distribure AppleScript as read only. I do not require you to embed any properties helding copyright notice for the code.
Credit for having contributed to your product would in all cases be nice!
If you use this code as part of script of yours you are of course welcome to post that code with my code in it here at macscripter.net. If you then wish to post your code elsewhere after having uploaded it to MacScripter.net please email me and ask for permission.
The ideal situation is however that you then refer to your code by a link to MacScripter.net
The sole reason for this, is that it is so much better for all of us to have a centralized codebase which are updated, than having to roam the net to find any usable snippets. Which some of us probabaly originated in the first hand.
I'm picky about this. If I find you to have published parts of my code on any other site without previous permission, I'll do what I can to have the site remove the code, ban you, and sue you under the jurisdiction of AGDER LAGMANNSRETT of Norway. Those are the terms you silently agree too by using this code.
The above paragraphs are also valid if you violate any of my terms.
If you use this or have advantage of this code in a professional setting, where professional setting means that you use this code to earn money by keeping yourself more productive. Or if you as an employee share the resulting script with other coworkers, enhancing the productivity of your company, then a modest donation to MacScripter.net would be appreciated.
*)
set theRootFolderAsAlias to choose folder
traverseTree(theRootFolderAsAlias, fileHandlerObject, missing value)
script fileHandlerObject
on handleAfile(aRefToAFile)
tell application "Finder"
try
delete aRefToAFile
on error
set locked of aRefToAFile to false
try
delete aRefToAFile
on error
tell me
activate
display dialog "Can't delete the file : " & (aRefToAFile as alias)
error -128
end tell
end try
end try
end tell
end handleAfile
end script
script folderHandlerObject
” Left as an exercise since I didn't need it then. Se folderHandlerObject above to get the hang of it.
end script
on traverseTree(hfsFolderNameAsAlias, aFileHandler, aFolderHandler)
tell application "Finder"
if not aFileHandler is missing value then
set tSelectedFiles to (every file of folder hfsFolderNameAsAlias) as alias list
if tSelectedFiles ≠{} then
repeat with aFile in tSelectedFiles
tell fileHandlerObject to handleAfile(aFile)
end repeat
end if
end if
set tFolders to (every folder of hfsFolderNameAsAlias)
repeat with tThisFolder in tFolders
my traverseTree(tThisFolder as alias, aFileHandler, aFolderHandler)
end repeat
if not aFolderHandler is missing value then
tell aFolderHandler to handleAFolder(tThisFolder as alias)
end if
end tell
end traverseTree
Best Regards
McUsr