PyObjC ContactsUI Framework

12.1 · active · verified Tue Apr 14

PyObjC provides Python wrappers for Apple's ContactsUI framework on macOS, enabling Python applications to integrate with the system's contact selection interface. This library is part of the larger PyObjC bridge, which facilitates full-featured Cocoa application development in Python. Currently at version 12.1, PyObjC projects are actively maintained with releases often synchronized with major macOS SDK updates.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to display a `CNContactPickerViewController` to allow the user to select contacts. It initializes an `NSApplication`, sets up a `CNContactPickerDelegate` to handle selection or cancellation, and then presents the picker as a sheet. The selected contact's name and phone numbers are logged. Note that running PyObjC UI code typically requires an active `NSApplication` event loop.

import objc
from AppKit import NSApplication, NSApp, NSObject, NSWindow
from ContactsUI import CNContactPickerViewController, CNContactPickerDelegate
from Contacts import CNContactGivenNameKey, CNContactFamilyNameKey, CNContactPhoneNumbersKey
from Foundation import NSLog


class ContactPickerDelegate(NSObject, CNContactPickerDelegate):
    def contactPicker_didSelectContact_(self, picker, contact):
        NSLog("Selected contact: %@ %@", contact.givenName(), contact.familyName())
        for phone_number in contact.phoneNumbers():
            NSLog("  Phone: %@", phone_number.value().stringValue())
        picker.dismissViewControllerAnimated_completion_(True, None)
        NSApp().stop_(None)

    def contactPickerDidCancel_(self, picker):
        NSLog("Contact picker cancelled")
        picker.dismissViewControllerAnimated_completion_(True, None)
        NSApp().stop_(None)


def main():
    app = NSApplication.sharedApplication()
    delegate = ContactPickerDelegate.alloc().init()

    picker = CNContactPickerViewController.alloc().init()
    picker.setDelegate_(delegate)
    picker.setDisplayedPropertyKeys_([CNContactGivenNameKey, CNContactFamilyNameKey, CNContactPhoneNumbersKey])

    # Present the picker. In a real app, this would typically be triggered by a button click.
    # For a simple script, we'll just present it directly from a dummy window or by itself.
    # For a console app, it usually needs a backing NSApplication and a way to present it.
    # Creating a minimal window to anchor it for this quickstart example.
    from AppKit import NSWindow, NSBorderlessWindowMask, NSBackingStoreBuffered
    from Foundation import NSMakeRect

    dummy_window = NSWindow.alloc().initWithContentRect_styleMask_backing_defer_(
        NSMakeRect(0, 0, 1, 1), NSBorderlessWindowMask, NSBackingStoreBuffered, False
    )
    dummy_window.orderOut_(None)
    app.activateIgnoringOtherApps_(True)
    
    app.beginSheet_modalForWindow_modalDelegate_didEndSelector_contextInfo_(
        picker.view(),
        dummy_window,
        None,  # No modal delegate needed for simple dismiss
        None,
        None
    )
    
    # The picker is presented as a sheet, which requires the app run loop.
    app.run()

if __name__ == '__main__':
    main()

view raw JSON →