March, 2014 – Graham Miln

Exporting Contacts to CSV

An AppleScript to export name and e-mail addresses from on Mac OS X to a Comma Separated Values (CSV) file. The created CSV file is suitable for importing into numerous other tools and services.

I wrote this script for Mac OS X 10.9, Mavericks, but it should work without too many changes on most recent versions of Mac OS X.

To use this AppleScript:

  1. Launch **AppleScript Editor** from Applications > Utilities
  2. Copy and paste the code below into a new document
  3. Run the script
  4. On completion a `contacts.csv` will appear on your desktop
-- Save comma separated values (CSV) file to desktop
set exportPath to (path to desktop as string) & "contacts.csv"

set contactsCSV to "" – variable to collect rows of addresses set quoteString to “"” – constant to ease concatenation

tell application “Contacts”

-- Repeat with every person in your Contacts
repeat with x from 1 to the count of people
	set thePerson to person x
	set theirName to the name of thePerson

	-- A person may have multiple e-mails addresses, add one row for each
	repeat with anEmail in the email of thePerson
		set contactsCSV to contactsCSV & quoteString & theirName & quoteString & "," & quoteString & (value of anEmail) & quoteString & "

" end repeat end repeat

end tell

– Write the CSV contents to a file set exportFile to open for access file exportPath with write permission set eof of exportFile to 0 write contactsCSV to exportFile starting at eof close access exportFile