Python Address Label
====================

* :download:`Download example <PyObjCExample-Python Address Label.zip>`

A PyObjC Example without documentation

.. rst-class:: tabber

Sources
-------

.. rst-class:: tabbertab

plugin.py
.........

.. sourcecode:: python

    """
    This plugin adds an 'Python Address Label' action to "Address" properties in
    the AddressBook application.
    
    To install this plugin you have to build it (using 'python setup.py py2app')
    and then copy it to  '~/Library/Address Book Plug-Ins' (this folder may
    not exist yet.
    """
    
    from AddressBook import kABAddressProperty
    from AppKit import NSNull, NSObject, NSStringPboardType, NSPasteboard
    
    
    class PyAddressLabelDelegate(NSObject):
        def actionProperty(self):
            return kABAddressProperty
    
        def titleForPerson_identifier_(self, person, identifier):
            return "Python Address Label"
    
        def shouldEnableActionForPerson_identifier_(self, person, identifier):
            return len(person.address()) > 0
    
        def performActionForPerson_identifier_(self, person, identifier):
            text = person.name()
            if person.department() is not NSNull.null():
                text += "\n" + person.department()
    
            if person.organization() is not NSNull.null():
                text += "\n" + person.organization()
    
            address = person.address()[0]
            text += "\n" + address.street()
            text += "\n" + address.zip() + " " + address.city()
    
            if address.country() is not NSNull.null():
                text += "\n" + address.country()
    
            pboard = NSPasteboard.generalPasteboard()
            pboard.declareTypes_owner_([NSStringPboardType], None)
            pboard.setString_forType_(text, NSStringPboardType)

.. rst-class:: tabbertab

setup.py
........

.. sourcecode:: python

    """
    Script for building the example.
    
    Usage:
        python3 setup.py py2app
    
    To use this copy dist/PyAddressLabel.plugin to the plugin directory:
       $ mv dist/PyAddressLabel.plugin ~/Library/"Address Book Plug-Ins/PyAddressLabel.plugin"
    """
    
    from setuptools import setup
    
    infoPlist = {
        "CFBundleName": "PyAddressLabel",
        "CFBundleGetInfoString": "Silly PopUp for AddressBook",
        "CFBundleVersion": "0.1",
        "CFBundleShortVersionString": "0.1",
        "NSPrincipalClass": "PyAddressLabelDelegate",
    }
    
    setup(
        name="PyAddressLabel",
        plugin=["plugin.py"],
        data_files=[],
        setup_request=["py2app", "pyobjc-framework-AddressBook", "pyobjc-framework-Cocoa"],
        options={"py2app": {"extension": ".bundle", "plist": infoPlist}},
    )

