Forum Archive

Add an address to a contact

daveaiello

I'm brand new to Pythonista, and a fairly new Workflow user.

I'm trying to create a contact by passing certain data elements on the clipboard from a running workflow. Someone on the Workflow subreddit said that running a Pythonista script was the best way to do it, and offered the following code in a gist:

import contacts
import clipboard
import webbrowser

name = clipboard.get().split(',')
pers = contacts.Person()
pers.first_name = name[0]
pers.last_name = name[1]
contacts.add_person(pers)
contacts.save()
webbrowser.open('workflow://')

My use is focused on creating contacts that exclusively contain information about businesses. So what I want to do is create a Person() that contains:

  • organization
  • work address
    ** street
    ** city
    ** state
    ** zip
  • phone
    ** main_phone
  • URL

My modified code begins as follows:

import contacts
import clipboard
import webbrowser

name = clipboard.get().split('|')
pers = contacts.Person()
pers.organization = bytes(name[0], encoding='UTF-8')

But then I get hung up on how to create an address, because I don't understand how to create Person.address from just reading the docs and reading through this forum.

Thanks for any guidance you can provide.

omz

I hope this example will help:

import contacts

person = contacts.Person()
person.address = [(contacts.WORK,
    {contacts.STREET: 'Infinite Loop 1',
     contacts.CITY: 'Cupertino'})
]
person.url = [(contacts.WORK, 'http://apple.com')]
contacts.add_person(person)
contacts.save()

Person.url and Person.address are slightly more complicated than Person.organization because you can have multiple addresses and URLs for each person. Each value in the list of addresses is a tuple with a label (contacts.WORK) and the actual address (a dictionary with the keys contacts.STREET, contacts.CITY etc.).

daveaiello

Thank you so much Ole!

I'm on the road right now, but I will try to incorporate your example into my script when I return.

daveaiello

In case anybody has a similar issue to mine, here's a link to a gist showing the completed script: add-contact.py.