TransWikia.com

Is it possible to compose Mac OS Mail in an external editor?

Ask Different Asked on November 23, 2021

For Thunderbird there is the External Editor extension a very very classical piece of software that, when called via a shortcut, pops up your external editor (emacs in my case) and let’s you edit your mail there. While editing the compose window of Thunderbird is blocked. When the external editor closes, the compose window is populated with the updated text of the mail. Very convenient to have your mail as one buffer in your emacs, for instance to edit tables, copy computational results, use power editing features like regexp-search-and-replace, etc.

My question: Is it possible to implement the same thing using Mac OS Mail + AppleScript or other tricks?

One Answer

I recently got my first Mac and was facing the same situation concerning my mail-writing habbits (using Thunderbird with external editor and vi on Linux). So far, I could not find an appropriate answer. Thus I started to look into Applescript.

I would like to enter mail addresses or add attachments in Apple Mail's composer window, while editing the message body in vi. So, I start with a composer window (either for a new mail or with a reply to another email) and when finished with addresses, I close the composer window and save the message as draft.

With the following Applescript script I can then extract addresses, subject, and message body into a textfile and open it with vi:

tell application "Mail"
        # Set global variables.
        set msgfilename to "/Users/me/Documents/message"
        set msgheaderdelimiter to linefeed & "#+#+#+#+#+#This line serves as delimiter for applescript.#+#+#+#+#+#" & linefeed
        set msgdraft to first item of messages of drafts mailbox

        # Open file to edit message.
        try
                set msgfile to open for access msgfilename with write permission
        on error number -49
                say "Error, file already open."
                close access msgfilename
                set msgfile to open for access msgfilename with write permission
        end try
        set eof msgfile to 0

        # Extract information from the message and write it to the file
        set msgrecipients to to recipients of msgdraft
        write "To: " to msgfile as «class utf8»
        repeat with rcp in msgrecipients
                set addr to address of rcp
                write addr to msgfile as «class utf8»
                write "," to msgfile as «class utf8»
        end repeat
        write linefeed & "CC: " to msgfile starting at -1 as «class utf8»
        set msgrecipients to cc recipients of msgdraft
        repeat with rcp in msgrecipients
                set addr to address of rcp
                write addr to msgfile as «class utf8»
                write "," to msgfile as «class utf8»
        end repeat
        write linefeed & "Subject: " to msgfile starting at -1 as «class utf8»
        set msgsubject to subject of msgdraft
        write msgsubject to msgfile starting at eof as «class utf8»
        set msgcontent to content of msgdraft
        write msgheaderdelimiter to msgfile as «class utf8»
        write msgcontent to msgfile starting at eof as «class utf8»
        close access msgfile
        delete msgdraft
end tell

# Start gvim with the prepared message file
do shell script "/usr/local/bin/gvim /Users/me/Documents/message"

After editing the message body, I save the textfile and close vi. With the following script, I can then open a new message composer window with the information from the textfile:

tell application "Mail"
        # Set global variables.
        set fillinToAddresses to false
        set fillinCCAddresses to false
        set defaultDelimiters to AppleScript's text item delimiters
        set msgfilename to "/Users/me/Documents/message"
        set msgheaderdelimiter to linefeed & "#+#+#+#+#+#This line serves as delimiter for applescript.#+#+#+#+#+#" & linefeed

        # Open file to create outgoing message.
        set msgfile to open for access msgfilename
        set fields to read msgfile from 1 for 10000 using delimiter linefeed as «class utf8»
        # Extract To addresses
        set AppleScript's text item delimiters to " "
        set bufferarray to every text item of item 1 of fields
        if (count of bufferarray) = 2 then
                set AppleScript's text item delimiters to ","
                set msgtoaddresses to every text item of item 2 of bufferarray
                set fillinToAddresses to true
        end if
        # Extract CC addresses
        set AppleScript's text item delimiters to " "
        set bufferarray to every text item of item 2 of fields
        if (count of bufferarray) = 2 then
                set AppleScript's text item delimiters to ","
                set msgccaddresses to every text item of item 2 of bufferarray
                set fillinCCAddresses to true
        end if
        # Extract subject
        set AppleScript's text item delimiters to "Subject: "
        set bufferarray to every text item of item 3 of fields
        set msgsubject to item 2 of bufferarray as «class utf8»
        # Extract mail body
        set buffer1 to read msgfile from 1 for 10000 as «class utf8»
        set AppleScript's text item delimiters to msgheaderdelimiter
        set buffer2 to every text item of buffer1
        set msgbody to item 2 of buffer2

        # Create outgoing message object
        set newmsg to make new outgoing message
        tell newmsg
                if fillinToAddresses then
                        repeat with addr in msgtoaddresses
                                make new to recipient at end of to recipients with properties {address:addr}
                        end repeat
                end if
                if fillinCCAddresses then
                        repeat with addr in msgccaddresses
                                make new cc recipient at end of cc recipients with properties {address:addr}
                        end repeat
                end if
                set the subject to msgsubject
                set the content to msgbody
        end tell

        # Close file
        close access msgfile
end tell

Now, I would add attachments and send the mail.

There are obviously still some weaknesses: The BCC and reply-to fields are still missing, but can easily be added. A bit worse, attachments are lost, when switching from the message composer to vi and back. It would be desired, that the new message composer window is opened automatically as vi is closed, without a manual script start as above. So, I am not completely happy with it yet, but maybe it's a starting point where some more advanced users can add improvements.

Answered by MMM on November 23, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP