• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Search Contacts for phone number
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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

  • Follow-Ups:
    • Re: Search Contacts for phone number
      • From: Shane Stanley <email@hidden>
References: 
 >Re: Search Contacts for phone number (From: Shane Stanley <email@hidden>)

  • Prev by Date: Re: Search Contacts for phone number
  • Next by Date: Re: Search Contacts for phone number
  • Previous by thread: Re: Search Contacts for phone number
  • Next by thread: Re: Search Contacts for phone number
  • Index(es):
    • Date
    • Thread