(* Program: remove_word_backups Author: Robert Boucher; http://www.motoboy.org/blog; ramanboucher@mac.com Created: 10 March 2003 License: All may use, but code may not be charged for! Send me your improvements. What it does: Starting at user home dir/Documents or specified directory and recursively search for files named "Backup..." or "Bkup..." and either move them to temp_folder (specified below) or delete them (moves them to the trash). These are the names word uses for its backup files. You can easily change this to remove any files...so be careful. Then, if requested, it will open Text Edit with a log, user can save it if she likes. What it should do: Provide an external way to set the properties like a prop file or a gui. Also remove files ending with certain things. (Like .bak or .out.) Save the log file somewhere. (I couldn't get 'tell application "TextEdit" to save...' to work.) *) (* Set 'global' user variables. (They need to be passed to functions, why?) Set these as appropriate for your needs. top_level_directory, the directory to start from temp_folder, the place to put the files if not deleted, must exist; also location of log file search_text, if files begin with this text, they're history search_text_two, another string to search for show_log, will open text edit if text is "true" results, for printing to log file log_file, name of log file new_date, for log file instructions, for log file gui_on, if false, we move Backup and Bkup file to the trash from the default directory, else we ask. *) -- set top_level_directory to home on the startup disk as string -- set top_level_directory to "Users:raman:Documents" set user_home to path to home folder as alias set user_home_dir to user_home as string set top_level_directory to user_home & "Documents" as string -- tell application "Finder" to set the top_level to folder "Users:raman:Documents" of disk "fog" as alias tell application "Finder" to set the temp_folder to folder "Temporary Items" of the startup disk as string set the search_text to "Backup" as string set the search_text_two to "Bkup" as string set the show_log to "true" as boolean set the results to {} as list set the log_file to "Word Backups Deleted" set the new_date to (current date) as string set the instructions to "Log file for Word backup remover. You may save this log if you like." & return set the gui_on to "true" as boolean (* End set user variables. *) (* Body of action starts here. *) (* Ask user to either trash or move files and where, or default to the Trash and directory specified above. *) if gui_on then display dialog ("Move files starting with " & search_text & " or " & search_text_two & " to " & return & " a) " & temp_folder & return & " b) Trash") default answer top_level_directory buttons {"Cancel", "Folder", "Trash"} default button "Trash" copy the result as list to {the top_level_directory, the action} else set the action to "Trash" end if (* If directory cannot be found, we fail and give notification (see on error below). *) try tell application "Finder" to set the top_level to folder top_level_directory as alias on error set action to "Cancel" if gui_on then display dialog "Directory specified doesn't exist." & return & "Please enter full path using : as separator." buttons {"Quit"} default button "Quit" end if end try (* Performing checks. *) if the action is not "Cancel" then (* Move to trash or folder. *) if the action is "Folder" then set location to temp_folder as string else set location to "Trash" end if (* Initial call to recursive function. *) copy my check_folder(top_level, temp_folder, search_text, search_text_two, action, results) to listing (* Show log file, if requested and if listing has data. *) if show_log then -- set the log_location to (temp_folder & log_file) as alias set temp_folder to temp_folder as alias if listing as string is not equal to "" then tell application "TextEdit" activate make new document at the beginning of documents set the name of window 1 to log_file set the text of the front document to (instructions & "Backup files moved to " & location & " on " & new_date & return & listing as string) try -- Doesn't work, throws some kind of error...saves it to random location save the front document in log_location on error if gui_on then display dialog "Cannot save logfile. Please do so manually." end if end try end tell else if gui_on then display dialog "No backup files found." buttons "Quit" default button "Quit" end if end if end if end if (* Checks file specified by this_info and next_item and either trashes it or moves it to the temp_folder. *) on check_file(this_info, search_text, search_text_two, source_folder, next_item, temp_folder, action) set the item_name to the name of this_info if the item_name begins with the search_text or the item_name begins with search_text_two then set deletable_item to (source_folder & next_item) as string if the action is "Folder" then tell application "Finder" to move file deletable_item to folder temp_folder else if the action is "Trash" then tell application "Finder" to delete file deletable_item end if return deletable_item else return "" end if end check_file (* Checks contents of this folder for file or folders. Recursively searches folder, passes files to check_file. *) on check_folder(source_folder, temp_folder, search_text, search_text_two, action, results) set source_folder to source_folder as string set the item_list to list folder source_folder without invisibles repeat with i from 1 to number of items in the item_list set next_item to item i of the item_list as string set this_item to (source_folder & next_item) as alias set this_info to info for this_item if folder of this_info is false then copy my check_file(this_info, search_text, search_text_two, source_folder, next_item, temp_folder, action) to temp_results if temp_results is not "" then set results to results & return & temp_results end if else set next_folder to (source_folder & next_item) as alias copy my check_folder(next_folder, temp_folder, search_text, search_text_two, action, results) to results end if end repeat return {results} end check_folder