PyObjC Intents Framework

12.1 · active · verified Tue Apr 14

PyObjC-framework-intents provides Python wrappers for Apple's Intents framework on macOS, enabling developers to integrate Python applications with system services like Siri and Shortcuts. It is part of the larger PyObjC project, a bridge between Python and Objective-C. The library is actively maintained, with releases often coinciding with new macOS SDK updates to ensure compatibility and introduce new features.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to import a class from the `Intents` framework and instantiate a basic `INIntent` object using PyObjC. Full integration with Apple's Intents system (e.g., for Siri or Shortcuts) typically requires defining and configuring intents within an Xcode project and running within an application context. The example also shows a minimal Objective-C class definition pattern in Python.

from Intents import INIntent
from Foundation import NSObject
import objc

# PyObjC often requires an application context for full functionality.
# This is a minimal example demonstrating class access.

# Instantiate a basic INIntent object (typically a subclass would be used)
# Note: Actual use of Intents often involves a complex app setup and definition
# through Xcode's project capabilities for proper system integration.

try:
    # Attempt to create a generic INIntent instance
    # In a real scenario, you'd subclass INIntent or use a specific IN*Intent class
    intent = INIntent.alloc().init()
    print(f"Successfully created INIntent instance: {intent}")
    print(f"Intent identifier: {intent.identifier()}")
except objc.nosuchclass_error:
    print("Intents framework classes are not available or Intents is not properly linked.")
except Exception as e:
    print(f"An error occurred: {e}")

# Example of how to define a simple Python class that could interact with Intents
# (though direct INIntent subclassing in Python for full functionality might be limited
# without Xcode-generated intent definitions)

class MySimpleIntentHandler(NSObject):
    def init(self):
        self = super().init()
        if self is None:
            return None
        print("MySimpleIntentHandler initialized.")
        return self

    @objc.python_method
    def handleMyCustomIntent_(self, intent):
        print(f"Handling custom intent: {intent.identifier()}")
        # In a real app, this would process the intent
        return None # Or return an INIntentResponse


if __name__ == '__main__':
    # This part would usually run within an application main loop (e.g., AppKit.NSApplication.sharedApplication().run())
    # For a simple script, it demonstrates object creation.
    handler = MySimpleIntentHandler.alloc().init()
    # Simulate calling the handler with a basic intent (won't actually do anything useful without a real intent from the system)
    # For demonstration, we'll create a dummy INIntent
    dummy_intent = INIntent.alloc().init()
    dummy_intent.setValue_forKey_(f"com.example.myintent.{id(dummy_intent)}", "identifier") # Set a dummy identifier
    handler.handleMyCustomIntent_(dummy_intent)

view raw JSON →