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.
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.
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
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
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. ;¬)
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.
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.
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.