-- Config those to your liking set saveCVSExport to "/Users/gb/Desktop/contacts.csv" set exportFields to {"Name", "Organization", "Note", "Birthday", "Nickname", "MSN", "ICQ", "Phone Number", "E-mail Address", "Address"} -- This settings will create as many rows as there are email addresses set exportMultipleEmailAddressesToMultipleContactWithSameName to true -- Config ends here -- -- Don't touch below -- set resultText to "" set currentLine to "" set numAddresses to 0 set numEmails to 0 set numPhones to 0 tell application "Address Book" -- Find the maximum number of phone numbers, addresses and email addresses repeat with x from 1 to the count of people set thePerson to person x set countA to count of address of thePerson set countE to count of email of thePerson set countP to count of phone of thePerson if countA > numAddresses then set numAddresses to countA end if if countE > numEmails then set numEmails to countE end if if countP > numPhones then set numPhones to countP end if end repeat -- Header row repeat with fieldName in exportFields my addCsvValue(fieldName) if fieldName is in "E-mail Address" and exportMultipleEmailAddressesToMultipleContactWithSameName is not true then repeat with x from 2 to numEmails my addCsvValue(fieldName & " " & x) end repeat else if fieldName is in "Address" then repeat with x from 2 to numAddresses my addCsvValue(fieldName & " " & x) end repeat else if fieldName is in "Phone Number" then repeat with x from 2 to numPhones my addCsvValue(fieldName & " " & x) end repeat end if end repeat set my resultText to my resultText & my currentLine & return -- Export all contacts repeat with x from 1 to the count of people set thePerson to person x set my currentLine to "" --set mylist to selection --repeat with thePerson in mylist -- Export each field specifiedaddCsvValue( repeat with fieldName in exportFields if fieldName is in "Name" then if exportMultipleEmailAddressesToMultipleContactWithSameName is true then my addCsvValue(the name of thePerson & "###emailLabelHere###") else my addCsvValue(the name of thePerson) end if end if if "Organization" is in fieldName then my addCsvValue(the organization of thePerson) end if if "Note" is in fieldName then my addCsvValue(the note of thePerson) end if if "Birthday" is in fieldName then set theDOB to the birth date of thePerson if theDOB is not missing value then set {dd, theMonth, yyyy} to the {day, month, year} of theDOB my addCsvValue(dd & " " & theMonth & " " & yyyy) else my addCsvValue("") end if end if if "Nickname" is in fieldName then my addCsvValue(the nickname of thePerson) end if if "MSN" is in fieldName then if (count of MSN handle of thePerson) > 0 then my addCsvValue(value of the first MSN handle of thePerson) else my addCsvValue("") end if end if if "ICQ" is in fieldName then if (count of ICQ handle of thePerson) > 0 then my addCsvValue(value of the first ICQ handle of thePerson) else my addCsvValue("") end if end if if "Phone Number" is in fieldName then set found to 1 repeat with thePhone in the phone of thePerson if found > numPhones then exit repeat end if my addCsvValue("(" & label of thePhone & ") " & value of thePhone) set found to found + 1 end repeat repeat with x from found to numPhones my addCsvValue("") end repeat end if if "E-mail Address" is in fieldName then if exportMultipleEmailAddressesToMultipleContactWithSameName is true then my addCsvValue("###emailHere###") else set found to 1 repeat with theEmail in the email of thePerson if found > numEmails then exit repeat end if my addCsvValue(value of theEmail) set found to found + 1 end repeat repeat with x from found to numEmails my addCsvValue("") end repeat end if end if if fieldName is in "Address" then set found to 1 repeat with theAddress in the address of thePerson if found > numAddresses then exit repeat end if set theCompleteAddress to "" if the street of theAddress is not missing value then set theCompleteAddress to the street of theAddress & ", " end if if the city of theAddress is not missing value then set theCompleteAddress to theCompleteAddress & the city of theAddress & " " end if if the state of theAddress is not missing value then set theCompleteAddress to theCompleteAddress & the state of theAddress end if set theCompleteAddress to theCompleteAddress & ", " if the zip of theAddress is not missing value then set theCompleteAddress to theCompleteAddress & the zip of theAddress end if my addCsvValue(theCompleteAddress) set found to found + 1 end repeat repeat with x from found to numAddresses my addCsvValue("") end repeat end if end repeat -- All fields have been done; add line to export string if exportMultipleEmailAddressesToMultipleContactWithSameName is true then set numCurrentEmails to the count of email of thePerson repeat with theEmail in the email of thePerson set theEmailValue to the value of theEmail set theEmailLabel to the label of theEmail set theLine to my searchReplace(my currentLine, "###emailHere###", theEmailValue) if numCurrentEmails > 1 then set theLine to my searchReplace(theLine, "###emailLabelHere###", " (" & theEmailLabel & ")") else set theLine to my searchReplace(theLine, "###emailLabelHere###", "") end if set my resultText to my resultText & theLine & return end repeat else set my resultText to my resultText & my currentLine & return end if end repeat my writeToFile(saveCVSExport, saveCVSExport as POSIX file) end tell on addCsvValue(toAdd) if toAdd is not missing value then set my currentLine to my currentLine & "\"" & (toAdd as string) & "\"," else set my currentLine to my currentLine & "\"" & "\"," end if end addCsvValue -- Write a string in a file; overwites if exists on writeToFile(theUFile, theFile) set touch to "touch " & theUFile do shell script touch try open for access file theFile with write permission set eof of theFile to 0 write (my resultText) to theFile starting at eof close access theFile on error try close access theFile end try end try end writeToFile on searchReplace(origStr, searchStr, replaceStr) set old_delim to AppleScript's text item delimiters set AppleScript's text item delimiters to searchStr set origStr to text items of origStr set AppleScript's text item delimiters to replaceStr set origStr to origStr as string set AppleScript's text item delimiters to old_delim return origStr end searchReplace