Re: Address Book Question
Re: Address Book Question
- Subject: Re: Address Book Question
- From: Paul Berkowitz <email@hidden>
- Date: Tue, 17 Feb 2004 13:07:20 -0800
On 2/17/04 10:53 AM, "Oakley Masten" <email@hidden> wrote:
>
>
Using the following script I can get Groups and Names but can not find
>
a way to get associated email addresses of the names.
>
>
tell application "Address Book"
>
--Routine gets list of groups
>
set NomeOGroups to name of every group as list
>
set theResult to choose from list NomeOGroups with prompt "Which
>
Group Do You Want?" without multiple selections allowed
>
>
--Routine gets list of names for choice
>
set ListONames to the name of every person
>
set theResult to choose from list ListONames with prompt "Which
>
Person Do You Want?" without multiple selections allowed
>
--display dialog "You have chosen - " & theResult
>
>
--Routine gets list of addresses
>
set ListOAddresses to the email of every person
>
>
end tell
>
>
What I get is a list of records like - {
>
email 1 of person id "C0EB6745-989F-11D7-B6EE-003065423E5C:ABPerson"
>
of application "Address Book"
>
}
>
>
If I use "email address" instead of just "email I get an error and no
>
data.
>
>
If I use "address" instead of just "email" I get the same list of
>
records as above.
>
>
How do I get the associated email address for a name listing?
The short answer is "read the dictionary more carefully". You're doing the
correct thing with persons and groups - you've checked their dictionary
entries and seen that these are objects (instances of classes0 with many
properties, and that 'name of' gets you the property you want, even for a
list.
It's almost the same with 'email'. 'email' is a class with a few properties,
and 'value' is the property you want. However, every person may have several
emails - email is an element, not a property. This actually works (which
surprises me):
set ListOAddresses to value of every email of every person
You get a list of lists. When a person has no email addresses, his sublist
is the empty {}. So your result is something like:
--> {{"email@hidden", "email@hidden"}, {}, {}, {},
{"email@hidden"}, {}, {"email@hidden", "email@hidden"}, ... }
To "flatten" that into a simple one-dimensional list omitting the empty
sublists, you'd have to do something like this:
tell application "Address Book"
set ListOAddresses to value of every email of every person
end tell
set FlatAddressesList to {}
repeat with i from 1 to (count ListOAddresses)
set subEmailList to item i of ListOAddresses
if subEmailList {} then set FlatAddressesList to FlatAddressesList &
subEmailList
end repeat
FlatAddressesList
If you want only the first email address of each person (although in Address
Book there's no such thing as a 'default' email address so you won't know
which one this will turn out to be - actually the first listed in each
contact), make the relevant line:
if subEmailList {} then set end of FlatAddressesList to item 1 of
subEmailList
(Unfortunately you can't do this without the repeat loop, like:
tell application "Address Book"
set ListOAddresses to value of first email of every person
end tell
since it will error when it hits a person with no emails. But this repeat
loop is super-fast OMM.)
--
Paul Berkowitz
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.