【问题标题】:How do I get (people whose id of addresses contains addressId) in JXA?如何在 JXA 中获取(地址 id 包含 addressId 的人)?
【发布时间】: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


    【解决方案1】:

    也许更像……

    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
    

    【讨论】:

    • 谢谢;我真正要寻找的是包含该地址的人(最终,该城市的所有人)。我是否正确,您的代码重写了我的示例中收集地址的部分,但行数更少?使用逗号作为行尾分隔符而不是分号是否有意义?缩进让我觉得有,但如果我用分号替换逗号,代码功能或执行时间似乎没有任何区别。
    • 逗号继续 const 声明。我声明了 3 个常量。 const 类似于 var,但一旦分配,它可能永远不会改变。它不是行分隔符。用分号替换逗号会将第 2 和第 3 更改为未声明的变量。这样不好。但它仍然有效。 JXA 程序员在声明方面要松懈得多。我来自多年的 javaScript,我更喜欢声明 var 或 const。
    猜你喜欢
    • 2022-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-23
    相关资源
    最近更新 更多