Send Mail Message with Rich Text from Named Clipboard

Hi,

I’ve copied a block of rich text into a named clipboard, and want to build an email with the contents of the named clipboard as the body.

Using the Send Mail Message action, I have the token for the named clipboard: %NamedClipboard%DC-Email-Body%

When the email is created, the body of the email is converted to plain text.

When I Display Named Clipboard, it shows all the correct formatting in place, so it’s the Send Mail Message action that is causing the problem.

Is there a way to pass rich text to the body of the email using this action?

Other options?

Thanks in advance.

Hey There,

No. I'm sorry to say.

The action works via AppleScript, and AppleScript doesn't support this.

There may be a way though.

I'm looking into it.

-Chris

Since Chris has confirmed this can't be done via the KM action, I'd suggest this:
Use KM to select the use Mail UI menus to create a new msg, and then (via KM) paste the named clipboard into the message body.

I've done this with great success for a variety of apps.

I find the KM action Select or Show Menu Item to work quite well.

To determine the KM actions needed, just go through the steps manually in Mail, using only the keyboard.

Good luck.

You can do some magic with AppleScript and System Events.

This will create a new message and set the To-Address.

It will then move focus to the body of the message, so you can use KM to paste there.

------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2015/10/03 06:00
# dMod: 2015/12/04 04:27
# Appl: Mail & System Events
# Task: Create New Mail Message, set To-Field, move focus into body (for pasting).
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Mail, @System_Events
------------------------------------------------------------

tell application "Mail"
  activate
  set newMsg to make new outgoing message with properties {visible:true}
end tell

tell application "System Events"
  if quit delay ≠ 0 then set quit delay to 0
  
  tell application process "Mail"
    set frontmost to true
    
    tell (first window whose subrole is "AXStandardWindow")
      
      delay 0.05
      
      # Comment-out to leave To field unchanged.
      tell text field "To:"
        set value to ""
        set focused to true
        set value to "Robert A. Heinlein <tanstaafl@pieinthesky.com>"
      end tell
      
      # Moves focus to the message body.
      tell scroll area 1 to tell UI element 1 to set focused to true
      
    end tell
  end tell
end tell

------------------------------------------------------------

This is a simple example – quite a lot more can be done.

AppleScripts can be tested with the Script Editor.app – and run from Keyboard Maestro using an Execute an AppleScript action.

I’m still looking into the issue of creating the message fully with AppleScript (ASObjC in this case).

-Chris

Edited 2015/12/04 04:27 – changed script to ensure Mail is activated.

2 Likes

@Albioner: The challenge I have run into is how to move the cursor to the body of the mail message before making a paste (or select & copy).

Here's an AppleScript from Chis that does that:

Ha! You beat me to it, with and even better solution.
Saving your code to my snippets.

Thanks.

Great code!

While you're still thinking about this, with the UI code, do you have a code snippet to set focus to the To-Address that could be used after the KM paste into the body?

THis would allow the user to take advantage of the Mail address book.

Hey JM,

Here you go:

-Chris

------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2015/10/03 06:00
# dMod: 2015/12/04 04:26
# Appl: Mail & System Events
# Task: Create New Mail Message, Paste Clipboard into Body, Move focus to To-field.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Mail, @System_Events
------------------------------------------------------------

# Just to provide some known content on the clipboard.
set the clipboard to "This is a test of the Emergency Broadcast System!"

# Create blank message.
tell application "Mail"
  activate
  set newMsg to make new outgoing message with properties {visible:true}
end tell

tell application "System Events"
  if quit delay ≠ 0 then set quit delay to 0
  
  tell application process "Mail"
    set frontmost to true
    
    tell (first window whose subrole is "AXStandardWindow")
      
      delay 0.05
      
      # Moves focus to the message body.
      tell scroll area 1 to tell UI element 1 to set focused to true
      
      delay 0.05
      
      # Emulate keystrokes for paste using System Events instead of KM for speed and convenience.
      keystroke "v" using command down
      
      # Moves focus to To-field:
      tell text field "To:"
        # set value to ""
        set focused to true
      end tell
      
    end tell
  end tell
end tell

------------------------------------------------------------

Edited 2015/12/04 04:26 – chanced script to endure Mail is activated.

1 Like

Awesome! :+1:

Thank you both for all your suggestions.

In the end, I left the creation of the email to the Send Mail Message action, as I had it before, except that I left the Body blank.

Then I stripped down your AppleScript (thanks! I never would have figured that out) to just move focus to the Body as one action.

Then I added a Paste from Named Clipboard action.

This leaves most of the work being done by KM, since I prefer to minimize the extra scripting needed. I’m still hoping you’ll find a way to pass Rich Text to the Body using the Send Mail Message action, though. :smile: (I think this would be a very useful enhancement.)

Thanks again.

Hey Albioner,

You're wasting time by splitting between a KM action and an AppleScript that could achieve what you're doing in the action.

You have to use AppleScript to move focus to the email body (via script at least), and the scripted process of create-email – move–focus is smoother, faster, and more bombproof.

That said if you're happy with the way you're doing things there's nothing wrong with it.

-Chris

Chris,

I get your point.

But there is something to be said for separating different actions/functions into different entities. I think Peter tries to do this with KM actions.

So, I makes sense to me to have a simple AppleScript for setting focus to the body of Mail (or whatever), and then use KM to do different things with it: copy, paste, filter, etc.

What I would do is to save the AppleScript to a file, and then reference/call that file from KM.

Just my 2¢.

Hey JM,

That is patently obvious but not necessarily true in this particular use-case.

As in most things it depends...

-Chris

Hi and good day,

This functions alright if in KM I do the following actions:

  • Copy to Named Clipboard 'Default Clipboard

  • Set *Variable’ to Text ‘%NamedClipboard%Default Clipboard%’

  • Activate Mail & leave it at front

  • Pause until Mail is at front

then the Applescript…


Without this there is an error:

-1719 /var/folders/ls/ybbxznzn42s7lwwdk25wh86w0000gn/T/Keyboard-Maestro-Script-B4971D1A-3DAE-45A5-A36E-67B508116378:413:420: execution error: System Events got an error: Can’t get window 1 of application process “Mail” whose subrole = “AXStandardWindow”. Invalid index.

/
with best regards,
Omar K Neusser
Stockholm, Sweden

Yes, I can understand that. However, building the email is only one part of the script. The other part includes copying text into two separate variables for the Subject and the Body, which is quickly going beyond my comfort level in AppleScript. I'm good with adapting existing AppleScripts, but I've never gotten very far writing my own. Somehow I always run into gotchas that keep me from doing what I want to. [That's why I bought Keyboard Maestro, after all!] :smile:

Thanks again.

Hey Omar,

Yes. Mail must be frontmost.

The script is supposed to handle that though.

I edited it and added a line to make sure that happens.

Try the new version, and let me know if it works properly now.

-Chris

Perfectly,

just 1 command: activate

thank you :smile:

/
bye,
Omar KN

1 Like