Filemaker address Book

I’m putting together a script to recuperate information from address book and post it into Filemaker. I know the code is really rough but I’m new to this. I’m able to get the address information but i’m unable to specify with the label’s “work” or “home” I’m also unable to specify which email address or phone numbers I’d like. I also feel I’m repeating myself a lot so i’m sure that there is some cleaning up to do. Could anyone please give me a hand.

(Thanks to StefenK and Dylan Webber for the list code.)

[code]try
set Lst to {}
tell application “Address Book”
set thePeopleList to every person
repeat with i from 1 to count of thePeopleList
set thename to (name of item i of thePeopleList)
set the end of Lst to thename
end repeat
end tell
set unsortedList to Lst
set old_delims to AppleScript’s text item delimiters
set AppleScript’s text item delimiters to {ASCII character 10}
set theList to paragraphs of (do shell script “echo " & quoted form of (unsortedList as string) & “| sort -d -f”)
set AppleScript’s text item delimiters to old_delims
theList
set thePersonChosen to (choose from list theList with prompt " Please choose a Person :”)
--------------------------------------------------------------------------
tell application “Address Book”
set thePersonId to every person whose name is thePersonChosen as string
set thePersonFirstName to (first name of (item 1 of thePersonId)) as string
if thePersonFirstName is missing value then return “” as string
set thePersonLastName to (last name of (item 1 of thePersonId)) as string
if thePersonLastName is missing value then return “” as string
set thePersonFullName to (name of (item 1 of thePersonId)) as string
if thePersonFullName is missing value then return “” as string
set thePersonWorkAddressStreet to (street of address 1 of (item 1 of thePersonId)) as string
if thePersonWorkAddressStreet is missing value then return “” as string
set thePersonWorkAddressZip to (zip of address 1 of (item 1 of thePersonId)) as string
if thePersonWorkAddressZip is missing value then return “” as string
set thePersonWorkAddressVille to (city of address 1 of (item 1 of thePersonId)) as string
if thePersonWorkAddressVille is missing value then return “” as string
set thePersonWorkAddressPays to (country of address 1 of (item 1 of thePersonId)) as string
if thePersonWorkAddressPays is missing value then return “” as string
set thePersonHomeAddressStreet to (street of address 2 of (item 1 of thePersonId)) as string
if thePersonHomeAddressStreet is missing value then return “” as string
set thePersonHomeAddressZip to (zip of address 2 of (item 1 of thePersonId)) as string
if thePersonHomeAddressZip is missing value then return “” as string
set thePersonHomeAddressVille to (city of address 2 of (item 1 of thePersonId)) as string
if thePersonHomeAddressVille is missing value then return “” as string
set thePersonHomeAddressPays to (country of address 2 of (item 1 of thePersonId)) as string
if thePersonHomeAddressPays is missing value then return “” as string
set thePersonCompany to (organization of (item 1 of thePersonId)) as string
if thePersonCompany is missing value then return “”
set thePersonJob to (job title of (item 1 of thePersonId)) as string
if thePersonJob is missing value then return “”
set thePersondepartment to (department of (item 1 of thePersonId)) as string
if thePersondepartment is missing value then return “”
set thePersonWorkPhone to (phone of (item 1 of thePersonId)) as string
if thePersonWorkPhone is missing value then return “”

end tell

on error
error “Failed to retrieve Contact details”
end try

try
tell application “FileMaker Pro Advanced”
activate
set the contents of field “Nom de Famille” of the current record ¬
to thePersonLastName
set the contents of field “Prénom” of the current record ¬
to thePersonFirstName
set the contents of field “Service” of the current record ¬
to thePersondepartment
set the contents of field “Poste” of the current record ¬
to thePersonJob
set the contents of field “T Rue” of the current record ¬
to thePersonWorkAddressStreet
set the contents of field “T Code Postale” of the current record ¬
to thePersonWorkAddressZip
set the contents of field “T Ville” of the current record ¬
to thePersonWorkAddressVille
set the contents of field “T Pays” of the current record ¬
to thePersonWorkAddressPays
set the contents of field “Rue” of the current record ¬
to thePersonHomeAddressStreet
set the contents of field “Code Postale” of the current record ¬
to thePersonHomeAddressZip
set the contents of field “Ville” of the current record ¬
to thePersonHomeAddressVille
set the contents of field “Pays” of the current record ¬
to thePersonHomeAddressPays
set the contents of field “Téléphone Entreprise” of the current record ¬
to thePersonWorkPhone
set the contents of field “Téléphone Mobile” of the current record ¬
to thePersonWorkCell
end tell
on error
error “Failed to add contact details to filemaker”
end try[/code]

Try this one. Simplified the repeat loop, this will be much faster. Also, the way the info is retrieved from an Address book entry will be much easier to read this way, but there isn’t much else you can do I think to simplify it. And I changed the way the errors are caught, plus a check if the user actually chose a name and hit Ok. Your previous script would have kept on going if the user hit cancel.

Let me know if that helps. Btw, check if the order in which I matched the details from the Address book is in the correct order, might have missed one.

try
	set theUnsortedList to {}
	tell application "Address Book"
		set thePeopleList to every person
		repeat with currentPersonFromList in thePeopleList
			set thename to (name of currentPersonFromList) as string
			set the end of theUnsortedList to thename
		end repeat
	end tell
	set old_delims to AppleScript's text item delimiters
	set AppleScript's text item delimiters to {ASCII character 10}
	set theSortedList to paragraphs of (do shell script (("echo " & quoted form of (theUnsortedList as string) & "| sort -d -f") as string))
	set AppleScript's text item delimiters to old_delims
	set thePersonChosen to (choose from list theSortedList with prompt " Please choose a Person :")
	
	if thePersonChosen is not false then
		set theErrorTitle to "Failed to retrieve Contact details"
		tell application "Address Book"
			set thePersonId to every person whose name is thePersonChosen as string
			--Here, the first line in the first block corresponds to the first line in the second block (after the "to") and so on...
			set ¬
				{thePersonFirstName, ¬
					thePersonLastName, ¬
					thePersonFullName, ¬
					thePersonWorkAddressStreet, ¬
					thePersonWorkAddressZip, ¬
					thePersonWorkAddressVille, ¬
					thePersonWorkAddressPays, ¬
					thePersonHomeAddressStreet, ¬
					thePersonHomeAddressZip, ¬
					thePersonHomeAddressVille, ¬
					thePersonHomeAddressPays, ¬
					thePersonCompany, ¬
					thePersonJob, ¬
					thePersondepartment, ¬
					thePersonWorkPhone} ¬
					to ¬
				¬
					{¬
						(first name of (item 1 of thePersonId)) as string, ¬
						(last name of (item 1 of thePersonId)) as string, ¬
						(name of (item 1 of thePersonId)) as string, ¬
						(street of address 1 of (item 1 of thePersonId)) as string, ¬
						(zip of address 1 of (item 1 of thePersonId)) as string, ¬
						(city of address 1 of (item 1 of thePersonId)) as string, ¬
						(country of address 1 of (item 1 of thePersonId)) as string, ¬
						(street of address 2 of (item 1 of thePersonId)) as string, ¬
						(zip of address 2 of (item 1 of thePersonId)) as string, ¬
						(city of address 2 of (item 1 of thePersonId)) as string, ¬
						(country of address 2 of (item 1 of thePersonId)) as string, ¬
						(organization of (item 1 of thePersonId)) as string, ¬
						(job title of (item 1 of thePersonId)) as string, ¬
						(department of (item 1 of thePersonId)) as string, ¬
						(phone of (item 1 of thePersonId)) as string}
			
			if thePersonFirstName is missing value then return "" as string
			if thePersonLastName is missing value then return "" as string
			if thePersonFullName is missing value then return "" as string
			if thePersonWorkAddressStreet is missing value then return "" as string
			if thePersonWorkAddressZip is missing value then return "" as string
			if thePersonWorkAddressVille is missing value then return "" as string
			if thePersonWorkAddressPays is missing value then return "" as string
			if thePersonHomeAddressStreet is missing value then return "" as string
			if thePersonHomeAddressZip is missing value then return "" as string
			if thePersonHomeAddressVille is missing value then return "" as string
			if thePersonHomeAddressPays is missing value then return "" as string
			if thePersonCompany is missing value then return "" as string
			if thePersonJob is missing value then return "" as string
			if thePersondepartment is missing value then return "" as string
			if thePersonWorkPhone is missing value then return "" as string
		end tell
		
		set theErrorTitle to "Failed to add contact details to filemaker" -- since now it is up to Filemaker to do the job, let's change the message to convey to the user in case something goes bad...
		tell application "FileMaker Pro Advanced"
			activate
			tell current record -- telling the current record once makes things faster instead of telling it x number of times, plus simplifies the reading for you! ;-)
				set the contents of field "Nom de Famille" to thePersonLastName
				set the contents of field "Prénom" to thePersonFirstName
				set the contents of field "Service" to thePersondepartment
				set the contents of field "Poste" to thePersonJob
				set the contents of field "T Rue" to thePersonWorkAddressStreet
				set the contents of field "T Code Postale" to thePersonWorkAddressZip
				set the contents of field "T Ville" to thePersonWorkAddressVille
				set the contents of field "T Pays" to thePersonWorkAddressPays
				set the contents of field "Rue" to thePersonHomeAddressStreet
				set the contents of field "Code Postale" to thePersonHomeAddressZip
				set the contents of field "Ville" to thePersonHomeAddressVille
				set the contents of field "Pays" to thePersonHomeAddressPays
				set the contents of field "Téléphone Entreprise" to thePersonWorkPhone
				set the contents of field "Téléphone Mobile" to thePersonWorkCell
			end tell
		end tell
	end if
on error theError number theErrorNumber
	if theErrorNumber ≠ -1700 then error (theErrorTitle & return & return & theErrorNumber & " : " & theError) as string -- -1700 is user cancelled
end try

Browser: Safari 531.9
Operating System: Mac OS X (10.6)

Thank You Very much I’m learning and trying to get my head round this all, through trail and much error. The order is fine I can deal with that the other aspects such as looping I find a little hard for the moment. Once again thanks.

Ça me fait plaisir… :slight_smile: