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

Here's my Delete Sync AppleScript. It's mostly the same as the Backup Folder AppleScript but the processing loop is changed.

set m_source_folder to ""
set m_destination_folder to ""
set m_deleted_folder to true

tell application "Finder"

-- Get destination and source folders
if (m_destination_folder = "") then
set m_destination_folder to choose folder with prompt "Delete items from this folder:"
end if
if (m_source_folder = "") then
set m_source_folder to choose folder with prompt "Use this folder to check against:"
end if
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

-- Log start time
set l_start_time to current date
log "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
log "Delete destination: " & m_destination_folder as string
log "Delete source: " & m_source_folder as string
log "Deleted folder: " & m_deleted_folder as string
log "Delete start time: " & l_start_time as string
log "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"

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

-- Convert folder class to string (":...")
set l_source_folder_string to m_source_folder as string

-- Set queue items and start main repeat loop.
set l_pending_folders to {m_destination_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 source folder string
set l_string to l_current_folder as string
try
set l_string to (text l_destination_length through (length of l_string) of l_string)
on error l_error
-- error if this is the destination folder base string
set l_string to ""
end try
set l_string to l_source_folder_string & l_string

-- Determine source folder.
-- Note that source folder must exist.
set l_source_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_destination_item in l_items

set l_delete_item to false
set l_source_item to ""

-- 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

-- If we need to delete the item...
if (l_delete_item is true) then

-- Log event
log "----Deleting item " & l_destination_item as string

-- 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

end if

-- If source item is a folder and wasn't deleted, add it to pending folders list
if (l_delete_item is false and class of l_destination_item is folder) then
set l_pending_folders to l_pending_folders & {l_destination_item}
end if
end repeat

end repeat

-- Log end time
log "============================================================="
log "Delete destination: " & m_destination_folder as string
log "Delete source: " & m_source_folder as string
log "Deleted folder: " & m_deleted_folder as string
log "Delete start time: " & l_start_time as string
log "Delete end time: " & (current date)
log "============================================================="
end tell

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