Backing Up Address Book with AppleScript

Here is an AppleScript to export all your Address Book Contacts.app contacts to a single vCard format (vcf) file. The resulting file can be imported into other contact programs on both Mac and PC computers:

set myBackupName to "AddressBook.vcf"

-- Add timestamp and Documents path
set myTimeStamp to (year of (current date)) & (month of (current date) as number) & (day of (current date))
set myBackupPath to the (path to the documents folder as string) & myTimeStamp & "-" & myBackupName as string

-- Remove any existing back up file created today
tell application "Finder"
    if exists (file myBackupPath) then
        delete file myBackupPath -- move to trash
    end if
end tell

-- To work on Mac OS X 10.4 - 10.7, change "Contacts" to "Address Book".
tell application "Contacts"

    -- Create an empty file
    set myBackupFile to (open for access file myBackupPath with write permission)

    repeat with per in people
        write (vcard of per as text) to myBackupFile
    end repeat

    -- Close the file
    close access myBackupFile

end tell
AppleScript to back up Address Book on Mac OS X

AppleScript to back up Address Book on Mac OS X

This AppleScript creates a single file in your Documents folder; the created file is called "yearmonthday-AddressBook.vcf".

This AppleScript avoids calling out to do shell script and uses only built-in AppleScript commands.

Update: Thanks to Adam van Gaalen for his feedback and improving this AppleScript. Thanks to Adam's testing this AppleScript will now work all the way back to Mac OS X 10.4.11.

This article was posted in and tagged , , and .

Published by Graham Miln on