On Sep 27, 2014, at 16:05, Bill Vlahos <
email@hidden> wrote:
This is pretty neat. I’m actually looking for a whole number and just used the area code as an example.
______________________________________________________________________
Never do that! :)
Always use a real example, because small differences can be critical - and you can cause someone to waste a lot of time running down the wrong trail while trying to help you.
tell application "Contacts"
name of first person whose (class of (first phone whose value contains "(805) 555-5555") is phone)
end tell
This seems to work and returns the first name instead of the ID of the record.
Of course it did, because that's what the script is telling it to do (
name of ). The script is also telling it to ONLY get the first hit (
first person).
tell application "Contacts"
set foundList to people whose (class of (first phone whose value contains "(405) 880-4551") is phone)
end tell
Your quoting on this script above got messed up somewhere, so I've replaced it.
This returns multiple people but not all of the people that have the number for reasons I don’t understand.
As I mentioned before I have had problems with consistent lookups, because within the Contacts database a number that appears to be "(805) 555-5555" in the UI shows up as "805-555-5555" or "805 555-5555" or "8055555555" in the actual data returned by AppleScript.
Alright. Let's break up the number into pieces. That ought to work, unless someone forgot to enter the area code.
While this could turn up some false positives, the likelihood isn't all that great - made less so by anchoring suFix to the end with ends with suFix.
-------------------------------------------------------------------------------------------
set incomingPhoneNumber to "805-555-5555"
set AppleScript's text item delimiters to "-"
set {areaCode, preFix, suFix} to text items of incomingPhoneNumber
tell application "Contacts"
set foundList to people whose (class of (first phone whose value contains areaCode and value contains preFix and value ends with suFix) is phone)
end tell
-------------------------------------------------------------------------------------------
Note: I would never use AppleScript's text item delimiters for this sort of thing UNLESS I was ABSOLUTELY certain the number would be CONSISTENTLY formatted this way.
Personally I'd use the
Satimage.osax to strip any non-digit characters and then grab the pattern ^\d{3}\d{3}\d{4}$
1. (The Satimage.osax will error if the pattern fails to match, so that works for the format test.)
[1] This pattern presupposing US phone numbers.
-------------------------------------------------------------------------------------------
# This one returns all phone number DATA, so you can check it.
-------------------------------------------------------------------------------------------
set incomingPhoneNumber to "805-555-5555"
set AppleScript's text item delimiters to "-"
set {areaCode, preFix, suFix} to text items of incomingPhoneNumber
tell application "Contacts"
set foundList to {name, value of phone} of (people whose (class of (first phone whose value contains areaCode and value contains preFix and value ends with suFix) is phone))
end tell
-------------------------------------------------------------------------------------------
This problem of possibly varied phone number format is why I wrote my original script to return flat text that could be searched with a regular _expression_.
-------------------------------------------------------------------------------------------
# Run this for a quick visual check of how consistently your phone numbers are formatted.
-------------------------------------------------------------------------------------------
set AppleScript's text item delimiters to linefeed
tell application "Contacts"
set foundList to (value of phone of people) as text
end tell
do shell script "<<< " & quoted form of foundList & " sed -E '/^$/d' | sort -n"
-------------------------------------------------------------------------------------------
To effectively use a lookup of the type in this post you need consistently formatted data.
--
Best Regards,
Chris