The main algorithm is to traverse the destination
hierarchy. For each file in
the destination, check to see if a corresponding file exists, if not
then
delete the destination file. Sound simple? Well, mostly it is given
that
Backup Folder already handles the traversal code.
set m_deleted_folder to true ... if (m_deleted_folder is true) then set m_deleted_folder to choose folder with prompt "Deleted items will be moved to this folder:" end if
First off, we have m_deleted_folder. If you try to delete a
file on
an AppleShare volume the Finder asks you if you want to delete it now,
which
hangs the AppleScript. So instead we can move the files to a folder and
even
set replacing true so that the file will always be moved there.
Of
course the folder still has to be emptied later. Maybe Mac OS X 10.1
allows
you to always delete a file.
-- See if the item exists at the destination try set l_source_item to get item (name of l_destination_item) of l_source_folder on error l_error set l_delete_item to true end try
I don't think I mentioned that it's easier to ask the Finder to get an
item
reference rather than ask it if an item exists. If you look at the
Event Log,
a command like if (exists file l_file of l_curr_folder) really
does
if (l_file is in (get every file of l_curr_folder))
which is kind of
inefficient and might break if the folder has hundreds of files.
-- Delete source try if (class of m_deleted_folder is alias) then move l_destination_item to m_deleted_folder with replacing else delete l_destination_item end if on error l_error log "****" & l_error end try
This is the main file processing routine. Either we move the file to
the
deleted folder or try to delete the file. I also added error output
which
I think I forgot to do before. Notice that I use m_deleted_folder
both to store a boolean value at the beginning (true or false
indicating
whether or not to ask for the folder) and the alias of the folder.
That's a strength and weakness of AppleScript -- no variable typing.
You can
assign any value type to a variable and then change the value type
later with
another assignment. You also don't initialize variables so if you go
around
changing a variable name throughout a script you might miss one and
it's
tough to catch. As you may guess it has happened to me. I guess I'm too
used
to strongly typed languages.
So now I have enough to properly synchronize one folder the same as
another
folder. Not very robust or perfect but now I can combine the last two
scripts
and have a credible backup solution. Next up is to combine the two
scripts,
add some initial processing (strictly for my web site), and actually
log
results to a file.
|