Re: Search Contacts for phone number
Re: Search Contacts for phone number
- Subject: Re: Search Contacts for phone number
- From: Paul Berkowitz <email@hidden>
- Date: Sun, 21 Sep 2014 18:41:43 -0700
- Thread-topic: Search Contacts for phone number
Title: Re: Search Contacts for phone number
On 9/21/14, 5:14 PM, "Shane Stanley" <email@hidden> wrote:
Name is a property, but phone is an element -- whose clauses only work with properties. A person can have more than one phone, and phones have values and labels, so you're going to have to do some looping. This should do what you want:
tell application "Contacts"
repeat with aPerson in people
set theNumbers to value of aPerson's phones
repeat with aNumber in theNumbers
if aNumber contains "805" then
return aPerson's name
end if
end repeat
end repeat
end tell
Except even after finding the first of these, it will keep looping and you'll never see any early returns, only the last one - if there are several people whose phone numbers contain one with "805".
Bill, I think you're going to have top be a bit more specific: can you really be certain that only one contact's phone number contains "805"? (Don't forget the 'contains' operator will also turn up phone numbers such as (310) 672-8051. Do you want a list of all of them? Then, adapt Shane's script like so:
set peopleList to {}
tell application "Contacts"
repeat with aPerson in people
set theNumbers to value of aPerson's phones
repeat with aNumber in theNumbers
if aNumber contains "805" then
set end of peopleList to aPerson's name
end if
end repeat
end repeat
end tell
return peopleList
If the same person has several phone numbers containing "805" (such as anyone living in the area code where I reside myself), you'll get his or her name several times. You can take care of that this way:.
set peopleList to {}
tell application "Contacts"
repeat with aPerson in people
set theNumbers to value of aPerson's phones
repeat with aNumber in theNumbers
repeat with aNumber in theNumbers
if aNumber contains "805" then
set end of peopleList to aPerson's name
exit repeat
end if
end repeat
end repeat
end repeat
end tell
return peopleList
The 'exit repeat' once the name has been added to the list (this exits only the inner repeat, traversing the several phones of this person) is quicker and neater than checking the list to see if the name is already there.
--
Paul Berkowitz
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden