Skip to main content

A Python Interface to macOS Accessibility API for UI Automation and AI Agents

Project description

Py2Mac

A Python Interface to macOS Accessibility API for UI Automation and AI Agents.

Overview

Py2Mac was developed with the primary goal of enabling seamless data extraction, automation, and the creation of autonomous AI agents that can directly interact with the computer via macOS Accessibility APIs. This package simplifies integration with the macOS Accessibility API, making it more accessible from Python. Additionally, Py2Mac includes tools for data cleanup and processing to improve data quality for large language models (LLMs) and integrates with LangChain for building AI Agents capable of interacting with UIs.

One of Py2Mac's key features is its reliance on the text-based accessibility tree, which allows it to interact with the UI without needing vision-language models (VLMs). By leveraging the same accessibility tools that allow individuals with disabilities to use a computer, Py2Mac enables UI interaction solely through text and accessibility data, making it compatible with any LLM, including open-source models. This approach illustrates the rationale behind the package: if accessibility tools can support effective computer use, an AI agent should also be able to interact with the UI independently of VLMs, providing a flexible solution for building autonomous agents.

Features

  • UI Interaction: Access and manipulate UI elements programmatically.
  • Data Processing: Clean and process data to make it more suitable for LLM integrations.
  • LangChain Integration: Use LangChain tools and examples to build AI Agents capable of interacting with the computer.

Installation

pip install py2mac

Getting Started

Prerequisites

  • macOS System with accessibility permissions enabled for your Python interpreter.
  • Python 3.11+ installed on your system.

Enabling Accessibility Permissions

  • Open System Preferences > Security & Privacy > Privacy tab.
  • Select Accessibility from the left panel.
  • Click the lock icon to make changes and enter your password.
  • Click the + button and add your Python interpreter (e.g., /usr/local/bin/python3).

IMPORTANT: Disclaimer

Full System Access: This package grants access to system applications and data, including the ability to read data from applications, interact with their user interfaces, and access minimized or backgrounded apps. Exercise caution to prevent unintentional exposure of sensitive information or unintended actions, especially when using the package with an AI agent or automation.

Data Privacy: Avoid leaking personal or sensitive information, such as passwords or private files, especially when integrating with external services or APIs.

Require User Confirmation: For safety and control, no action should be executed by AI agents or automations without explicit user confirmation.

Basic Usage

Accessing Running Applications

from py2mac.application import get_running_applications

# Get all running applications
apps = get_running_applications()
for app in apps:
    print(app.localized_name, app.pid)

Interacting with the Frontmost Application

from py2mac.application import Application

# Get the frontmost application (the one currently in focus)
app = Application.from_frontmost_app()
print(f"Interacting with: {app.localized_name}")

# Refresh the UI element tree
app.refresh_ui_tree()

# Access the root UI element
root_element = app.root_ui_element

Working with UI Elements

Exploring UI Elements

# Convert the UI tree to a dictionary
ui_tree = root_element.asdict()
print(ui_tree)

Accessing Specific UI Elements

# Get a UI element by its unique ID
element_id = 'AXButton__OK__123456789'  # Replace with the actual ID
ui_element = app.get_ui_element(element_id)

# Print element attributes
print(ui_element.AXTitle)
print(ui_element.AXRole)

Performing Actions on UI Elements

# Perform an action (e.g., press a button)
try:
    ui_element.AXPress()
    print("Action performed successfully.")
except UIActionError as e:
    print(f"Failed to perform action: {e}")

Examples

To get started with the examples, clone the repository and set up the environment:

git clone git@github.com:rafalwytrykus/py2mac.git
cd py2mac
poetry install

Library usage interactive jupyter notebook

The library comes with an interactive Jupyter notebook that demonstrates the basic usage of the package. To run the notebook, execute the following commands:

cd examples
poetry run jupyter lab

AI Agent Web UI Example

The library includes examples of LangChain tool integration and an AI Agent capable of interacting with UI elements. To launch the AI Agent example using the Chainlit web UI:

  1. Start the Chainlit web UI with the following commands:
cd examples
chainlit run py2mac_agent.py -w
  1. Open the Chainlit web UI in your browser: http://localhost:8000
  2. Click on the settings icon in the left corner of input field and select the application you want to interact with from the dropdown list.
  3. You can now interact with the AI Agent by typing in the input field. The agent can read UI state, set properties, and perform actions on UI elements.

NOTE: For safety reasons, the AI Agent will not perform any actions without explicit user confirmation. Confirm each action in the console before it executes.

Contributing

Thank you for your interest in contributing to Py2Mac! We welcome contributions of all kinds—whether it’s reporting issues, adding new features, improving documentation, or suggesting ideas.

Thank you for using Py2Mac! We hope this package helps you develop macOS UI automation and AI agents.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

py2mac-0.1.2.tar.gz (19.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

py2mac-0.1.2-py3-none-any.whl (21.5 kB view details)

Uploaded Python 3

File details

Details for the file py2mac-0.1.2.tar.gz.

File metadata

  • Download URL: py2mac-0.1.2.tar.gz
  • Upload date:
  • Size: 19.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.3 Darwin/24.0.0

File hashes

Hashes for py2mac-0.1.2.tar.gz
Algorithm Hash digest
SHA256 504d1b6a3edb34b079809ab9812517856082eb2bc5e4464aa0cc83c3caa3564a
MD5 05ae79573d813e96ad6164154c890e06
BLAKE2b-256 c8ccf9826257473350b5c95fa956da0d76059b6f39d08500af7858750e299456

See more details on using hashes here.

File details

Details for the file py2mac-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: py2mac-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 21.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.3 Darwin/24.0.0

File hashes

Hashes for py2mac-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 7487d4007e0d9ce85093242c8907d14cf8c5917c7a18e0af5bea5d50dd510e91
MD5 4d37f7750bce7bc88add27e03cd8d265
BLAKE2b-256 25f032a0b12963af6f0a9e538abbc94343966b771a96cf53fc170f26333ed3c7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page