Я использую 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, чтобы получить одного человека, у которого есть адрес, соответствующий этому идентификатору адреса?