exporting groups from addressbook

Hi,

I have an addressbook with over 7,000 contacts and am planning to convert these contacts to a cvs format for a database.

I have tons of groups which these 7,000 are sorted under, and am wondering what is the best way to export each of these groups into a vcard format and then store them in a folder with the group’s name?

thanks in advance.

-patrick

patrick,

you knew that you can export vCards from Addressbook groupvise by holding down the option key and then dragging the group to a finder Window?

ok - but of course it can also be done with a script. I recently wrote an export script so it just needed some minor modification to to your job. Only limitation of my script: you need an existing vCard file (won’t be overwritten):

set destFolder to choose folder with prompt "Please choose a destination folder for the VCard export:"
set dummyVcard to choose file with prompt "Please select an existing vCard file"
tell application "Finder"
	set dummyVcard to (duplicate dummyVcard to destFolder)
	set name of dummyVcard to "dummyVCard.tmp"
	set dummyVcard to file "dummyVCard.tmp" of destFolder
end tell

tell application "Address Book"
	repeat with gp in groups
		
		set gp_name to my fixColonsInName(name of gp)
		set gp_name to my handleDuplicates(gp_name, "", destFolder)
		tell application "Finder"
			set groupFolder to (make new folder at destFolder with properties {name:gp_name})
		end tell
		repeat with pe in (every person of gp)
			set vcfName to my fixColonsInName(name of pe)
			set vcfName to my handleDuplicates(vcfName, ".vcf", groupFolder)
			tell application "Finder"
				set newVCard to (duplicate dummyVcard to groupFolder)
			end tell
			try
				set wFile to open for access (newVCard as string) with write permission
				write (vcard of pe as Unicode text) to wFile starting at 0
				close access wFile
			on error errMsg
				display dialog "An error occured during vCard Export: " & errMsg
			end try
			tell application "Finder" to set name of newVCard to vcfName
			
		end repeat
	end repeat
	
	tell application "Finder" to delete dummyVcard
end tell

on fixColonsInName(theName)
	set {olddelims, text item delimiters} to {text item delimiters, ":"}
	set nameItems to text items of theName
	set text item delimiters to "/"
	set retVal to (nameItems) as string
	set text item delimiters to olddelims
	return retVal
end fixColonsInName

on handleDuplicates(itemName, itemExtension, destFolder)
	tell application "Finder"
		set strLen to length of itemName
		set n to 1
		repeat while (exists (item (itemName & itemExtension) of destFolder))
			set itemName to ((characters 1 thru strLen of itemName) as string) & (n as string)
			set n to n + 1
		end repeat
	end tell
	return (itemName & itemExtension)
end handleDuplicates


wonderful!

thank you so much, this saves the day!

-p