5

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?

Thomas
  • 1,282
  • 2
    Though it’s a more generalized solution, this answer to a similar Q certainly meets your criteria. Sidenote: it’s simple to config a .authinfo profile if you’re dedicated to using Emacs. – njboot May 19 '15 at 08:17
  • The question is really to have best of both worlds: Editing in emacs, but searching via spotlight, reasonable IMAP synchronization, etc. I've used mutt + offlineimap for quite a while, but I've become too old for this. – Thomas May 19 '15 at 08:32
  • I don't know about automating emacs, so I won't directly answer you. You should be able to use "Keyboard Maestro". Here is an example of working with another GUI editor: http://rocketink.net/2013/05/quickcursor-keyboard-maestro.html If you make this work with emacs, I would enjoy seeing your answer. – Lee Joramo May 19 '15 at 17:12
  • 3
    What about 'copy/paste' – CousinCocaine May 20 '15 at 21:00
  • This guy does it in BBEdit and posted his code: http://lists.apple.com/archives/applescript-users/2013/Apr/msg00142.html – CousinCocaine May 20 '15 at 21:02
  • Copy/Paste was a joke, right? The BBEdit thing looks like a really bad idea in combination with IMAP and also otherwise. This will break on weak Internet connections, when Apple changes database format, and also it is not for initial composition of e-mails, just for mails that are already stored. – Thomas May 21 '15 at 08:19

1 Answers1

1

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.

MMM
  • 11