Address book: Find person by phone number

Did anyone search for an address book contact via phone number (mobile, fixed, …)?

Tried to replace “emails” in this code by “phones”

http://macscripter.net/viewtopic.php?pid=116263#p116263

tell application "Address Book"
   set setOfPeople to name of people whose value of emails contains "0"
   choose from list setOfPeople
end tell

but no contact was found although there are a lot of contact numbers with a “0” in a phone number. Thanks in advance!

This worked for me.

tell application "Address Book"
	set myList to name of (people whose value of phones contains "530-751-2345")
	choose from list (myList)
end tell

Are you using “contains 0” to mean “has no phone number”?

Thanks, this works well! But I have only a part of the whole telephone number, so I want to look for telephone numbers that contain e.g. “123” and retrieve that people. Is that possible?

I didn’t test mikerickson’s script, but it should work with his script. When you know the exact telephone number, you could use the following:


tell application "Address Book"
   set myList to name of (people whose value of phones is "530-751-2345") -- pay attention to the 'is'
   choose from list (myList)
end tell

If you know only a part of the phone number, like in your case, use mikerickson’s script:

tell application "Address Book"
   set myList to name of (people whose value of phones contains "123") -- pay attention to the 'contains'
   choose from list (myList)
end tell

Hope it helps,
ief2

ief,
I tried your script and it failed.
The problem is that phones of SomePerson is a list of complete phone numbers { “530-752-1234”, “530-756-9876”}

{ “530-752-1234”, “530-756-9876”} contains “752”

will be true only if one of the list entries is exactly equal to “752”

What the OP is looking for is People whose Value of Phones contains (any string which contains “123”)

Which I don’t know how to script without looping.

Oh, well in that case, I suggest the use of a loop

Are you looking for a specific part of the number? As in, is 123 the area code (123-456-7890), or do you want to look anywhere in the phone number?

Hi,

Here is a solution with a small dash of “Address Book Programming” in Cocoa:

The short and simple foundation tool absearch quickly searches the Address Book for contacts containing the given phone number part and returns their unique ID, first, last & company name.

The AppleScript then parses the returned absearch result and displays a list of contact names. After the user chose a name, it retrieves its unique ID and can act on the contact.

Here is the ready for use script, it will run on Mac OS X 10.5 or higher.

Hope that helps.

Best regards from ice-cold Berlin,

Martin


-- author: Martin Michel
-- eMail: martin@joyofscripting.com
-- created: 30.01.2010

property mytitle : "phone search"

