Set quote level when making new email in Mail with Html

My goal is to make an email in Mail with Html. I filched a great script from the LateNigh SW site for this:

use framework "Foundation"
use framework "AppKit"
use scripting additions

-- classes, constants, and enums used
property NSUTF8StringEncoding : a reference to 4
property NSSharingServiceNameComposeEmail : a reference to current application's NSSharingServiceNameComposeEmail
property NSAttributedString : a reference to current application's NSAttributedString
property NSString : a reference to current application's NSString
property NSSharingService : a reference to current application's NSSharingService

-- MAIL MESSAGE VALUES
set headlineText to "<p style=\"font-family:-apple-system,san-serif;font-size:64px;text-align:left;margin-bottom:0;\"></p>"

set bodyText to "<p>test before quote</p>

<blockquote>
<p>quote 1</p>

<blockquote>
<p>quote 2</p>
</blockquote>

<p>quote 1</p>
</blockquote>

<p>no quote</p>"

set messageSubject to "HOWDY"
set recipientAddresses to {"johnny-appleseed@apple.com"}

-- THE HTML FOR THE MESSAGE
set thisHTML to "<!DOCTYPE html>
<html lang=\"en\"> 
<head>
	<meta charset=\"utf-8\" />
	<title>MAIL TEMPLATE</title>
</head>
<body style=\"margin:40;width:100%;Height:100vh;\">" & ¬
	headlineText & return & ¬
	bodyText & return & ¬
	"</body>
</html>"

set theSource to NSString's stringWithString:thisHTML
set theData to theSource's dataUsingEncoding:NSUTF8StringEncoding
set anAttributedString to NSAttributedString's alloc()'s initWithHTML:theData documentAttributes:{}

-- USE THE MAIL SHARING SERVICE TO CREATE A NEW MAIL MESSAGE
set aSharingService to NSSharingService's sharingServiceNamed:(NSSharingServiceNameComposeEmail)
if aSharingService's canPerformWithItems:{"someone@somewhere.com"} then
	set aSharingService's subject to messageSubject
	set aSharingService's recipients to recipientAddresses
	tell aSharingService to performSelectorOnMainThread:"performWithItems:" withObject:{anAttributedString} waitUntilDone:false
end if

This works almost:

How do I set the blockquotes and the font? I’ve also tried to use textutil to convert the html to rtf but this eats the blockqutes.

By using the sharing service to create the message, the user may not have mail as their default email application, and any commands for formatting you use to work with mail may fail.

In my case, your script opens an email in Outlook.

In Mail, I don’t know how to script importing formatted rich text.

If you don’t mind indenting with tabs, this would work:

set bodyText to " test before quote " & linefeed & "No indent " & linefeed & tab & "Indent 1 " & linefeed & tab & "indent 2 " & linefeed & tab & tab & "double indent " & linefeed & tab & "indent"
tell application "Mail"
	set myemail to make new outgoing message ¬
		at beginning ¬
		with properties {content:bodyText}
	get content of myemail
end tell

You may also try saving your draft and working on the file itself.

It’s good that you provided a screenshot of the non-working message.

But it would be better if you provide a screenshot of the message in the form in which you wish to receive it. Otherwise, here everyone must guess long and hard what you are trying to achieve.

The script is for my application Mail Archiver X. The user can reply to an archived email. Depending on which email client is the default one a script for Mail or Outlook is done. If another email client is the default one then the app does a simple mailto: is done.

When using NSSharingService with quoted text the result looks really suboptimal.

Email in Mail Archiver:

Email replied in Mail:

If I understand you:


use framework "Foundation"
use framework "AppKit"
use scripting additions

-- classes, constants, and enums used
property NSUTF8StringEncoding : a reference to 4
property NSSharingServiceNameComposeEmail : a reference to current application's NSSharingServiceNameComposeEmail
property NSAttributedString : a reference to current application's NSAttributedString
property NSString : a reference to current application's NSString
property NSSharingService : a reference to current application's NSSharingService

-- I assume you have retrieved following texts some way
set headlineText to "On 09.0421,12:25:39 wrote Beatrix Willius"
set whenWroteText to "On 09.Apr 2021, at 10:34,Thomas Tempelmann <tt@tempel,org> wrote"
set bodyText to "Decrypt entschlüsselt jeden code - key. Wenn du einer hat das fen effekt, dass du damit prüfst, ab am ende die entschlüsselst ja nix, du prüfst ja die sig des originals\""

-- MAIL MESSAGE VALUES
set headlineText to "<p style=\"font-family:-apple-system,san-serif;font-size:12px;text-align:left;margin-bottom:0;;color:black\">" & headlineText & "</p>"
set headlineText to headlineText & "<br><br>"

