Pin Me

Useful AppleScript to Create an iCal and Address Book Archive

written by: Tampa Mac Support•edited by: Rebecca Scudder•updated: 9/8/2009

Here's an easy little AppleScript that can be scheduled in iCal to create an archive of your Address Book and iCal events. It can be very useful to have an up-to-date backup of these items.

  • slide 1 of 1

    AppleScript to Create a Address Book and iCal Archive

    The other day while working with a client I ran into a bit of an issue when addressing a corrupt iCal database. The issue was that iCal randomly deleted certain events before a particular date. The cause of the corruption could have been linked to a variety of things. The resolution seemed simple enough since the client was using Time Machine. This is the kicker- Time Machine offers you very little use in times where you need to restore an entire database. Sure I could incrementally pull the data off the Time Machine backups but that would be tedious and near impossible considering the amount of backups being performed. The other alternative was to do a complete restore from a previous Time Machine backup but not only would this be time consuming it would also leave a ton of other pertinent data off the machine.

    Finally I realized the only way to ensure that iCal and Address Book are being properly backed up in case of a disaster is to manually perform a backup for each application and save those files.

    That’s where this script comes in. I created a simple script that can be scheduled in iCal and set to run at a certain date and time. All this script does is create an Address Book Archive and a Back up of iCal and place these files on the Desktop. These files are both placed on your desktop and are then backed up when your next Time Machine backup occurs.

    Therefore, in the unfortunate event your iCal or Address Book become corrupted you can simply retrieve these backup files and restore your database. Enjoy!

    Here's the AppleScript:

    tell application "iCal" to activate

    delay 2

    tell application "System Events"

    tell menu item "Back up iCal…" of menu "File" of menu bar 1 of application process "iCal" to click

    delay 3

    keystroke "d" using command down

    keystroke return

    end tell

    tell application "iCal" to quit

    tell application "Address Book" to activate

    delay 2

    tell application "System Events"

    tell menu item "Address Book Archive…" of menu 1 of menu item "Export" of menu 1 of menu bar item "File" of menu bar 1 of application process "Address Book" to click

    delay 3

    keystroke "d" using command down

    keystroke return

    end tell

    tell application "Address Book" to quit

    Now that you have the script you will need to open AppleScript and cut and paste the above code in the Script Editor. Once that is done simply save the script and launch iCal and add an event. When you add the event make sure to add an alarm and choose Run Script. Now you can direct iCal to the saved AppleScript and when that date and time occurs your Mac will run the script and deposit an iCal and Address Book archive on your Desktop.