How do I use the ObjC bridge Contacts framework in JavaScript for Automation? I am able to use the old AddressBook framework, but not the Contacts one.
Let's say I have an ID:
id = Application("Contacts").myCard().id()
The old AddressBook framework works fine:
ObjC.import("AddressBook");
book = $.ABAddressBook.addressBook
person = book.recordForUniqueId(id)
console.log(person.valueForProperty($.kABFirstNameProperty).js)
But how to use the Contacts framework?
I tried (based on the documentation for unifiedContactWithIdentifier
):
ObjC.import('Contacts')
keysToFetch = [ $.CNContactGivenNameKey, $.CNContactFamilyNameKey ]
store = $.CNContactStore
store.unifiedContactWithIdentifierKeysToFetchError( id, keysToFetch, null )
...
This fails with Error -2700: Script error. The popup says TypeError: store.unifiedContactWithIdentifierKeysToFetchError is not a function
Edit: an even more basic example, the defaultContainerIdentifier
method gives an undefined
output:
ObjC.import('Contacts')
store = $.CNContactStore
console.log(store)
/* [Class CNContactStore] */
console.log(store.defaultContainerIdentifier)
/* undefined */
In the first case you get an instance of the class using a class method +addressBook
. In the second case you have no instance and you are trying to call an instance method. You cannot call instance methods on a class.
I suppose you have to create a new instance using:
store = $.CNContactStore.alloc.init