We'll start out small by writing an AppleScript to go
through a folder and
its subfolders, creating default.html aliases if there is an index.html
in
the folder. Now, my development process is to start out at the lowest
level
and work up. So I create utility routines and objects and then I create
higher level routines and objects that use the lower level ones and so
forth.
It's probably not as efficient as working top-down -- actually it
should be
the same if they're both planned out. But the big advantage for me is
that
I get to work and debug small problems first so that I'm not
overwhelmed by
the big picture ("ok, I'm having a problem here but to fix that I have
to
fix this problem down here but to fix that I have to fix this other
problem
even farther down" etc). Anyway, here's the first script:
property m_start_folder : ""
tell application "Finder" if (m_start_folder = "") then set m_start_folder to choose folder with prompt "Choose start folder for creating default.html aliases" end if set l_pending_folders to {m_start_folder} repeat while ((length of l_pending_folders) > 0) set l_curr_folder to first item of l_pending_folders set l_pending_folders to l_pending_folders & (get folders of l_curr_folder) if (length of l_pending_folders = 1) then set l_pending_folders to {} else set l_pending_folders to rest of l_pending_folders end if if (exists file "index.html" of l_curr_folder) then if (exists item "default.html" of l_curr_folder) then delete item "default.html" of l_curr_folder end if set l_alias to make new alias at l_curr_folder to file "index.html" of l_curr_folder set name of l_alias to "default.html" end if end repeat end tell
This is a simple script. We have m_start_folder which once it's
set
will not be reset unless you recompile the script. This is so I can run
it
unattended. After that we're using a queue of l_pending_folders
in a
breath-first traversal of the folder structure. For each folder we
append
any subfolders to l_pending_folders, then look for an
index.html file
which we then create a default.html alias to it. Repeat until we run
out of
pendinf folders.
I can already tell that I won't use this script as given because it's
very
inefficient. It blindly recreates default.html files redundantly even
if they
exist and point to the correct file. It's easy to add a check for that.
We
replace the if (exists file... clause with:
if (exists file "index.html" of l_curr_folder) then if (exists item "default.html" of l_curr_folder) then set l_alias to item "default.html" of l_curr_folder if ((class of l_alias is alias file) and (original item of l_alias is not equal to file "index.html" of l_curr_folder)) then delete item "default.html" of l_curr_folder set l_alias to make new alias at l_curr_folder to file "index.html" of l_curr_folder set name of l_alias to "default.html" end if else set l_alias to make new alias at l_curr_folder to file "index.html" of l_curr_folder set name of l_alias to "default.html" end if end if
So now we check that default.html is an alias and it points to the
index.html
file in the same directory. If default.html is not an alias then we
leave it
alone since we obviously didn't create the file in the first place. The
new
code is probably a bit too unwieldy but it works I think. That's the
problem
with unwieldy code, you have to test all the possible cases and you
might
miss one... Ok, I'm pretty sure it works. One thing I note is that get
folders doesn't retrieve aliases of folders. So if I want to do
folder
aliases too then I'll have to take care of that separately. But then
again,
folder aliases can introduce an infinite loop so there'd be even more
code
to check for that and I don't want or need to deal with that headache
for my
purposes.
That's enough for today. We've come up with two routines: one to
recursively
traverse a directory structure and one to create an alias of a file.
Next
time I'll write a routine to copy changed or new files from one folder
structure to another folder structure.
|