I’ve been playing about with AppleScript today as there are a couple of tasks I want to automate on my system (Snow Leopard 10.6.3 as I write this). My background in I.T. means that task automation is something I’m into for obvious reasons … why take ages to do something many times when you can automate the process and have it done in 1/10 of the time when it needs to be done next? 🙂

Anyway, I use KeePassX to store my passwords (it’s the OS X version of KeePass, in my opinion easily the world’s best open source password safe. Although I use Time Machine (it does a great job) I need to carry my passwords with me. I don’t use the same password all over the place which means I have a whole bunch of passwords – KeePassX means I don’t have to remember them all.

So, where does AppleScript fit in? I used to use a bash script to keep my USB key (a LaCie itsakey) and local hard drive password databases in sync and, while it worked fine, I decided to have a go at doing the same thing using AppleScript.

AppleScript is, for lack of a better expression, painfully close to being English. It’s stupidly easy to write although there are some advanced things you can do with it too (I’ll get to those in a future post).

Here’s my entire script – hope someone finds it useful. 🙂 Please note that this version uses Growl to display notifications – I highly recommend installing it if you don’t have it installed already. Please also note that a production script would have much better error handling but this one does what I need it to. Comments welcome!

Script to backup KeePass database

-- see if Growl is available on this system
tell application "System Events" to set GrowlAvailable to exists application process "GrowlHelperApp"

if GrowlAvailable then
	-- enable the script to send Growl notifications
	tell application "GrowlHelperApp"
		set the allNotificationsList to {"Old KeePass versions removed.", "Old KeePass versions backed up.", "Master KeePass versions copied successfully."}
		set the enabledNotificationsList to {"Old KeePass versions removed.", "Old KeePass versions backed up.", "Master KeePass versions copied successfully."}
		register as application "Backup KeePass Database" all notifications allNotificationsList default notifications enabledNotificationsList icon of application "Script Editor"
	end tell
end if

set diskName to "LaCie"
set folderName to "KeePass"
set databaseName to "passwords.kdb"
set databaseBackup to databaseName & ".bak"
set keyfileName to "passwords.key"
set keyfileBackup to keyfileName & ".bak"

-- get the username of the current logged in user
set userName to do shell script "whoami"

try
	tell application "Finder"
		if exists folder folderName of disk diskName then
			-- set the locations of the original, destination and backup KeePass files
			-- because we're using aliases, these files & folders MUST exist first or an exception will be thrown
			set sourceDB to file databaseName of folder folderName of folder "Data" of folder userName of folder "Users" of disk " HD" as alias
			set sourceKeyfile to file keyfileName of folder folderName of folder "Data" of folder userName of folder "Users" of disk " HD" as alias
			set destDB to file databaseName of folder folderName of disk diskName as alias
			set destKeyfile to file keyfileName of folder folderName of disk diskName as alias

			set bakDB to file databaseBackup of folder folderName of disk diskName as alias
			set bakKeyfile to file keyfileBackup of folder folderName of disk diskName as alias

			-- set where the files will be copied to
			set destFolder to folder folderName of disk diskName as alias

			-- if exists file databaseBackup of folder folderName of disk diskName then
			if exists file bakDB then
				-- delete the existing backup files
				delete file bakDB
				delete file bakKeyfile
				if GrowlAvailable then
					tell application "GrowlHelperApp"
						notify with name "Old KeePass versions removed." title "Backup KeePass Database" description "Old KeePass versions removed." application name "Backup KeePass Database"
					end tell
				end if
			else
				set continueResult to display dialog "Backup files are missing from " & folderName & " on disk " & diskName & ".  Continue anyway?" buttons {"Yes", "No"} with icon caution with title "Backups missing"
				if button returned of continueResult is "No" then
					return
				end if
			end if

			-- backup the existing files
			if exists file destDB then
				set name of destDB to databaseBackup
				set name of destKeyfile to keyfileBackup
				if GrowlAvailable then
					tell application "GrowlHelperApp"
						notify with name "Old KeePass versions backed up." title "Backup KeePass Database" description "Old KeePass versions backed up." application name "Backup KeePass Database"
					end tell
				end if
			end if

			-- copy the master files
			copy file sourceDB to folder destFolder
			copy file sourceKeyfile to folder destFolder
			if GrowlAvailable then
				tell application "GrowlHelperApp"
					notify with name "Master KeePass versions copied successfully." title "Backup KeePass Database" description "Master KeePass versions copied successfully." application name "Backup KeePass Database"
				end tell
			end if
		else
			display dialog "Oops!  The destination folder you selected, /" & diskName & "/" & folderName & "/, ain't there!.  🙁  Please check it and try again." buttons {"Ok"} with icon stop with title "Oh no!"
		end if
	end tell
on error
	-- an exception occurred
	display dialog "An expected error has occurred while backing up your KeePass files.  Have a look to see if all the required files are there and try again ... k?  :)" buttons {"Ok"} with icon stop with title "Oh no!"
end try