Skip to content

Save drafts without a window, address drafts by id, and add draft/open modes to forward_email - #154

Merged
patrickfreyer merged 2 commits into
patrickfreyer:mainfrom
planttltd:feat/background-drafts-v2
Nov 25, 2026
Merged

patrickfreyer merged 2 commits into
patrickfreyer:mainfrom
planttltd:feat/background-drafts-v2

Conversation

@planttltd

Copy link
Copy Markdown

Problem

  • compose_email(mode="draft") (plain text) makes the message with visible:false and then runs close window 1 saving yes. The message has no window, so that line acts on whichever Mail window is in front: a viewer gets closed, or another compose window gets saved and closed.
  • manage_drafts(action="create") has no save step. It leaves the hidden message for Mail to autosave and cannot say which draft it made.
  • manage_drafts open, send and delete act on the first draft whose subject contains draft_subject, and list shows no ids, so there is no way to point at one draft exactly. search_emails, read_email, move_email and manage_trash already work by Mail id.
  • forward_email can only send. There is no way to save a forward for review, so it is removed under --read-only.

Changes

Two commits.

Save drafts without a window and address drafts by id

  • compose_email(mode="draft") (plain text) and manage_drafts(action="create") call save on the hidden message. That files it in Drafts with no window. Both report Draft id: N.
  • The id is found by snapshotting the ids in Mail's drafts mailbox before the message is made and taking the one new draft with the same subject. Using the app-level Drafts mailbox covers a draft that lands in another account's Drafts because of the sender alias.
  • manage_drafts takes draft_id. With it, open, send and delete look the draft up as «class mssg» id N of draftsMailbox and read its id back, so a miss is reported as "No draft found" rather than acting on a stale reference. draft_subject works as before. list shows each draft's id.

forward_email: draft and open modes, and message_id

  • mode="send" (default) is unchanged.
  • mode="open" runs Mail's own forward and leaves its compose window up for review. Nothing is sent.
  • mode="draft" saves to Drafts without opening a window. Mail's forward command opens a compose window even without opening window, so the draft is a new hidden message: the original's plain-text content under a Begin forwarded message: header (From, Subject, Date, To), with a copy of each original attachment. Attachments are saved to a temporary folder, one subfolder each so equal names cannot clash, attached, and the folder is removed afterwards. The draft is saved and its id reported as above.
  • message_id looks the original up directly, as in read_email. subject_keyword is now optional, so to moves to second place. Callers using keyword arguments are unaffected.
  • Under --read-only, forward_email stays registered like compose_email and reply_to_email and refuses only mode="send".

All new paths go through run_applescript, so they hold mail_automation_lock.

What can and can't be done without a window

Path Window
compose_email(mode="draft"), plain text None
manage_drafts(action="create") None
forward_email(mode="draft") None, plain-text body, original attachments kept
compose_email(mode="draft", body_html=...) Compose window, unchanged. The HTML is pasted in
reply_to_email(mode="draft") Compose window, unchanged
forward_email(mode="open"), manage_drafts(action="open") Compose window, by design

Replies are left as they are on purpose. Mail's reply command opens a compose window even without opening window, and it is the only way AppleScript gets In-Reply-To and References. A windowless reply would have to be a new, unthreaded message, which is a worse reply than the current one. The same limit is why a windowless forward is rebuilt rather than made with forward: it is not threaded and carries no HTML formatting. Callers who want Mail's own forward can use mode="open".

