• 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: Christopher Stone <email@hidden>
  • Date: Mon, 22 Sep 2014 21:37:37 -0500

[Hell; sent off-list again...]

On Sep 21, 2014, at 16:33, Bill Vlahos <email@hidden> wrote:
How do I AppleScript a search of the Contacts application if I know the phone number? The application is a CallerID lookup so I just need to return the first name in the Mac Contacts application that has the phone number I’m searching for.

This script works to find every contact by name.
tell application "Contacts"
get name of first person whose name contains "Bill"
end tell
______________________________________________________________________

Hey Bill,

You can do something like this:

-------------------------------------------------------------------------------------------
tell application "Contacts"
  properties of first person where value of its phones contains "(###) ###-####"
end tell
-------------------------------------------------------------------------------------------

This is tricky though.  If the format of the number in Contacts doesn't exactly match the query number it will not be found.

Just for giggles I threw this together.  At the moment it only finds on the area code, but it'd be easy to make that a whole phone number.  If I was being serious I'd use the Satimage.osax, or a Mavericks Library, or Perl for more sophisticated regular expressions than sed uses.

-------------------------------------------------------------------------------------------
# Auth: Christopher Stone
# Task: Constructs a table of Contacts with the found area code with number, name, ID.
# Tags: @Contacts, @Address_Book, @Address Book, @Find, @Value, @Phone Number, @Telephone
# Test: OSX 10.9.5
-------------------------------------------------------------------------------------------
set foundList to {}
set areaCode to "405"

tell application "Contacts"
  set reco to {id, value of phones, name} of every person
end tell

repeat with i from 1 to length of item 1 of reco
  repeat with n in (item i of item 2 of reco)
    set end of foundList to (contents of n) & tab & item i of item 3 of reco & tab & item i of item 1 of reco
  end repeat
end repeat

set AppleScript's text item delimiters to linefeed
set foundList to foundList as text

set foundList to do shell script "<<< " & quoted form of foundList & ¬
" sed -En '/^\\(" & areaCode & "\\)/p' | sort -n | column -t -s' '"
-------------------------------------------------------------------------------------------

If I was doing this on a regular basis I'd export a query file that got updated anytime the Contacts database was changed and then do the actual lookup on it instead of in Contacts directly.

That would be simple and lightning fast.

I'd also take the time to make sure all of my phone numbers were consistent.

There's also the possibility of doing an actual SQL query with the Contacts database, although I haven't fiddled with this and don't know how flexible the MySQL query language is.

--
Best Regards,
Chris

 _______________________________________________
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: 
 >Search Contacts for phone number (From: Bill Vlahos <email@hidden>)

  • Prev by Date: Re: Search Contacts for phone number
  • Next by Date: Re: automating e-mail generation with a script
  • Previous by thread: Re: Search Contacts for phone number
  • Next by thread: Re: Search Contacts for phone number
  • Index(es):
    • Date
    • Thread