retrieving a phone number from Address Book

I just wanted to post a little success i have had in finding a simple way to get a phone number from a named contact in Address Book. I am integrating this into a Filemaker application. I have had some help from viewing work of a number of people here, so Thanks to all of you.



set fullname to "Herb Isaacs"

tell application "Address Book"
	
	activate
	set thePeople to every person whose name = fullname
	repeat with thePerson in thePeople
		
		set mphone to value of (every phone of thePerson whose label = "Mobile")
		display dialog "Mobile Phone " & mphone
		return
	end repeat
		
end tell


Hi hiconcepts,

Code Exchange is the appropriate forum to “Share your coding ideas freely”.
But thanks for sharing and good luck with your Filemaker application.

That’s fine for some cases but it’s not very flexible and it it relies on your finding only
one person. You use the variable name “mphone”, suggesting a unique entity, for what
is not a string but a list. This means you are getting the result you want in this case
only because AppleScript coerces a list to a delimited string when you do ˜list & string’
as you do for your dialogue.

Herb may have three mobile phones or there may be two Herbs. In that case you will
get a string of mobile numbers all joined together using AppleScript’s text item
delimiter[s], which will usually be {“”}” in fact “” because the AppleScript people never
actually allowed more than one delimiter.

Here’s a script that will be more useful. Rather than bother with building a string for
display, I have kept the hits as a list and used the list dialogue. I’ve also rather
overdone the ˜the’s to combat one of my pet hates, and that is the use of ˜the’ as a
prefix to variable labels as in theMobile . AppleScript was designed to read as far as
possible like human language and the optional ˜ the ’ is allowed to make it more so.
It is therefore rather silly to use ˜the…’ as a variable name, and this bad practice
grew up a long time ago. I personally use the _underscore at the beginning of every
variable label because that allows me not only to use human-like variables but also
to use ˜the _list’ instead of just ˜_list ’ when it seems to flow better.

Anyway, try this

set the _search_string to "Jln"
tell application "Address Book"
	set _people to the people whose name contains the _search_string
	set the _list to {}
	repeat with _person in _people
		set _full_name to the _person's name
		set _mobile_numbers to the value of ¬
			(every phone of the _person whose label = "Mobile")
		set _index to 0
		repeat with _number in _mobile_numbers
			set _index to _index + 1
			set the end of the _list to ¬
				_full_name & space & "Mobile " & _index & ": " & the _number
		end repeat
	end repeat
end tell
if the _list is {} then
	display dialog return & "No hits" giving up after 2
	return
end if
choose from list _list

Moved it.

Hi. This is purely a matter of personal preference, but I really don’t see the human-reading value in adding underscores to anything. Not that my code is famous for its legibility :smiley: , but I find underscores (and other symbols) anti-legible. If it makes it more descriptive or is necessary (due to a reserved word), I do occasionally use “the” as a prefix to a camelCase variable, but I otherwise don’t bother with making AppleScript conform to English; it may be descendant of English, but it’s a unique language with attendant distinctions and advantages over the strictures of its parent.


tell application "Address Book" to my Message(((people whose name is "John Doe")'s phones whose label is "Mobile")'s value)

on Message(theData) --> if > 1 person, data = a list of lists
	--do things with item(s) in Filemaker
end Message

–edited out superfluous existence check

Hi hiconcepts. Welcome to MacScripter and thanks for sharing your script.

It turns out that Address Book (in Snow Leopard, at least) is capable of some ridculously complex ‘whose’ filters (Edit: Inner ‘whose’ filter shortened in response to Marc Anthony’s observation in the following post):

set fullname to "Herb Isaacs"

tell application "Address Book"
	set nestedLists to (value of phones of (people whose name is fullname) whose label is "mobile")
end tell

set flatList to {}
repeat with thisList in nestedLists
	set flatList to flatList & thisList
end repeat

if (flatList is {}) then
	display dialog "No mobile numbers found for  "" & fullname & ""."
else
	choose from list flatList with prompt "Mobile number(s) found for "" & fullname & "":"
end if

But if you have a very large number of contacts in your Address Book, ‘whose’ filters could turn out to be quite slow. A faster method would be to get Address Book simply to return every single name and the details of every single phone it has and to let the script sift through the lists of text:

set fullname to "Herb Isaacs"

tell application "Address Book"
	set {allNames, {allPhoneLabels, allPhoneNumbers}} to {name, {label, value} of phones} of people
end tell

set flatList to {}
repeat with i from 1 to (count allNames)
	if (item i of allNames is fullname) then
		repeat with j from 1 to (count item i of allPhoneLabels)
			if (item j of item i of allPhoneLabels is "Mobile") then
				set end of flatList to item j of item i of allPhoneNumbers
			end if
		end repeat
	end if
end repeat

if (flatList is {}) then
	display dialog "No mobile numbers found for  "" & fullname & ""."
else
	choose from list flatList with prompt "Mobile number(s) found for "" & fullname & "":"
end if

Hi, Nigel. and label of phones contains “mobile” is superfluous in your interpretation.

An interesting side note: It appears that you need to physically type the hyphens, when you enter phone data into Address Book. If you don’t, it visually fakes them in the entry, but it strings the numbers together in the AS result.

Hmmm. It does seem to be. Thanks! I’ve edited my post accordingly.

The automatic formatting of phone numbers in the display can be turned on or off in Address Book’s preferences. I have it turned off as it doesn’t alway show UK numbers the way I want to see them.