我如何在JXA中获得(地址ID包含addressId的人)? [英] How do I get (people whose id of addresses contains addressId) in JXA?
问题描述
我正在MacOS Mojave 10.14.3上使用带osascript的JavaScript来编写一个脚本,以显示联系人中的人员及其联系信息.我要添加一个功能,以显示特定城市中的所有人.这是在脚本编辑器中运行的简化版本:
I’m using JavaScript with osascript, on MacOS Mojave 10.14.3, to write a script that displays people and their contact information from Contacts. I’m adding a function to show all of the people in a particular city. Here’s a simplified version that runs in Script Editor:
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测试版本.它不会遍历所有联系人,而是遍历匹配的地址并获取其地址ID包含addressId
的人:
I have an AppleScript test version that is slightly more efficient. Rather than looping through all contacts, it loops through the matching addresses and gets 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 (地址ID包含addressId的人)
始终返回一个人,因为地址ID是唯一的,因此只有一个人的地址列表可以包含任何特定的地址ID.
The AppleScript (people whose id of addresses contains addressId)
always returns one person, because address ids are unique and therefore only one person’s list of addresses can contain any particular address id.
如果JavaScript和AppleScript中有更好的方法可以按联系人所在城市之一的方式从联系人中获取联系人,那么我会对此感兴趣.
If there are better ways in JavaScript or AppleScript to get people from Contacts by the city of one of their addresses, I’d be interested in that.
但是我的问题是,是否有一种方法可以使用JavaScript复制(地址ID包含addressId的人)的第一项的功能
,从而获得具有地址的人匹配该地址ID?
But my question is, is there a way to duplicate the functionality of first item of (people whose id of addresses contains addressId)
using JavaScript, so as to get the one person who has an address matching that address id?
推荐答案
也许更像是……
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
或者如果您想要地址...
Or if you want the address…
const contacts = Application('Contacts').people,
address = contacts.addresses.whose({city: "San Diego"})().filter(a=>a.length)[0]
address
这篇关于我如何在JXA中获得(地址ID包含addressId的人)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!