【发布时间】:2019-03-20 16:21:45
【问题描述】:
我在 MacOS Mojave 10.14.3 上使用带有 osascript 的 JavaScript 来编写一个脚本,用于显示联系人及其联系人信息。我正在添加一个功能来显示特定城市中的所有人。这是在脚本编辑器中运行的简化版本:
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) 总是返回一个人,因为地址 ID 是唯一的,因此只有一个人的地址列表可以包含任何特定的地址 ID。
如果在 JavaScript 或 AppleScript 中有更好的方法可以让联系人按他们的地址之一的城市从联系人中获取,我会对此感兴趣。
但我的问题是,有没有一种方法可以使用 JavaScript 复制 first item of (people whose id of addresses contains addressId) 的功能,从而得到一个地址与该地址 id 匹配的人?
【问题讨论】:
标签: macos javascript-automation