I kept at it. Seems this below will work. Not sure if this is the right way to do it.
function run(){
const Contacts = Application('Contacts');
for (var contact of Contacts.people()){
var found = contact.emails.whose({value:{_contains:"email@email.com"}})
if (found.length > 0){
return contact.name()
}
}
}
The way I figured is that Contacts.people is an array. And emails of a contact is also an array.
And if I have to use whose, it cannot be on an array of arrays.
Not sure if that is the right understanding.
I am in love with your scripts. So elegant. Unfortunately, not able to understand them well.
I even tried to learn Haskell some time ago, but got sidetracked and could not complete it.
It be so cool if you did a video about how people can learn this style of programming. I am kidding, grateful enough that you share your expertise here. There is text and videos on functional programming, but not many example js or python scripts with some explanation.
After you observed, I can see how you made two apple event calls, but I guess I will have to read about how the zip then happened, and some of the advanced js code. I will work on understanding your answer.
I will try to resume learning Haskell and need more JS expertise.
But one important question, which was a stumbling block last time.
Is there a way one can debug this code using an editor like VS Code say? Where the variables etc can flash on the side and one can create visual breakpoints? @ComplexPoint
You can step through the code in the Safari JS debugger.
In VS Code you need to insert the keyword debugger; somewhere in your source, in a line of its own, to break flow and enter the Safari Web Inspector view:
Are you saying that we can debug JXA code in VSC directly?
How do we "enter the Safari Web Inspector view" in VSC?
I don't see any option for that in the VSC View menu.
This isn't what you asked for, but here's a short AppleScript that returns a list of contacts containing a given email address, which equals {} if none exist.
tell application "Contacts"
set setOfPeople to (people whose value of emails contains "robert@apple.com")
end tell
Learning to transform from AppleScript to JXA is valuable though. More AppleScript solutions exist in the wild and JXA just seems to be more modern language and worth learning therefore.