Filemaker Applescript

Sorry I posted This in the wrong forum:
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]

Hi,

this is a “cleaned up” version of your script which retrieves al desired information


property emptyString : ""

try
	tell application "Address Book" to set unsortedList to name of people
	
	set old_delims to text item delimiters
	set text item delimiters to linefeed
	set theList to paragraphs of (do shell script "echo " & quoted form of (unsortedList as string) & "| sort -d -f")
	set text item delimiters to old_delims
	set thePersonChosen to (choose from list theList with prompt " Please choose a Person :")
	if thePersonChosen is false then return
	set thePersonChosen to item 1 of thePersonChosen
	
	--------------------------------------------------------------------------
	tell application "Address Book"
		set thePersonId to 1st person whose name is thePersonChosen
		tell thePersonId
			set thePersonFirstName to my computeValue(first name)
			set thePersonLastName to my computeValue(last name)
			set thePersonFullName to my computeValue(its name)
			if thePersonFullName is emptyString then error
			set {thePersonWorkAddressStreet, thePersonWorkAddressZip, thePersonWorkAddressVille, thePersonWorkAddressPays} to my getAddress(its addresses, "work")
			set {thePersonHomeAddressStreet, thePersonHomeAddressZip, thePersonHomeAddressVille, thePersonHomeAddressPays} to my getAddress(its addresses, "home")
			set thePersonCompany to my computeValue(organization)
			set thePersonJob to my computeValue(job title)
			set thePersondepartment to my computeValue(department)
			set thePersonWorkPhone to my getPhone(phones, "work")
		end tell
	end tell
on error e
	error "Failed to retrieve Contact details" & return & e
end try
---------------------------------------------
try
	tell application "FileMaker Pro Advanced"
		activate
		tell current record
			set contents of field "Nom de Famille" to thePersonLastName
			set contents of field "Prénom" to thePersonFirstName
			set contents of field "Service" to thePersondepartment
			set contents of field "Poste" to thePersonJob
			set contents of field "T Rue" to thePersonWorkAddressStreet
			set contents of field "T Code Postale" to thePersonWorkAddressZip
			set contents of field "T Ville" to thePersonWorkAddressVille
			set contents of field "T Pays" to thePersonWorkAddressPays
			set contents of field "Rue" to thePersonHomeAddressStreet
			set contents of field "Code Postale" to thePersonHomeAddressZip
			set contents of field "Ville" to thePersonHomeAddressVille
			set contents of field "Pays" to thePersonHomeAddressPays
			set contents of field "Téléphone Entreprise" to thePersonWorkPhone
			set contents of field "Téléphone Mobile" to thePersonWorkCell
		end tell
	end tell
on error e
	error "Failed to add contact details to filemaker" & return & e
end try

on computeValue(val)
	if val = missing value then return emptyString
	return val
end computeValue

on getAddress(adr, lab)
	tell application "Address Book"
		repeat with anAddress in adr
			if label of anAddress is lab then
				return {my computeValue(street of anAddress), my computeValue(zip of anAddress), my computeValue(city of anAddress), my computeValue(country of anAddress)}
			end if
		end repeat
	end tell
	return {emptyString, emptyString, emptyString, emptyString}
end getAddress

on getPhone(pho, lab)
	tell application "Address Book"
		repeat with aPhone in pho
			if label of aPhone is lab then
				return my computeValue(value of aPhone)
			end if
		end repeat
	end tell
	return emptyString
end getPhone


One small thing. There is a difference between the object “field” and “cell”. Generally “field” is used when you want to target all the records of a table* (of a database), whereas “cell” is used to target a specific field of a particular record (of a layout, window or document).

Fortunately using “current record” overrides this, so it works as expected. But really, the correct hierarchy is “record/cell”.

tell current record
set cell “some name” to “donuts”
end

You can see this in this (old) file from FileMaker (which used to be in the app download, but isn’t anymore, sadly):
http://fmdl.filemaker.com/MISC/fmp10/fp/apple_events_reference_wwe.zip

“Object Hierarchy” layout, cute chart. “field” is contained by “table”, “cell” is contained by “record”

“database” vs “document” (or “window” or "layout) are similarly different from “field” and “cell”

The hierarchy mirrors the basic difference between all records of a FileMaker file, and the found set of a particular window. AppleScript has the ability to see data from all records, no matter what the found set is, using “table” and “field”; FileMaker itself can only access the found set (or its related records).

Usually it does not matter, but sometimes it does.

  • “table” actually means, in this case (and most cases) “table occurrence” (on the Relationship Graph). Both AppleScript and FileMaker document use the term “table” instead, so it can be confusing. The real base tables in FileMaker are not exposed.