Как я могу получить (люди, чей идентификатор адресов содержит addressId) в JXA? - PullRequest
0 голосов
/ 20 марта 2019

Я использую JavaScript с osascript на MacOS Mojave 10.14.3, чтобы написать скрипт, который отображает людей и их контактную информацию из Контактов.Я добавляю функцию, чтобы показать всех людей в определенном городе.Вот упрощенная версия, которая запускается в редакторе скриптов:

contacts = Application('Contacts').people;
matchingAddresses = contacts.addresses.whose({city: "San Diego"});

var addressIds = [];
for (var matchedAddress of matchingAddresses()) {
    if (matchedAddress.length > 0) {
        addressIds.push(matchedAddress[0].id());
    }
}

//loop contacts and get those with a matching address
var personIds = [];
for (var possiblePerson of contacts()) {
    for (var addressToCheck of possiblePerson.addresses()) {
        if (addressIds.includes(addressToCheck.id())) {
            var personId = possiblePerson.id();
            if (!personIds.includes(personId)) {
                personIds.push(personId);
            }
        }
    }
}

personIds.length;

У меня есть тестовая версия AppleScript, которая немного более эффективна.Вместо того, чтобы перебирать все контакты, он перебирает совпадающие адреса и получает people whose id of addresses contains addressId:

tell application "Contacts"
    set matchingAddresses to (every address where its city is "San Diego") of every person

    set personIds to {}
    repeat with matchedAddressList in matchingAddresses
        repeat with possibleAddress in items of contents of matchedAddressList
            set addressId to id of possibleAddress
            set matchingPerson to first item of (people whose id of addresses contains addressId)
            if not (personIds contains id of matchingPerson) then
                set end of personIds to id of matchingPerson
            end if
        end repeat
    end repeat

    count of personIds
end tell

AppleScript (people whose id of addresses contains addressId) всегда возвращает одного человека, потому что идентификаторы адресов уникальны и, следовательно, список только одного человекаадреса могут содержать любой конкретный идентификатор адреса.

Если есть лучшие способы в JavaScript или AppleScript, чтобы получить людей из Контактов по городу одного из их адресов, я бы заинтересовался этим.

Но мой вопрос: есть ли способ дублировать функциональность first item of (people whose id of addresses contains addressId) с использованием JavaScript, чтобы получить одного человека, который имеет адрес, соответствующий этому идентификатору адреса?

1 Ответ

0 голосов
/ 20 марта 2019

Может быть, что-то еще, как ...

const contacts = Application('Contacts').people,
    matchingAddresses = contacts.addresses.whose({city: "San Diego"}),
    addressIds = matchingAddresses().filter(a=>a.length).map(a=>a[0].id());

addressIds[0]; // the first id

Или, если вы хотите адрес ...

const contacts = Application('Contacts').people,
    address = contacts.addresses.whose({city: "San Diego"})().filter(a=>a.length)[0]

address
...