• 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: Shane Stanley <email@hidden>
  • Date: Tue, 23 Sep 2014 14:38:56 +1000

On 23 Sep 2014, at 12:37 pm, Christopher Stone <email@hidden> wrote:

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.

I think it's actually an SQLite database, if that makes any difference. 

FWIW, you can do powerful searches in Objective-C, which means you can do them in AppleScriptObjC, but the search method is reasonably complex. Here's how you would do the search for the name of all people with a phone number containing "805":

use framework "Foundation"
use framework "AddressBook"

on testIt()
-- build the search
set searchElement to current application's ABPerson's searchElementForProperty:(current application's kABPhoneProperty) label:(missing value) |key|:(missing value) value:"805" comparison:(current application's kABContainsSubString)
-- get the address book
set theAddressBook to current application's ABAddressBook's sharedAddressBook()
-- perform the search
set theFinds to theAddressBook's recordsMatchingSearchElement:searchElement
-- extract the names of the found people
set theFirstnames to theFinds's valueForKey:(current application's kABFirstNameProperty)
set theLastnames to theFinds's valueForKey:(current application's kABLastNameProperty)
set theResult to {}
repeat with i from 1 to count of theFirstnames
set end of theResult to ((item i of theFirstnames) as text) & space & (item i of theLastnames)
end repeat
return theResult
end testIt

In the search element, you could, for example, enter a label of "current application's kABAddressHomeLabel" to restrict the search to Home phone numbers. And the key value lets you search on, say, just the street or city name in addresses. You can also combine multiple searches, and search for different types of matches.

So on my iMac with my contacts, the original code I posted took about 11 seconds. Nigel's version, getting the records first, cut that down to about 0.12 seconds. The code above takes about 0.004 seconds. Not that I worry about such things, of course...

-- 
Shane Stanley <email@hidden>
<www.macosxautomation.com/applescript/apps/>

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

  • Prev by Date: Re: automating e-mail generation with a script
  • 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