Section 5.4. Backing Up Files


5.4. Backing Up Files

Talk to any computer expert, and you'll be told the same thing: backing up your files is not an option, it's a must! Unless you're interested in joining the millions of people who've lost essential files, you should back up your files regularly.

There are plenty of choices for backing up your files; it's only a matter of picking the one with the features and price you like. Here are a few of the options:

  • Commercial programs like Dantz's Retrospect Desktop (www.dantz.com/en/products/mac_desktop/index.dtml; $130) let you automatically back up important files at intervals you specify.

Up to Speed
Boolean Values

One of the cornerstones of all programming languages is the Boolean type. This simple kind of information has only two possible states: true and false. That makes it perfect for simple operations, such as determining whether something exists.

You can set Boolean variables just like any other variable:

set finderShouldQuit to true set scriptDone to false

Boolean values also have special operators (keywords) you can use. If you ever took a logic course in high school, you'll instantly recognize the three basic operators: and, or, and not. These keywords let you combine two or more Boolean values in one command, doubling their power. For instance, operators let you check whether two conditions are both met in your script, or whether either is met. Here's how:

  • If both sides of an and operator are true, it produces true. Otherwise, it produces false. For example:

    display dialog (true and false)         --That displays "false"     display dialog (true and true)         --That displays "true"

  • An or operator, on the other hand, produces true if either side is true. The only time it produces a false is if both sides are false. For example:

    display dialog (true or false)         --That displays "true"     display dialog (false or false)         --That displays "false"

  • The not operator works with only a single value (either true or false), and produces its opposite; for example:

    display dialog (not true)         --That displays "false"     display dialog (not false)         --That displays "true"

You can use any of these operators in your if statements as well. That's why the script for creating the Old Desktop folder works: it checks to see if the Old Desktop folder already exists, and then applies a not operator to the result. That means that if the folder doesn't exist, the if statement is run; if the folder does exist, the if statement isn't run.


  • Apple's own Backup 2.0 program is a perk for using the .Mac service ($99 per year). You can use it to automatically back up files onto a CD or DVD, to your iPod, or even to your iDisk. See www.mac.com for more details about Backup 2.0, a mac.com email address, and all the other benefits of a .Mac membership.

  • Shareware programs like Mike Bombich's Carbon Copy Cloner (www.bombich.com/software/ccc.html; $5) can back up your entire hard drive to another disk, quickly and easily. If you're looking for an inexpensive, simple backup solution, Carbon Copy Cloner is the perfect tool.

  • The Finder (free, included with Mac OS X) can be used to back up files, too. Unfortunately, every file or folder you want to back up has to be copiedmanuallyby you. Don't use this method if you have lots of important files to back up; it'll take hours.

The problem with all these solutions, of course, is that they either cost money or are too time-consuming to use regularly. That's why AppleScript is a great alternative: you can customize the files you want it to back up, and it's completely free.

5.4.1. The duplicate Command

The move command is for transporting an item from one folder to another. The duplicate command, on the other hand, is for copying an item from one folder to another. The original file stays untouched, and an exact copy of that file is goes anywhere you specify. That location can be another folder, another partition of your hard drive, or another drive altogether (including a USB thumb drive, an external FireWire drive, or your iPod).

The way you use the duplicate command is very similar to the way you use the move command:

tell application "Finder"     duplicate someItem to somePlace with replacing end tell

Here's how the command breaks down:

  • duplicate is the command directed at the Finder, to tell it to copy something.

  • Everything that follows the duplicate command goes by the order of "what you want to duplicate" followed by "where you will save that duplicated copy." You replace the someItem variable with the name(s) of the files and/or folders you want to duplicate. Likewise, you replace somePlace with the name of the folder or disk where you want those duplicate copies to be saved.

  • The with replacing bit tells the Finder to erase any older revisions of your files in the backup folder and replace them with the newer version. That way, you won't be stuck with all your month-old backups; you'll just have the newest versions of your files backed up.

  • Note, however, that the with replacing option considers only one thing: file names. If two files have the same name, the file that you're duplicating always replaces the one that's already thereeven if the file that's already there is bigger, newer, and shinier than the one that you're duplicating.

The with replacing option is case insensitive. If you duplicate myllamas.txt to a folder that already has MyLlamas.txt, for instance, Mac OS X considers them the same name, so it would replace the existing file (MyLlamas.txt) with the new file (myllamas.txt).

With that information in hand, you can write a simple backup subroutine, as shown here:

on backupFolderToDisk(startFolder, targetDisk)     tell application "Finder"         duplicate every file of startFolder to disk targetDisk with replacing         duplicate every folder of startFolder to disk targetDisk ¬             with replacing     end tell end backupFolderToDisk

Say you're a doctor and you want to back up your Patients folder to an external FireWire drive called Medical Backup. While you're at it, you'd also like to back up everything in your Home Documents folder, just in case your hard drive gets damaged on the flight to your next medical convention.

The good news is that you already have a subroutine for backing up files to a separate disk, so you're halfway to a working script. The bad news is that your script doesn't actually run your subroutine anywhere, so your essential files never get copied over to your external drive.

To fix this, you just have to call your existing subroutine from elsewhere in your script. The new subroutine-calling lines (shown next in bold) are what actually tell AppleScript "Please run my backup commands":

backupFolderToDisk("Patients", "Medical Backup") --Replace yourUsername below with your actual username backupFolderToDisk("Macintosh:Users:yourUsername:Desktop:","Medical Backup") --Here's the previous subroutine: on backupFolderToDisk(startFolder, targetDisk)     tell application "Finder"         duplicate every file of startFolder to disk targetDisk with replacing         duplicate every folder of startFolder to disk targetDisk with replacing     end tell end backupFolderToDisk

Each time you call the backupFolderToDisk subroutine, the Finder whirs into action and copies your requested files to the backup disk (Figure 5-5).

Be sure to replace "Patients," "Medical Backup," and so on with the actual folders and backup disk you want to use.

And if you'd like to back up additional folders, just insert extra backupFolderToDisk calls at the top of your script.

Figure 5-5. This same window shows up whether you're copying files yourself in the Finder or having AppleScript do the copying for you.


When it's this easy, you have no excuse not to back up your Mac.

To make it even easier, you can schedule your backup script to run on certain days of the week. Page Section 13.5 has the details.



AppleScript. The Missing Manual
AppleScript: The Missing Manual
ISBN: 0596008503
EAN: 2147483647
Year: 2003
Pages: 150

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net