PyObjC OpenDirectory Framework

12.1 · active · verified Tue Apr 14

PyObjC is a bridge between Python and Objective-C, enabling Python scripts to interact with Objective-C libraries, most notably macOS Cocoa frameworks. The `pyobjc-framework-opendirectory` package provides Python wrappers for the OpenDirectory and CFOpenDirectory frameworks on macOS. The project maintains an active and sustainable release cadence, typically aligning with macOS SDK updates and Python version support. The current version is 12.1.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to import the `OpenDirectory` framework and retrieve the default OpenDirectory session on macOS. It then attempts to list the available directory node names, showcasing basic interaction with the framework.

import OpenDirectory

# A simple quickstart for OpenDirectory.
# This demonstrates importing the framework and accessing a default session.
# More complex operations would involve querying specific nodes, users, or groups.

def get_opendirectory_info():
    """
    Retrieves information from the default OpenDirectory session.
    """
    print("Attempting to get default OpenDirectory session...")
    try:
        # Access the shared default session
        session = OpenDirectory.ODSession.defaultSession()
        if session:
            print(f"Successfully obtained default OpenDirectory session: {session}")

            # Example: list all node names
            # ODSession.nodeNamesAndReturnError_ is a common way to get node names.
            # The second argument is for an NSError object, passed as None from Python.
            node_names, error = session.nodeNamesAndReturnError_(None)

            if node_names:
                print(f"Available OpenDirectory nodes: {', '.join(node_names)}")
            elif error:
                print(f"Error listing nodes: {error.localizedDescription()}")
            else:
                print("No OpenDirectory nodes found.")
            return session
        else:
            print("Failed to obtain default OpenDirectory session.")
            return None
    except Exception as e:
        print(f"An error occurred: {e}")
        return None

if __name__ == "__main__":
    get_opendirectory_info()

view raw JSON →