0

I'm trying to get all the email addresses of the selected card in Apple's Contacts.app. In either language I can get the selected card into the variable selPerson. But then...

In AppleScript this works:

get the properties of every email of selPerson

result is a list of records (one for each stored email address) with several properties such as id,label (as shown in the app) and value (the actual address). Simple.

But how can I achieve the same result in JavaScript?

selPerson.emails()

returns an array of email 'references' and

selPerson.emails()[0].properties()

gives me an object with the properties for the first one in the array. But what I cannot fathom how to do is combine it all to produce an array of all email objects, i.e. one for each email address.

I've ended up kludging it using array.map(), but that is just iterating through the first array of email refs. and creating a second array of the full objects, but there must be a better way to do this. It seems one case where it's much easier in AppleScript, although other inabilities of AS mean doing this is JS is a better option. In any case, I want to understand more about JXA.

Cœur
  • 37,241
  • 25
  • 195
  • 267
UKenGB
  • 21
  • 2

1 Answers1

0

Perhaps something like this:

const
    person = matches.at(0),
    emails = person.emails;

return Array.from({
    length: emails.length
}, (_, i) => emails.at(i).properties());
houthakker
  • 688
  • 5
  • 13