-- I am called when the user opens the script with a double click
on run
	try
		-- asking the user to provide the search string
		set searchstring to my askforsearchstring()
		if searchstring is missing value then
			return
		end if
		
		-- searching the address book with a command line tool
		set qtdtoolpath to quoted form of POSIX path of (((path to me) as text) & "Contents:Resources:absearch")
		set command to qtdtoolpath & " -phone " & quoted form of searchstring
		set outputlines to paragraphs of (do shell script command)
		
		-- no persons found :-(
		if outputlines is {} then
			set errmsg to "Unfortunately the search did not find matching persons."
			my dsperrmsg(errmsg, "--")
			return
		end if
		
		-- processing the result
		set personids to {}
		set personnames to {}
		
		set countoutputlines to length of outputlines
		
		repeat with i from 1 to countoutputlines
			set outputline to item i of outputlines
			set {personid, personname} to my gettxtitems(tab, outputline)
			set personids to personids & personid
			set personnames to personnames & ((i & space & personname) as text)
		end repeat
		
		-- letting the user choose from a list of found persons
		set chosenpersonidx to my getchosenpersonidx(personnames)
		if chosenpersonidx is missing value then
			return
		end if
		
		-- getting the person id
		set personid to item chosenpersonidx of personids
		
		tell me
			activate
			display dialog "ABPerson UniqueID:" & return & return & personid
		end tell
		
		-- example how to further process the found person with AppleScript
		tell application "Address Book"
			activate
			set p to item 1 of (every person whose id is personid)
		end tell
		
	on error errmsg number errnum
		my dsperrmsg(errmsg, errnum)
	end try
end run

-- I am asking the user to provide the part of a phone number to search for
on askforsearchstring()
	try
		tell me
			activate
			display dialog "Please enter the part of a phone number to search:" default answer "" buttons {"Cancel", "Enter"} default button 2 with icon note
			set dlgresult to result
		end tell
		set searchstring to text returned of dlgresult
		if searchstring is "" then
			my askforsearchstring()
			return
		else
			return searchstring
		end if
	on error
		return missing value
	end try
end askforsearchstring

-- I am returning the list index of a chosen person
on getchosenpersonidx(personnames)
	choose from list personnames with title mytitle with prompt "Please choose a person:" without multiple selections allowed and empty selection allowed
	set choice to result
	if choice is not false then
		set chosenpersonname to item 1 of choice
		set chosenpersonnameparts to my gettxtitems(space, chosenpersonname)
		set chosenpersonidx to (item 1 of chosenpersonnameparts) as integer
		return chosenpersonidx
	else
		return missing value
	end if
end getchosenpersonidx

-- I am returning the text items of a text separated by the given delimiter
on gettxtitems(delim, txt)
	set olddelims to AppleScript's text item delimiters
	set AppleScript's text item delimiters to {delim}
	set txtitems to text items of txt
	set AppleScript's text item delimiters to olddelims
	return txtitems
end gettxtitems

-- I am displaying error messages to the user
on dsperrmsg(errmsg, errnum)
	tell me
		activate
		display dialog "Sorry, an error occurred:" & return & return & errmsg & " (" & errnum & ")" buttons {"OK"} default button 1 with icon stop with title mytitle
	end tell
end dsperrmsg

Here’s a looping method

set listOfFolks to {}
set partialNumberSought to "75"

tell application "Address Book"
	repeat with onePerson in people
		if (value of phones of onePerson as string) contains partialNumberSought then
			copy name of onePerson to end of listOfFolks
		end if
	end repeat
end tell

if length of listOfFolks = 0 then
	display dialog "No one has a phone number that contains " & partialNumberSought
else
	choose from list listOfFolks with title (partialNumberSought) & "Holders"
end if

mikerickson,

I am trying to figure out how to have address book open up the selection at the end of your script.

The script that you provided works for me, but after you choose the person that you want, nothing happens, and I want it to open up that person in the address book. How do I go about doing that?

Here is the script you provided:


set listOfFolks to {}
set partialNumberSought to "75"

tell application "Address Book"
   repeat with onePerson in people
       if (value of phones of onePerson as string) contains partialNumberSought then
           copy name of onePerson to end of listOfFolks
       end if
   end repeat
end tell

if length of listOfFolks = 0 then
   display dialog "No one has a phone number that contains " & partialNumberSought
else
   choose from list listOfFolks with title (partialNumberSought) & "Holders"
end if 

Thanks and really appreciate it.

Regards,

Phil

Hi.

Where the phone values are coerced to string, AppleScript’s text item delimiters should be set to something which is neither the default {“”} nor part of the search string, otherwise you could get false positives.


set listOfFolks to {}
set partialNumberSought to "75"

set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to return
tell application "Address Book"
	repeat with onePerson in people
		if (value of phones of onePerson as string) contains partialNumberSought then
			set end of listOfFolks to name of onePerson
		end if
	end repeat
end tell
set AppleScript's text item delimiters to astid

if (listOfFolks is {}) then
	display dialog "No one has a phone number that contains " & partialNumberSought
else
	set theChoice to (choose from list listOfFolks with title (partialNumberSought) & "Holders")
	if (theChoice is false) then error number -128
	tell application "Address Book" to set selection to person (item 1 of theChoice)
end if

But in fact it’s faster to get Address Book to return the values in bulk and to loop through them with vanilla AppleScript than it is to extract them from the application individually:


set listOfFolks to {}
set partialNumberSought to "75"

tell application "Address Book" to set {phoneNumbers, peoplesNames} to {value of phones, name} of people
repeat with p from 1 to (count peoplesNames)
	repeat with aPhone in item p of phoneNumbers
		if (aPhone contains partialNumberSought) then
			set end of listOfFolks to item p of peoplesNames
			exit repeat
		end if
	end repeat
end repeat

if (listOfFolks is {}) then
	display dialog "No one has a phone number that contains " & partialNumberSought
else
	set theChoice to (choose from list listOfFolks with title (partialNumberSought) & "Holders")
	if (theChoice is false) then error number -128
	tell application "Address Book" to set selection to person (item 1 of theChoice)
end if

Neat. Easy and brisk to extract the whole number as well.