kcw | journal | 2001 << Previous Page | Next Page >>

First the source code for my backup AppleScript, then I'll talk about it in the next journal.

set m_source_folder to ""
set m_destination_folder to ""

tell application "Finder"

-- Get source and destination folders
if (m_source_folder = "") then
set m_source_folder to choose folder with prompt "Choose source folder to copy from"
end if
if (m_destination_folder = "") then
set m_destination_folder to choose folder with prompt "Choose destination folder to copy to"
end if

-- Get source folder length so that we can strip it out of each source folder's path
set l_source_length to (length of (m_source_folder as string)) + 1

-- Convert folder class to string (":...")
set l_destination_folder_string to m_destination_folder as string

-- Set queue items and start main repeat loop.
set l_pending_folders to {m_source_folder}

-- While items remain in queue, process each one
repeat while ((length of l_pending_folders) > 0)

-- Remove the first item from l_pending_folders
set l_current_folder to first item of l_pending_folders
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

-- Log current folder
log "Checking folder " & l_current_folder as string

-- Determine destination folder string
set l_string to l_current_folder as string
try
set l_string to (text l_source_length through (length of l_string) of l_string)
on error l_error
-- error if this is the source folder base string
set l_string to ""
end try
set l_string to l_destination_folder_string & l_string

-- Determine destination folder.
-- Note that destination folder must exist.
set l_dest_folder to alias l_string

-- Cycle through items in the current folder and process each item
set l_items to items of l_current_folder

repeat with l_source_item in l_items

set l_copy_item to false
set l_dest_item to ""

-- See if the item exists at the destination
try
set l_dest_item to get item (name of l_source_item) of l_dest_folder
on error l_error
set l_copy_item to true
end try

-- Check to see if source and destination differ.
if (l_copy_item is false) then
if (class of l_source_item is not equal to class of l_dest_item) then
-- First check class of items
set l_copy_item to true
else if (class of l_source_item is not folder and modification date of l_source_item
is not equal to modification date of l_dest_item) then
-- Second, check date of items. Only for non-folders
set l_copy_item to true
end if
end if

-- If we need to copy the item...
if (l_copy_item is true) then

-- Log event
log "----Copying item " & l_source_item as string

-- Copy source to destination
if (class of l_source_item is folder) then
-- If source is a folder then create destination folder
set l_folder to make new folder at l_dest_folder
set name of l_folder to (name of l_source_item)
else
-- If source is not a folder then copy it to destination
with timeout of 86400 seconds -- 1 day timeout
try
duplicate l_source_item to l_dest_folder with replacing
on error l_error
end try
end timeout
end if

end if

-- If source item is a folder, aadd it to pending folders list
if (class of l_source_item is folder) then
set l_pending_folders to l_pending_folders & {l_source_item}
end if
end repeat

end repeat
end tell

Copyright (c) 2001 Kevin C. Wong
Page Created: August 20, 2004
Page Last Updated: August 20, 2004