TBH, I was running out "clean dialog" options -- because there are no proper radio buttons you'd need to put "Change", "Prepend" and "Append" in as checkboxes. Hmmm, perhaps you could do variables for each and, if blank, that operation doesn't happen.
And I assume by "Append" you mean "add between end of name and beginning of extension (if any)"? So myFile
-> myFileExtra
and myFile.txt
->myFileExtra.txt
?
Pseudo-code for the renaming example, in case something concrete helps:
on myRename(aFolder)
set Local_collection to contents of aFolder
for eachItem in Local_collection do
if eachItem is a directory then
myRename(eachItem)
else
do renaming on eachItem
end if
end for
do renaming on aFolder
end myRenaming
So if you have
dir1
subdir2
subdir3
file3
file2a
file2b
file1a
file1b
...and you call myRename(dir1)
, the routine would look in folder dir1
and see subdir2
. That's a folder, so myRename(subdir2)
would be called and would see subdir3
. That's also a folder, so myRename(subdir3)
is called.
At this point you have
myRename(dir1)
myRename(subdir2)
myRename(subdir3)
all running.
Everything in subdir3
is a file, matching the else
of the if
, so is renamed. Finally subdir3
is renamed and myRename(subdir3)
exits, returning "control" to myRename(subdir2)
. So you know have
myRename(dir1)
myRename(subdir2)
myRename(subdir2)
then recurses any other folder in it's collection (none in this case) as above, renames all files, renames subdir2
, and exits. "Control" is returned to myRename(dir1)
,... etc.
"See a directory, dig down a level. See a file, process. Nothing left in there to process? Rename self, go back up a level, continue."
The two tricks here are:
a) Dig down first! That's what the -d
in the shell script find
command is there for. Otherwise you end up renaming any "child" directories before you process their contents and the previous paths to those contents are now invalid.
b) Have a "stop and return" condition in the function -- in our case it's "everything in Local_collection has been processed" -- else you'll get runaway recursion Luckily KM has a limit on the number of macro instances that can run at once, so you won't lock your machine up. (Unluckily that same limit will stop us from going more than ~24 folders deep -- each recursion seems to count as two "normal" instances -- unless you change a hidden preference from the default of "50".)