set bodyText to ¬
	"<div class=\"quotebox\">
         <cite>" & whenWroteText & ":</cite>
         <blockquote>
            <div>
              <p style=\"font-family:-apple-system,san-serif;font-size:18px;text-align:left;margin-bottom:0;;color:gray\">" & bodyText & "</p><br>
            </div>
          </blockquote>
      </div>"

set messageSubject to "HOWDY"
set recipientAddresses to {"johnny-appleseed@apple.com"}

-- THE HTML FOR THE MESSAGE
set thisHTML to "<!DOCTYPE html>
<html lang=\"en\"> 
<head>
   <meta charset=\"utf-8\" />
   <title>MAIL TEMPLATE</title>
</head>
<body style=\"margin:40;width:100%;Height:100vh;\">" & ¬
	headlineText & return & ¬
	bodyText & return & ¬
	"</body>
</html>"

set theSource to NSString's stringWithString:thisHTML
set theData to theSource's dataUsingEncoding:NSUTF8StringEncoding
set anAttributedString to NSAttributedString's alloc()'s initWithHTML:theData documentAttributes:{}

-- USE THE MAIL SHARING SERVICE TO CREATE A NEW MAIL MESSAGE
set aSharingService to NSSharingService's sharingServiceNamed:(NSSharingServiceNameComposeEmail)
if aSharingService's canPerformWithItems:{"someone@somewhere.com"} then
	set aSharingService's subject to messageSubject
	set aSharingService's recipients to recipientAddresses
	tell aSharingService to performSelectorOnMainThread:"performWithItems:" withObject:{anAttributedString} waitUntilDone:false
end if

Thanks for the script. But there still is no quoting.

I was looking for information about the blockquote tag on the net. As I understand it, this tag is not designed to work in email clients, but is only intended to work in browsers. And even to work in browsers, it needs to specify the site URL where the blockquote settings are stored.

And to work in email clients, you need to embed the settings into HTML, creating various blockquote styles yourself. Here is an example of how this can be done (I created 2 blockquote styles for you: blockquote1 and blockquote2):


use framework "Foundation"
use framework "AppKit"
use scripting additions

-- classes, constants, and enums used
property NSUTF8StringEncoding : a reference to 4
property NSSharingServiceNameComposeEmail : a reference to current application's NSSharingServiceNameComposeEmail
property NSAttributedString : a reference to current application's NSAttributedString
property NSString : a reference to current application's NSString
property NSSharingService : a reference to current application's NSSharingService

-- MAIL MESSAGE VALUES
set headlineText to "<p style=\"font-family:-apple-system,san-serif;font-size:64px;text-align:left;margin-bottom:0;\"></p>"

set bodyText to "<p style=\"color:brown\">test before quote</p>

<style>
p.blockquote1 {
font: 18px/24px normal helvetica, sans-serif;
color:green;
margin-top: 10px;
margin-bottom: 10px;
margin-left: 50px;
padding-left: 15px;
border-left: 30px solid #ccc;
}
</style>

<style>
p.blockquote2 {
font: 18px/24px normal helvetica, sans-serif;
color:red;
margin-top: 10px;
margin-bottom: 10px;
margin-left: 150px;
padding-left: 15px;
border-left: 3px solid #ccc;
}
</style>

<p class=\"blockquote1\">Quote 1</p>
<p class=\"blockquote2\">Quote 2</p>
<p class=\"blockquote1\">Quote 1</p>
<p style=\"color:brown\">no quote</p>"

set messageSubject to "HOWDY"
set recipientAddresses to {"kniazidis.rompert@gmail.com"}

-- THE HTML FOR THE MESSAGE
set thisHTML to "<!DOCTYPE html>
<html lang=\"en\"> 
<head>
<meta charset=\"utf-8\" />
<title>MAIL TEMPLATE</title>
</head>
<body style=\"margin:40;width:100%;Height:100vh;\">" & ¬
	headlineText & return & ¬
	bodyText & return & ¬
	"</body>
</html>"

set theSource to NSString's stringWithString:thisHTML
set theData to theSource's dataUsingEncoding:NSUTF8StringEncoding
set anAttributedString to NSAttributedString's alloc()'s initWithHTML:theData documentAttributes:{}

-- USE THE MAIL SHARING SERVICE TO CREATE A NEW MAIL MESSAGE
set aSharingService to NSSharingService's sharingServiceNamed:(NSSharingServiceNameComposeEmail)
if aSharingService's canPerformWithItems:{"kniazidis.rompert@gmail.com"} then
	set aSharingService's subject to messageSubject
	set aSharingService's recipients to recipientAddresses
	tell aSharingService to performSelectorOnMainThread:"performWithItems:" withObject:{anAttributedString} waitUntilDone:false
end if