Backup this File, but only if it changes…

Have you ever wanted to try make a backup of a large file, or a large number of files, but didn’t want to waste the disk space and time it took to do it? For example, I recently ran iPhoto 5 for the first time, and it needed to upgrade my iPhoto library. But I still wanted to be able to run iPhoto 4 with it.
On Mac OS X, this is one task that is made easy by the UNIX underpinnings of that operating system.
You see, on a UNIX system, each file is actually simply a pointer to something called an INODE. It is possible that two files can share an inode. In that case, the files are identical. However, if one of the files is deleted or rewritten, then a new inode is created. The other file that shared the inode will be untouched.
I’ve leveraged this fact to make my “upgrade” to iPhoto 5 without modifying my original iPhoto library.
First, I renamed my iPhoto Library
mv "iPhoto Library" "old iPhoto Library"
Then I created a mirror directory structure
cd "old iPhoto Library"
find . -type d -exec mkdir -p ../iPhoto\ Library/{} ';'
Then I used the ln command to create new files pointing to the INODES of the existing files
find . -type f -exec ln {} ../iPhoto\ Library/{} ';'
This task will consume very little disk space, even if the images are really huge. If I ever want to revert to the old library, simply swap the names of the directories around.

AppleScript trick


If you have multiple copies of the same application on your hard disk, it can be a bit unpredictable which one will launch if you say
tell application "MyApp" to activate
in AppleScript.
Here is a code snippet I figured out for making sure it is the one in the /Applications directory that gets launched.
set mypath to ""
tell application "Finder" to set mypath to path to the "apps" from system domain as string
set mypath to mypath & "MyApp.app"
tell application mypath to activate