AppleScripting the Address Book

I need a bit of help accessing the phone & e-mail addresses in OS X’s Address Book. I can access the name fields just fine, but there is something about indexed phone and -emial fields I must be missing. My goal is to pull up a list of names like ‘Jones’ and have a dialog display them all with their first name, last name, phone & e-mail address.

Any help would be appreciated.

Bill

You just have to understand how Address Book links the email and phone classes to the person class. This works for me:

Jon


[This script was automatically tagged for color coded syntax by Script to Markup Code]

This might be close to what you want. It was quickly written and doesn’t include much in the way of error checking but that will allow you to add your own touch to the script. :wink:

set dd to (display dialog "Enter a last name" default answer "")
set nameEntered to text returned of dd
if nameEntered is "" then return
set phone_text to ""
set email_text to ""
set complete_ to "                                                                  "

tell application "Address Book" to ¬
	set persons_ to every person whose name contains nameEntered

if (count of persons_) is 0 then
	display dialog "No person found with last name "" & nameEntered & """ buttons ¬
		{"OK"} default button 1
	return
end if

tell application "Address Book"
	repeat with person_ in persons_
		tell person_
			set name_ to name
			
			set phones_ to phones
			repeat with phone_ in phones_
				set phone_text to phone_text & return & label of phone_ & ": " & value of phone_
			end repeat
			
			set emails_ to emails
			repeat with email_ in emails_
				set email_text to email_text & return & label of email_ & ": " & value of email_
			end repeat
			if phone_text is not "" or email_text is not "" then
				set complete_ to complete_ & return & "--" & return & name_ & phone_text & email_text & return & "--" & return
			end if
		end tell
		set name_ to ""
		set phone_text to ""
		set email_text to ""
	end repeat
end tell

choose from list (paragraphs of complete_) with prompt "" with empty selection allowed and multiple selections allowed

– Rob

Rob, it looks like you and I were pretty much on the same track.

Jon

Yup. Great minds… :wink:

– Rob

Hey Rob & jonn8:

Thanks so much for the prompt and, what appear to be, thorough replies. I’ll give them a try …

Once again thanks!

Bill

Wow guys!

These are just plug and play. Much appreciated. I will study the code to see what I need to learn to write more scripts of my own in the future.

Now that’s service!

Bill

Glad to help, Bill. Here’s the version that I’ve added to my collection. It’s very similiar to my first offering but I like this one better.

set dd to (display dialog "Enter a last name" default answer "")
set nameEntered to text returned of dd
if nameEntered is "" then return
set phone_text to ""
set email_text to ""
set complete_ to "The following entries were found in Address Book            "

tell application "Address Book" to ¬
	set persons_ to every person whose name contains nameEntered

if (count of persons_) is 0 then
	display dialog "No person found with last name "" & nameEntered & """ buttons ¬
		{"OK"} default button 1
	return
end if

repeat with person_ in persons_
	tell application "Address Book"
		tell person_
			set name_ to name
			set phones_ to phones
			repeat with phone_ in phones_
				set phone_text to phone_text & ¬
					(return & label of phone_ & ": " & value of phone_)
			end repeat
			set emails_ to emails
			repeat with email_ in emails_
				set email_text to email_text & ¬
					(return & label of email_ & ": " & value of email_)
			end repeat
		end tell
	end tell
	
	if phone_text is not "" or email_text is not "" then ¬
		set complete_ to complete_ & (return & ¬
			return & name_ & phone_text & email_text)
	
	set name_ to ""
	set phone_text to ""
	set email_text to ""
end repeat

choose from list (paragraphs of (complete_ & return)) ¬
	with prompt "" with empty selection allowed without multiple selections allowed

– Rob

Yep, glad to help. I amended my script too but I modified my original post. See above for my modified script.

Jon

bjast, I highly recommend any script written by either Jon, Rob or many of the others who post solutions on this BBS. The magic these folks perform daily is truly better than any Siegfried & Roy show, sans the lions. Anyhow, I am a regular user of scripts written by these people. Rob has a script called “Safari Selection to Script Editor” that allows you to highlight scripts from a browser window, then you can run his script and it will open your selection in the Script Editor of your choice. Jon has a script named “Convert Script to MarkUp Code” that allows you to colorize your scripts so that they match the syntax in your script editor when you post your scripts to HTML/BBC pages/forums. And that’s just a small sampling of their expertise. Jon, Rob, Camelot, NG, TJ, JP and a long list of the many others, who offer their help and support to the AppleScript community, deserve a lot of thanks. ;¬)

http://scriptbuilders.net/category.php?search=jonathan+nathan
http://homepage.mac.com/jonn8/as/
http://scriptbuilders.net/category.php?search=jorgensen

Thanks Greg:

I feel like I’ve found a strong resource for learning AppleScript. I’ve read books about it, but needed a resource like this to get me past the startup stage. I’ll check out the other scripts you mentioned.

Much appreciated.

Bill

Greg, you are very kind and generous. Thank you for your kind words and, more importantly, for all that YOU do for the scripting community.

One of the oft unseen benefits of helping others is the learning process that it provides for those offering help. I have learned much by helping others, and I’ve accumulated some very handy scripts that I hadn’t even considered writing. It’s a win-win situation that I am happy to be a part of. :slight_smile:

I join you in offering kudos and thanks to the talented scripters who share their time and expertise, and to those who ask the questions that inspire them. Thanks also goes to the entire MacScripter.net crew for working tirelessly to provide a wide variety of valuable resources, and to MacServe.net, the host whose behind-the-scenes efforts go largely unnoticed due to the fine service that they provide.

Respectfully and humbly,
Rob

Yet again, Rob and I are of one mind. He’s said everything I wanted to say–and with more eloquence.

Thanks to all.

Jon