3

I have an AppleScript that nicely collates information and creates an email message with attachments.

I cannot find a way for the script to set the message format to "Plain Text" which is required by the receiving inbox (rather than the default "Rich Text").

Is there an AppleScript way (or trick) to setting the message format to "Plain Text"?

Jonathan Cross
  • 675
  • 8
  • 17
So Over It
  • 3,668
  • 3
  • 35
  • 46

4 Answers4

3

I found this question while trying to solve exactly this problem. Eventually I came up with the following solution:

tell application "Mail"
    set newMessage to make new outgoing message 
        with properties {visible:true,
                         subject:"message title",
                         sender:"sender@from.address",
                         content:mailBody}
    tell newMessage
        make new to recipient with properties {address:"mail@recipient.com"}
    end tell
    activate
end tell

tell application "System Events"
    click menu item "Make Plain Text" of ((process "Mail")'s (menu bar 1)'s (menu bar item "Format")'s (menu 1))
end tell

Hopefully someone will find this useful, I know I would have several hours ago!

This is tested on Mail versions 7.3 and 12.4. Newer, or older, versions might need different menu titles.

Jonathan Cross
  • 675
  • 8
  • 17
Paul Wagland
  • 27,756
  • 10
  • 52
  • 74
0

Before the section in your script that creates a message, add this line.

 tell application "Mail" to set default message format to plain text

At the very end of the script add this to reset the value

 tell application "Mail" to set default message format to rich text
adayzdone
  • 11,120
  • 2
  • 20
  • 37
  • Unfortunately this gives me an AppleScript error `Mail got an error: Can’t make string into type constant.` – So Over It Aug 29 '12 at 02:51
  • Have you tried running the command by itself? It works for me on 10.6.8 with Mail 4.5. – adayzdone Aug 29 '12 at 03:00
  • 1
    I'm running Mail Version 6.0 on 10.8.1. After looking through the AppleScript dictionary after your clue, I discovered that the format appears to have changes from `set default message format to plain text` to `set default message format to plain format` This will then compile and run - but unfortunately the message is still in Rich Text. – So Over It Aug 29 '12 at 03:02
  • So you can run the plain text script, THEN create a new message, type a few words into the body, highlight them and hit command B to make them bold? – adayzdone Aug 29 '12 at 03:37
  • 1
    Yep, even if I set as plain text and THEN create the message it still sends in Rich Text. This is without any 'editing' of the mail message on my part. If I check the mail headers of the message sent by AppleScript they will contain `Content-Type: TEXT/HTML;`. Headers will only contain `Content-Type: text/plain;` if I manually send a message with Plain Text selected from within Mail.app. – So Over It Aug 29 '12 at 08:23
  • @SoOverIt I can compile and run the new format (`set default message format to plain format`), but get an error message saying "Mail got an error: Can’t set default message format of outgoing message id 66 to plain format." The suggestion from Paul Wagland above works, but seems like a bit of a hack. – Jonathan Cross Feb 12 '20 at 01:03
0

You can use keyboard shortcut shiftt also to change the message format to plain text:

tell application "System Events" to keystroke "t" using {command down, shift down}

Tested in Mail version 12.4

Jonathan Cross
  • 675
  • 8
  • 17
0

Sad to say that Apple’s implementation is so broken that there is currently no way around it. You will have to suffer through it or find a way to send your mails through a commandline.

It got worse in macOS 13. Trying to have System Events send a keyboard shortcut will result in a permission error, even though all permissions are granted in System Settings.