omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    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

    Pythonista
    3
    12
    5584
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • average
      average last edited by average

      I've been trying to create a contact and assign it a default pic for the past hour.

      1. 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.

      2. 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?

      cvp 1 Reply Last reply Reply Quote 0
      • cvp
        cvp @average last edited by

        @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()
        
        
        1 Reply Last reply Reply Quote 1
        • average
          average last edited by average

          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!

          1 Reply Last reply Reply Quote 0
          • average
            average last edited by

            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]
            
            1 Reply Last reply Reply Quote 0
            • ccc
              ccc last edited by

              Do you really need ObjC to do these thing? Why not just use the contacts module?

              cvp 1 Reply Last reply Reply Quote 0
              • cvp
                cvp @ccc last edited by

                @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...

                1 Reply Last reply Reply Quote 1
                • average
                  average last edited by

                  What @cvp said. Thanks again @cvp, you saved me countless hours. You must be some sort of guru I take it :)

                  cvp 1 Reply Last reply Reply Quote 0
                  • cvp
                    cvp @average last edited by

                    @average Believe me, I am very far from being a guru. I have a lot of free time to try and try again šŸ˜€

                    1 Reply Last reply Reply Quote 1
                    • ccc
                      ccc last edited by

                      Why use ObjC for person.image_data?
                      https://forum.omz-software.com/topic/5448/contacts-images

                      cvp 2 Replies Last reply Reply Quote 2
                      • cvp
                        cvp @ccc last edited by

                        @ccc I didn't imagine that because original objectivec code was from omz him-self here

                        1 Reply Last reply Reply Quote 0
                        • cvp
                          cvp @ccc last edited by

                          @ccc the worst is that I certainly read this topic, ha this old memory šŸ˜¢šŸ‘“šŸ»

                          1 Reply Last reply Reply Quote 0
                          • ccc
                            ccc last edited by

                            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.

                            1 Reply Last reply Reply Quote 1
                            • First post
                              Last post
                            Powered by NodeBB Forums | Contributors