Skip the navigation

10 AppleScripts to make you love your Mac (even more)

May 21, 2009 12:01 AM ET

Targeted backups

Although Time Machine will do automated backups for those with modern versions of OS X, some readers might want an easy way to, say, keep all files related to a critical project together for an additional "in case of catastrophe" backup, or perhaps for something that could be easily downloaded to a USB drive.

It's pretty intuitive to select files to back up by name or date. For example, take this line of AppleScript:

tell application "Finder" to duplicate (every item in folder "Documents" of home whose modification date comes after (current date) - 7 * days) to folder "backup_docs" of disk "Backups" with replacing

It copies all items in your Documents folder that were modified in the last week to the backup folder on your "Backups" disk; modify the folder locations and dates as needed (note that yesterday would be current date - 1 * days [not day]). "With replacing" tells the script to overwrite any existing files with the same name.

The same "whose" construction can be used to find other things, such as these two statements that can be used for all types of script or image files:

whose name contains "script"
or
whose kind ends with "image" .

Since I prefer to select files to back up based on "everything changed since I last ran this script," here's a quick modification created with Sal Soghoian's help:

Code

tell application "Finder"
        activate
        -- Configure source and destination folders
        set source_folder to folder "Documents" of home
        set destination_folder to folder "docs" of disk "BackupDisk"

        -- Create a config file 1st time script runs to keep track of latest time run
        if not (exists file "backup_script_config.txt" in source_folder) then
                make new document file at source_folder with properties {name:"backup_script_config.txt"}
                duplicate every item in source_folder to destination_folder with replacing
        else
                set last_run to modification date of file "backup_script_config.txt" in source_folder
                duplicate (every item in source_folder whose modification date comes after last_run) to destination_folder with replacing

                -- Update config file modification date
                set the open_file to open for access file ((document file "backup_script_config.txt" of source_folder) as string) with write permission
                        set eof of the open_file to 0
                        write last_run to the open_file starting at eof
                        close access the open_file
        end if
end tell

Immediate backup on import

This script is designed for tasks like saving photographic images, where (conscientious) users might want to immediately back up files as soon as they're imported. Apple offers a sample script for download, which you set up after you enter source and destination folders; it then continues to run in the background.

I encountered a bit of a performance lag when I played with it, but for some, the security of assured immediate backup is worth a bit of sluggishness.



Our Commenting Policies