How to Get one specific contact?

Because the find contact action in shortcuts is too slow to get one contact I would like to fetch the contact with scriptable.

I used example scripts to get an identifier. But it gives me an error.

  • Is there a way to fetch just 1 contact?
  • can I fetch a contact on the basis of name or phone number in stead of uuid?

Thnx!
René

// get list of contacts with their identifier
let containers = await ContactsContainer.all()
let contacts = await Contact.all(containers)
contacts.forEach(function(contact){console.log(contact.identifier + " " + contact.familyName);})
// 1639C0F9-02C1-4553-888E-5CF4ADCE5E5F


// get one contact
let containers2 = await ContactsContainer.withIdentifier('1639C0F9-02C1-4553-888E-5CF4ADCE5E5F')
let contacts2 = await Contact.all(containers2)
contacts2.forEach(function(contact2){console.log(contact2.identifier + " " + contact2.familyName);})

// Result: 
// 2020-06-30 08:51:14: Error: The contacts container could not be found.

‘’’

In case it helps on the Shortcuts front, you can efficiently limit the contacts using this approach.

When posting code, wrap it in triple back ticks and it will format the code.

// get list of contacts with their identifier
let containers = await ContactsContainer.all()
let contacts = await Contact.all(containers)
contacts.forEach(function(contact){console.log(contact.identifier + " " + contact.familyName);})
// 1639C0F9-02C1-4553-888E-5CF4ADCE5E5F

// get one contact
let containers2 = await ContactsContainer.withIdentifier('1639C0F9-02C1-4553-888E-5CF4ADCE5E5F')
let contacts2 = await Contact.all(containers2)
contacts2.forEach(function(contact2){console.log(contact2.identifier + " " + contact2.familyName);})

// Result:
// 2020-06-30 08:51:14: Error: The contacts container could not be found.

and watch out for smart quotes when copying & pasting

As I understand your code, containers is the set of contact containers, contacts is a list of contacts in the set of contact containers, and thencontact is each contact in the set of contacts in the set of contact containers. It then looks like you are passing a contact ID (identifier) to a contact container function that accepts contact container ID (withIdentifier()); so it is looking for a container with the sme ID as the contact, but they are not the same thing.

Thanks for the editing suggestions!

And pointing to the flaw in my script. I was indeed using a contact uuid for a container object. That doesn’t work. Thanks!

I didn’t understand your shortcuts suggestion. Or at least I don’t recognise the solution for my code.
In the first step do you present a list of names Or contacts within which to search? That wouldn’t work as my collection is all the contacts. Or do I misunderstand? I hope so,

I have a name as input. And the complete set of contacts in a phone as the collections to search in.

I did some tests and every query of the contacts with the standard action ‘find contact’ takes the same amount of time. None of the fields I used (or even limiting the number of returned elements to 1) had any influence on the search time.
The search time is 20 seconds on my clients iPhone. On mine it’s 2 seconds.

René

In your original post you said this:

There was no indication there that you always wanted to search all contacts, and in your JavaScript, you were attempting to use a single specific ID. My deduction therefore was that you were ultimately intending to get access directly to “one contact”.

If you are using Scrpitable, or Shortcuts to access all contacts, you are at some level retrieving information about all contacts - I’m not sure why you are necessarily expecting in that case that Scriptable would out perform Shortcuts. It might, but there’s no obvious indicator as why that would be expected.

The approach I gave was a way to effecively preload direct access to a small sub set of contacts, which are then near instant to access. I used a choose from option simply as an example to illustrate how quick and easy that then becomes.

Shortcuts is retrieving all of the contacts data and then applying a local filter on that data set. If you think of contacts as a database, … well that would be nice. Instead think of it like a bucket of data. You grab the bucket, pour it into a seive you happen to have on you and you get the data you want out. i…e you fetch everything, and then filter it outside of the bucket.