MacOS – type of emails from Contacts.app in AppleScript (on El Capitan)

applescriptcontactsemailmacos

I want to work with a list of all email addresses that appear in my address book. I thought I could simply set such a list with AppleScript, and use the elements, maybe after casting the references to strings, as this answer suggests. However, the code below runs into a casting error, with or without explicit casting (e.g. as string):

error "Can’t make «class az21» 1 of «class azf4» id
\"C74972A5-88D4-4B0F-8DEA-304914926EE6:ABPerson\" of application
\"Contacts\" into the expected type." number -1700 from «class az21» 1
of «class azf4» id "C74972A5-88D4-4B0F-8DEA-304914926EE6:ABPerson"

tell application "Contacts"
    set allEmails to every email of every person
end tell
repeat with anEmail in allEmails
    display dialog anEmail
end repeat

This problem, of course, also inhibits any more meaningful uses of the email addresses.

What is this about, and what is a workable way to collect and access all email addresses in my address book?

I am running Public Beta 5 of El Capitan, though I did not test whether the behavior is any different on older (= actual) releases.

Best Answer

allEmails is a list that contains several lists, each sublist contains emails of a contact.

email is an object, you need to get the value of this object.

So, you need a loop for each list, and another loop for each value.

tell application "Contacts"
    set myList to value of emails of people
end tell
repeat with aList in myList
    repeat with anEmail in aList
        display dialog anEmail
    end repeat
end repeat