Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello ā I'm @olemoritz on Twitter.
Create a new contact with a contact pic
-
I've been trying to create a contact and assign it a default pic for the past hour.
-
I use the normal contacts module to create and save a person, newPerson with basic fields and a middle name thats like "tmpId" that i use as an identifier for the next step.
-
I use basically this code to get that person by a unique middle name identifier and set their contact image to the last taken image on the device. i reset the middle name to blank. https://github.com/tdamdouni/Pythonista/blob/master/module/contacts-module-access-profile-picture.py
what i'm seeing is exactly the behavior i want: normal person, empty middle name, and a contact pic. however, after about 10 seconds, the contact gets modified to have the middle name set to my identifier and the contact image gone. almost like the mutations get erased. precisely what I don't want.
data points:
- iphone 6s latest os
- all contacts are synced to cloud on google
- manually editing the contact and setting the image works fine
- latest pythonista version
any ideas on how to just create a contact using the CNContact like in the link i shared (rather than just for the mutation step) rather than this two step process?
-
-
@average this works
# https://forum.omz-software.com/topic/2734/contacts-module-access-profile-picture/4 from objc_util import * import photos # Load classes we need from Contacts.framework: NSBundle.bundleWithPath_('/System/Library/Frameworks/Contacts.framework').load() def main(): CNContactStore = ObjCClass('CNContactStore').alloc().init().autorelease() Containers = CNContactStore.containersMatchingPredicate_error_(None,None) for Container in Containers: containerId = Container.identifier() break # get first container all_assets =photos.get_assets() asset = photos.pick_asset(assets=all_assets) img_data_bytes = asset.get_image_data().getvalue() CNMutableContact = ObjCClass('CNMutableContact').alloc().init() CNMutableContact.firstName = 'aaTest' # just to be the first CNMutableContact.imageData = img_data_bytes CNSaveRequest = ObjCClass('CNSaveRequest').new().autorelease() CNSaveRequest.addContact_toContainerWithIdentifier_(CNMutableContact, containerId) CNContactStore.executeSaveRequest_error_(CNSaveRequest, None) if __name__ == '__main__': main()
-
thanks! that seems to work great for creating a new contact and retaining the image.
how do I add a phone number? heres what Iām trying unsuccessfully:
lv=ObjCClass('CNLabeledValue').alloc().init() lv.label='Main' lv.value='1113335555' mc.phoneNumbers=[lv]
thank you!
-
Update I figured it out, thanks!!
heres what worked:
pn=ObjCClass('CNPhoneNumber').alloc().init(stringValue='1231231234') lv=ObjCClass('CNLabeledValue').alloc().init(label=contacts.MAIN_PHONE, value=pn).autorelease() mc.phoneNumbers=[lv]
-
Do you really need ObjC to do these thing? Why not just use the contacts module?
-
@ccc I use objectivec for the image and if he uses contacts in the same script, he said in his first post that he had problems of one destroying the other one...
-
-
@average Believe me, I am very far from being a guru. I have a lot of free time to try and try again š
-
Why use ObjC for person.image_data?
https://forum.omz-software.com/topic/5448/contacts-images -
-
@ccc the worst is that I certainly read this topic, ha this old memory š¢š“š»
-
My recollection is that person.image_data was not present in the original version of the contacts module but was added later. That is why it is not in the webpage docs but is in the in-the-app docs. I also think the workaround code was published before person.image_data was added to the module.
With the current module, I believe that you can do it all without ObjC.