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...
|