Skip to main content

macpymessenger is a Python 3.14+ library for sending macOS iMessages through AppleScript.

Project description

macpymessenger

macpymessenger sends iMessages from Python on macOS.

It talks to the built-in Messages app through AppleScript, and adds Python 3.14 t-string templates and typed errors.

Features

  • Send iMessages to phone numbers or email addresses from Python.
  • Use Python 3.14 t-strings for message templates.
  • Send the same message to many recipients.
  • Delay a send with delay_seconds.
  • Handle delivery, delay, template, and configuration errors explicitly.
  • Opt in to file logging when you want a local log file.

Requirements

macpymessenger requires macOS and Python 3.14 or newer.

It uses AppleScript, so it is not a cross-platform messaging gateway. It sends through the Messages app on the Mac that runs your script.

Installation

Add macpymessenger to a project with uv:

uv add macpymessenger

Or with pip:

pip install macpymessenger

Quick start

from macpymessenger import Configuration, IMessageClient
from macpymessenger.exceptions import MessageSendError

client = IMessageClient(Configuration())

try:
    client.send("+15555555555", "Hello from macpymessenger!")
except MessageSendError as error:
    print(f"Delivery failed: {error}")

send() returns None on success and raises MessageSendError when delivery fails.

You can also wait before sending:

client.send("+15555555555", "I will arrive soon.", delay_seconds=60)

delay_seconds must be a non-negative int.

Templates

Templates are Python callables that return Python 3.14 t-strings. Jinja2 is not used, and there is no templates/ directory.

client.create_template(
    "welcome",
    lambda name: t"Hello, {name}! Welcome aboard.",
)

client.send_template("+15555555555", "welcome", {"name": "Ada"})

Every interpolation must resolve to a str, or rendering raises TemplateTypeError.

Bulk sending

numbers = ["+15555555555", "+15555555556", "+15555555557"]
successful, failed = client.send_bulk(numbers, "Reminder: meeting at 10 AM.")

print(f"Sent: {successful}")
print(f"Failed: {failed}")

send_bulk() returns two lists: successful recipients and failed recipients.

Configuration and logging

macpymessenger uses the bundled AppleScript by default. To use your own:

from pathlib import Path
from macpymessenger import Configuration, IMessageClient

configuration = Configuration(send_script_path=Path("/path/to/custom/sendMessage.scpt"))
client = IMessageClient(configuration)

Configuration checks at creation that the script exists and is readable, and raises ScriptNotFoundError if not.

File logging is opt-in:

from macpymessenger import Configuration, FileLoggingConfiguration, IMessageClient

client = IMessageClient(Configuration(), file_logging=FileLoggingConfiguration())

This writes macpymessenger.log in the current working directory. You can also pass your own logging.Logger to IMessageClient.

Public API

These classes are importable from the package root:

from macpymessenger import (
    Configuration,
    FileLoggingConfiguration,
    IMessageClient,
    RenderedTemplate,
    SubprocessCommandRunner,
    TemplateManager,
)

Custom exceptions live in macpymessenger.exceptions.

Development

Install development dependencies:

uv sync

Run the checks:

uv run ruff check
uv run ty check
uv run pytest
uv build
uv run sphinx-build docs docs/_build/html

License

macpymessenger is available under the Apache-2.0 license.

Credits

Created and maintained by Ethan Wickstrom.

macpymessenger started as a fork of Rolstenhouse/py-iMessage.

For more detail, see the docs directory.

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

macpymessenger-0.3.0.tar.gz (13.4 kB view details)

Uploaded Source

Built Distribution

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

macpymessenger-0.3.0-py3-none-any.whl (16.0 kB view details)

Uploaded Python 3

File details

Details for the file macpymessenger-0.3.0.tar.gz.

File metadata

  • Download URL: macpymessenger-0.3.0.tar.gz
  • Upload date:
  • Size: 13.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for macpymessenger-0.3.0.tar.gz
Algorithm Hash digest
SHA256 d57b46a59ef1ca581f1f4bb18c82289d7be01396ae84c3a4538f96ee4841dfe4
MD5 eee5df52a06c30903ef6ce6f9a409b19
BLAKE2b-256 dab6f0f1f4819bf82622ad95d1beba9b2b81ca271070ccfdc751a9bdfd626423

See more details on using hashes here.

File details

Details for the file macpymessenger-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: macpymessenger-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 16.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for macpymessenger-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 de7cd791b7d1b038489343ccc9bcdd6663c5638c70ab32d93c21bc3adc267656
MD5 55dbd6e90dae8ab381754a726edc5a88
BLAKE2b-256 06fd256cd967e41ffd923a0f799e78638ff665722b3f588632d04b121ea63c38

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