Syncing your Apple Contacts to a Git Repository as vCards 05 Dec 2013

I have all my contacts stored in Apples Contacts.app. This is synced via the iCloud to my iPhone. Which is pretty handy.

However I would prefer to have this data also as plain text I can store and backup independent from Apple.

My Idea is that every Contact can be represented as a vCard. A vCard is represented as plain text (lets see how this will work out for associated images). And all of this vCards could be stored in a git repository.

This would allow me to:

So lets go

Step 1: Have a way to extract all contacts to one folder programmatically

Contacts allows me to export a contact as a vCard. This adds all information and the photo as plain text.

If I try to export all Contacts from the Contacts.app I receive one big file with all contacts.

I played around with AppleScript and found a working script that extracts all Contacts into one vCard. In this case I had to rewrite this script to make seperate vCards for each contact.

However it was more easy to write an own script with automator. I only had to select all available contacts and export them individually to a folder.

Workflow in Automator

I extracted the document.wflow from the saved Automator Workflow and wrote a simple shell script to sync the contacts

#!/usr/bin/env bash
rm -f *.vcf
automator export-contacts-as-vcard.wflow
git add --all *.vcf
git commit -m "`date "+%FT%T"`"
git push origin master

export-contacts-as-vcard.wflow and sync (the bash file) reside in the contacts folder. Everytime sync is executed all contacts are synced from the address book and updated in the git repository. Then the update is pushed to a remote git repository.

This script can be executed in a cron job and you can backup your contacts on your own terms.

This was the most simplest solution I could hack together as fast as possible. It would be great if the workflow xml file would be written in a programming language. But for now it works.