Script to import contacts from CSV file into Address Book

Hi,

I am wondering if it would be possible to import contacts from a CSV file into a group in Address Book using applescript, and if so, if anyone is aware of a script that does this or something similar.

I have a CSV file with three columns ‘First Name’, ‘Last Name’ and ‘Email Address’. I would like these to correspond with ‘First’, ‘Last’ and (Work) ‘Email’ in Address Book.

Ideally the script would also check to see if entries already exist for the people being imported, but this is less crucial.

Thanks in advance for any assistance,

Nick

Hi,

the problem with CSV files is they have no unique format, although there is a RFC specification.
The separation character may be comma, tab or semicolon, the text encoding could be anything,
and the treatment of quotes and new line characters could be different.

Assuming the CSV file is MacRoman encoded, the separation character is comma, and the fields contain
no new line characters or quotes


property theLabel : "Work"
property theGroup : "My Group"

set csvText to paragraphs of (read file "Disk:path:to:csv.txt")
set {TID, text item delimiters} to {text item delimiters, ","}

repeat with anItem in csvText
	set {firstName, lastName, emailAddress} to text items of anItem
	tell application "Address Book"
		set thePerson to make new person with properties {first name:firstName, last name:lastName}
		make new email at end of emails of thePerson with properties {label:theLabel, value:emailAddress}
		add thePerson to group theGroup
	end tell
end repeat
set text item delimiters to TID
tell application "Address Book" to save


Thanks Stefan

Nick