Can't access information for a address book person

I have a project that has two text fields, the first name and last name.
when the person clicks submit it should find that persons name in address book and show information for it
but i can not get it to work

on populateInfo()
	set firstName to contents of text field "firstName" of window 1
	set lastName to contents of text field "lastName" of window 1
	tell application "Address Book"
		activate
		if (first person whose first name is firstName and last name is lastName) exists then
			set thePerson to first person whose first name is firstName and last name is lastName
		end if
	end tell
	if (thePerson exists) then
		-- THIS LINE WONT WORK
		display dialog last name of thePerson
	end if
end populateInfo

on end editing theObject
	if (name of theObject is "lastName") then populateInfo()
end end editing

The line:

display dialog last name of thePerson

wont woork.

It works if I am inside the tell block of the “Address Book” but not outside.
The reason I need it outside is after I get the details from thePerson I need to update some textFields in my application and when I was inside the tell block I could not access text field “lastName”

any help is appreciated
thanks

Hi IMTheNachoMan,

Try the following code:

tell application "Finder" to make file at desktop with properties {name:"Birthday export.txt"}
set exportFile to (((path to desktop) as string) & "Birthday export.txt") as alias

tell application "Address Book"
	set addressBookAsList to every person
	open for access exportFile with write permission
	repeat with i from 1 to count of addressBookAsList
		set theName to (name of item i of addressBookAsList)
		set eAddress to value of first email of item i of addressBookAsList
		set theBirthDate to (birth date of item i of addressBookAsList) as string
		write (theName & tab & theBirthDate & tab & eAddress & return) as string starting at eof to exportFile
	end repeat
	close access exportFile
	quit application "Address Book"
end tell

Optionally you can just leave out writing it down to a file.

Hi,

last name is a property of person in Address Book’s dictionary. You need to get it inside the Address Book block. Try this:

on populateInfo()
	set firstName to contents of text field "firstName" of window 1
	set lastName to contents of text field "lastName" of window 1
	tell application "Address Book"
		activate
		if (first person whose first name is firstName and last name is lastName) exists then
			set thePerson to first person whose first name is firstName and last name is lastName
			set lastName to last name of thePerson
		end if
	end tell
	if (thePerson exists) then
		display dialog lastName
	end if
end populateInfo

on end editing theObject
	if (name of theObject is "lastName") then populateInfo()
end end editing

Best wishes

John M

on populateInfo()
	set firstName to contents of text field "firstName" of window 1 -- "Adam"
	set lastName to contents of text field "lastName" of window 1 -- "Smith"
	tell application "Address Book"
		activate
		if (first person whose first name is firstName and last name is lastName) exists then -- does Adam Smith exist?
			set thePerson to first person whose first name is firstName and last name is lastName -- get Adam Smith's data
		end if
	end tell
	if (thePerson exists) then
		-- THIS LINE WONT WORK
		display dialog last name of thePerson -- display Adam Smith's last name*
	end if
end populateInfo
  • isn’t this redundant? Couldn’t you simply have said ‘display dialog lastName’? Since you were searching for a person whose first name is ‘Adam’ and last name is ‘Smith’ chances are not high that you’ll get other results than ‘Smith’ (if you don’t mind possible differences in case) …