PyObjC ApplicationServices Framework

12.1 · active · verified Sun Apr 12

PyObjC provides Python bindings for Objective-C frameworks on macOS, enabling Python developers to interact with Apple's system APIs. `pyobjc-framework-applicationservices` offers wrappers for the ApplicationServices umbrella framework, which includes various core services like CoreGraphics, CoreText, and HIServices. The library is currently at version 12.1 and maintains an active release cadence, typically aligning with macOS and Python version updates.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to access a sub-framework (CoreGraphics) within ApplicationServices to retrieve the primary display's bounds. It highlights the direct import pattern and attribute access for framework elements.

import ApplicationServices
import os

def get_display_bounds():
    """
    Retrieves the bounds of the primary display using CoreGraphics
    from ApplicationServices.
    """
    # Access CoreGraphics functions via ApplicationServices.CoreGraphics
    main_display_id = ApplicationServices.CoreGraphics.CGMainDisplayID()
    bounds = ApplicationServices.CoreGraphics.CGDisplayBounds(main_display_id)

    # PyObjC automatically bridges CGRect to a dictionary-like object
    x = bounds.origin.x
    y = bounds.origin.y
    width = bounds.size.width
    height = bounds.size.height

    print(f"Primary display bounds: x={x}, y={y}, width={width}, height={height}")
    return {"x": x, "y": y, "width": width, "height": height}

if __name__ == '__main__':
    # This example does not require an active NSApplication runloop
    # for just getting display bounds.
    display_info = get_display_bounds()
    assert display_info['width'] > 0 and display_info['height'] > 0
    print("Successfully retrieved display information.")

view raw JSON →