Finding contacts in Address Book by email address

Hello!

I am a newbie here and apologize if I am repeating a question - I tried to search for it, but no luck…

I am trying to find contacts in my Address Book using their email address. For example, search for “test@test.org” and get back the Person whose name is “Happy Tester” whose email address is “test@test.org” (more accurately, get back the set of people…)

My script works, but it is very slow as it iterates over the set of people. I cannot figure out how to make it more elegant (and efficient).

Here is what I have:


using terms from application "Address Book"
	tell application "Address Book"
		-- person has emails; emails is a list of items with label & value
		-- want people with email list which includes an item with the value "blah"
		set setOfPeople to {}
		repeat with thisPerson in people
			set passingEmails to (every email whose value is "blah") in thisPerson
			if (count of passingEmails) is greater than 0 then
				copy thisPerson to the end of setOfPeople
			end if
		end repeat
		
		repeat with thisPerson in setOfPeople
			set nameOfThisPerson to name of thisPerson
			display dialog nameOfThisPerson
		end repeat
		
	end tell
end using terms from

As you can see, I iterate over the set of people and for each person get the set of their email addresses which have the value “blah”. If that set is non-empty, I keep track of the person. After going over the entire set of people I have the list of people with matching email addresses.

It seems like there should be a way to rewrite this. The trouble I am having is that each person has a set of emails, and each email has a value (and label and id); so I need to hold on to the person as I try to filter the set of emails by their values.

Any tips would be most appreciated - hopefully this post is not too convoluted.

Thanks much,

Bart

Hi Bart,

does this help at all?

tell application "Address Book"
	set setOfPeople to {}
	repeat with this_person in people
		if value of email of this_person contains "blah" then
			copy name of this_person to end of setOfPeople
		end if
	end repeat
	choose from list setOfPeople
end tell

Thanks,
Nik

Hi

Try this :

tell application "Address Book"
	set setOfPeople to name of people whose value of emails contains "blah-----@-----.com"
	choose from list setOfPeople
end tell