[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
______________________________________________________________________
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
|