Testing

  • PYTHONPATH=plugin python -m pytest tests/ -q: 340 passed (325 before), 194 subtests. New tests in tests/test_windowless_drafts.py check that the draft scripts contain no activate, keystroke, System Events, pasteboard, close window or with opening window, that the Drafts snapshot comes before the message is made and save after it, that id lookups never scan the mailbox, that bad ids are rejected before any AppleScript runs, that the attachment folder is removed, and that --read-only still allows draft and open.
  • python scripts/check_versions.py, python -m build and python scripts/verify_wheel.py pass.
  • Every generated script (compose draft, manage_drafts list, create, open, send and delete by id and by subject, forward draft, open and send) compiles with osacompile.
  • Checked live on macOS against an iCloud account, with drafts addressed to my own address only, each deleted afterwards with manage_drafts(action="delete", draft_id=...): compose_email(mode="draft") with an attachment, manage_drafts(action="create"), and forward_email(mode="draft", message_id=...) of a message with two PDF attachments. Each draft appeared in Drafts with its id, recipients and attachments; Mail's window count did not change. A second delete of the same id returned "No draft found matching: id N".
  • Not run live: the open paths and anything that sends.

Plantt Ltd added 2 commits November 20, 2026 14:44
compose_email(mode="draft") made a hidden message (visible:false) and
then ran "close window 1 saving yes". The message has no window, so that
acted on whichever Mail window was in front. It now calls "save" on the
hidden message, which files it in Drafts without opening anything, and
reports the new draft's id. manage_drafts(action="create") had no save
step at all; it now saves the same way and reports the id.

The id is found by snapshotting the ids in Mail's Drafts mailbox before
the message is made and taking the one new draft with the same subject.

manage_drafts takes draft_id, so open, send and delete can act on one
draft by its Mail id ("«class mssg» id N", re-read to catch a miss)
instead of the first subject match. action="list" shows each id.

With body_html, compose_email still pastes the body into a compose
window, so that draft path is unchanged.
forward_email could only send. It now takes mode ("send", the default,
"draft" or "open") and message_id, which looks the message up directly
instead of taking the first subject match.

mode="open" runs Mail's own forward and leaves its compose window up for
review without sending.

mode="draft" saves to Drafts without opening a window. Mail's forward
command opens a compose window even "without opening window", so the
draft is a new hidden message instead: the original's plain-text content
under a "Begin forwarded message:" header, with a copy of each original
attachment (saved to a temporary folder, attached, then removed). It is
saved and its id found as compose_email's drafts are. It has no HTML
formatting.

With a non-sending mode, forward_email no longer needs to be removed
under --read-only; like compose_email and reply_to_email it stays
registered and refuses only mode="send". Its "to" parameter now comes
second, since subject_keyword is optional when message_id is given.
patrickfreyer pushed a commit that referenced this pull request Nov 25, 2026
…the snapshot and lookups; skip attachments that can't be copied

From the second review of #154:
- The Drafts snapshot runs in its own try, so a Drafts mailbox Mail can't
  read no longer stops a draft from being saved (main didn't need it).
- Lookups after 'save' can't raise: an error there made the tool report
  'Error' for a saved draft, and a retry created a duplicate.
- More than one new matching draft (Gmail sync copies) is reported as
  ambiguous instead of picking the first id.
- compose/create/forward print the success line and draft id only when the
  draft was found; otherwise they say the save is not verified.
- forward draft: an attachment that can't be saved (e.g. not downloaded on
  IMAP) is skipped and reported instead of losing the whole draft.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CoqrHaj3Ru8RUcwAATtUT1

Copy link
Copy Markdown
Owner

Posted by Claude (AI maintainer agent) on Patrick's behalf.

Thank you @planttltd! This landed via #158, which keeps your two commits as-is. Changes on top: attachment names from incoming mail are kept inside the temp folder; a draft is reported as saved (with its id) only once it's found in Drafts, otherwise as unverified; Gmail sync copies count once; and an attachment that can't be copied is skipped and reported instead of losing the whole forward draft. Closing in favor of #158.


Generated by Claude Code

patrickfreyer added a commit that referenced this pull request Nov 25, 2026
Save drafts without a window, address drafts by id, forward draft/open modes (carries #154)
@patrickfreyer
patrickfreyer merged commit 3d13442 into patrickfreyer:main Nov 25, 2026
@patrickfreyer patrickfreyer mentioned this pull request Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants