(* This simpler version of the applescript exports only names and email addresses. The one on this page exports phone numbers and addresses too. *)

(*
Earthlink requires a return at the bottom of the csv or it ignores last record

*)

set theValues to {"Title", "First Name", "Last Name", "Middle Name", "Name", "Nickname", "E-mail Address", "E-mail 2 Address", "E-mail 3 Address"}
set theLine to my list2string(theValues)
set theData to theLine
set theResult to button returned of (display dialog "Include all records in Address Book, or only records with E-mail addresses?" buttons {"Cancel", "Only with E-mails", "All records"} default button 3)
tell application "Address Book"
	set thePeople to every person
	set thePeople to people 1 through 3
	set theCount to 0
	repeat with eachPerson in thePeople
		set theEmails to emails of eachPerson
		set c to count theEmails
		if theResult = "All records" or c > 0 then
			set theTitle to title of eachPerson
			set theTitle to my setValue(theTitle)
			--0 Title
			set theValues to {theTitle}
			set theNameFirst to first name of eachPerson
			set theNameFirst to my setValue(theNameFirst)
			--1 First Name
			set theValues to theValues & {theNameFirst}
			set theNameLast to last name of eachPerson
			set theNameLast to my setValue(theNameLast)
			--2 Last Name
			set theValues to theValues & {theNameLast}
			set theNameMiddle to middle name of eachPerson
			set theNameMiddle to my setValue(theNameMiddle)
			--3 Middle Name
			set theValues to theValues & {theNameMiddle}
			--4 Name (Display Name)
			set theName to name of eachPerson
			set theName to my setValue(theName)
			set theValues to theValues & {theName}
			--5 Nickname
			set theNameNick to nickname of eachPerson
			set theNameNick to my setValue(theNameNick)
			set theValues to theValues & {theNameNick}
			
			set theEmails to emails of eachPerson
			set c to count theEmails
			--6,7,8 E-mail Address (1 through 3)
			repeat with x from 1 to 3
				if (c ≥ x) then
					--return value of item x of theEmails
					set theValues to theValues & {my setValue(value of item x of theEmails as string)}
				else
					set theValues to theValues & {""}
				end if
			end repeat
			set theLine to my list2string(theValues)
			set theData to theData & return & theLine
			set theCount to theCount + 1
		end if
	end repeat
end tell
set theData to theData & return

set theCSVfile to choose file name with prompt "Save the exported data:" default name "AddressBook.csv" default location (get path to desktop folder from user domain)
set myFileReference to open for access theCSVfile with write permission
set eof of myFileReference to 0
write theData to myFileReference
close access myFileReference
beep
say "Wrote " & theCount & " address records" as string
on list2string(theList)
	set AppleScript's text item delimiters to "\",\""
	set theLine to theList as string
	set AppleScript's text item delimiters to ""
	set theLine to "\"" & theLine & "\"" as string
	return theLine
end list2string
on setValue(theItem)
	if theItem = missing value then
		set theItem to ""
	else
		set AppleScript's text item delimiters to the "\""
		set searchListlist to every text item of theItem
		set AppleScript's text item delimiters to "\"\""
		set theItem to the searchListlist as string
		set AppleScript's text item delimiters to ""
	end if
	return theItem
end setValue



-- more export information, and script that exports names, addresses, phones, emails

-- applescript page