How Do I Move Cursor to the Body of a Apple Mail Message?

####How Do I Move Cursor to the Body of a Apple Mail (AM) Message?
I’m building a macro where I need to copy the entire body of a Mac Mail message. I need to copy it because I need the rich text used in the body to paste into another app (Evernote). I don’t think AS or JS can do this, as I think they can only get the plain text of the body. If I’m wrong, please advise.

With Outlook, the FIND command would take me to the body.
But not so in AM.

I’ve tested and searched, and can find no KB shortcut to move cursor to body.
I can use multiple TABS, but it is unreliable because the number depends on what header fields (bcc, attachments, etc) have been enabled.

Any ideas?

EDIT: Previously I call this “Mac Mail”. The proper name if “Apple Mail”, so I have changed all references to it to use the proper name.

OK, I overlooked this before. Looks like AS/JS can return rich text:

content (rich text, r/o) : Contents of an email message

I’ve just got to get this on the Clipboard. I suppose I can use the same technique we used with the rich text hyperlink macro.

Hey JM,

It’s a lie. No RTF data is copied to the clipboard when setting it to content.

How are you viewing mail? In the Preview Pane?

Do you use the new vertical message list on the side or the old style one above the preview pane?

You can do what you want with a click, but System Events will let you move focus directly.

-Chris

Can you please give me a clue as to how to do this?
How to use SE to set focus on the Mail body?
This would be after executing a Forward command, which is no problem.

I just looked at the SE Dictionary again, and I didn’t see anything.
But doesn’t this have to be Mail specific?
There is no Mail menu or shortcut (that i have been able to find) to move to body.

Thanks.

Hey JM,

Yes…

------------------------------------------------------------
# Script moves focus to editing panel of front message.
------------------------------------------------------------
tell application "System Events"
  if quit delay ≠ 0 then set quit delay to 0
  
  tell application process "Mail"
    set frontmost to true -- Just to be sure
    
    tell (first window whose subrole is "AXStandardWindow")
      if text field 1 exists then
        if text field 1's name is not "To:" then
          error "The front window is probably not an outgoing message."
        end if
      else
        error "The front window is probably not an outgoing message."
      end if
      
      tell scroll area 1
        set its focused to true
      end tell
      
    end tell
    
  end tell
  
  delay 0.05
  
  -- Just to demonstrate.  I'd probably use KM for this.
  keystroke "a" using {command down}
  
end tell
------------------------------------------------------------

I’ve used the [ To: ] field as a check to be sure the front window is an outgoing message window. (Unfortunately Mail.app itself — unlike the blessed Eudora — provides no means to do this.)

The script will work with a new outgoing message, a reply, or a forward.

It’s a good idea to use an error-handler in every script, so if it errors you at least know and have some kind of error message (although AppleScript’s error messages can be cryptic). AppleScript will fail silently in Keyboard Maestro unless you set the action to output the result — and you generally don’t want that.

You’re missing the fact that System Events is the agent for GUI-Scripting.

We’re actually talking to the UI-Elements of the process — in this case Mail.app.

Some apps have non-standard UI-Elements that are not amenable to GUI-Scripting with System Events (like Firefox), but the vast majority of them can be seen to one degree or another by SEV.

-Chris

2 Likes

I highly recommend UIBrowser for GUI scripting.

Enrique T

2 Likes

I concur. I used it to write the script above.   :smile:

-ccs

Thanks, Chris! That works perfectly!

But you cheated! LOL
UI Browswer does all the work for you, including giving you the AppleScript code. :smile:
You should have let me in on that “little” secret. :wink:

Thanks Enrique! This is huge! :+1:
This makes so many things possible, and much easier. :smile:

Hey JM,

Of course!   :smile:

I use the tools at my disposal, and I’ve mentioned more than once on the forum that I use UI Browser (and provided the link).

It’s very helpful, but it doesn’t do all the work.

You should also install Xcode, so you have the Accessibility Inspector.app. It’s not nearly as nice as UI Browser, but sometimes the information it provides is useful in figuring things out.

-Chris

I want to be able to forward or email but at the end of the body i want to insert a text string
Using Apple Mail

I found on this forum the following question that looks similar to mine:

But the answer is a Applescript which select the whole body text.
I would like to have an Applescript which send me to the end of the body text so i can insert more text there.

Yes, it is quite similar. So, the best approach is to reply to that topic.
Thus, I have moved your post/topic to the existing topic.

I think I can answer your question, but I need to investigate further.

Using the script posted above:

All you need to do is replace the last line:

keystroke "a" using {command down}

with this, which is the

key code 125 using {command down}

I haven’t tested this since it is a simple change, so please let me know if it works for you, or not.

Hey Chris,

All you have to do is remove 1 line from the script to stop it from selecting-all…

keystroke "a" using {command down}

Modify the script.

Then add a Type a Keystroke action to the macro with to get you to the end of the message body.

Or you change the AppleScript as JM suggests.

In general I prefer to have Keyboard Maestro type keystrokes, but it depends on the situation. Keyboard Maestro is less prone to side-effects from the user pressing keys on the keyboard.

-Chris

Not working for me:

tell application "System Events" to ¬
    tell application process "Mail" to ¬
        get the windows whose subrole is "AXStandardWindow"

Result is nothing.

Hey Mitchell,

Are you running Sierra?

I’m not yet, so I can’t test.

In any case – figure out the problem…

tell application "System Events"
   tell application process "Mail"
      tell front window
         subrole
      end tell
   end tell
end tell

Run this with a new message window open.

-Chris

Never will.

Then what OS are you running?

And how do you have Mail configured?

-Chris

[OS X 11.6, not sure what part of Mail configuration you are looking for]

I simplified this to the following; why is focused still false?

tell application "System Events"
	tell application process "Mail"
		set frontmost to true
		set focused of scroll area 1 of window 1 to true
		get focused of scroll area 1 of window 1
	end tell
end tell

The reason I need this is a little different the others above: In a macro that uses ⌘K, paste, Return to paste a link on the selected text. After the Return the message no longer has focus. (Mail bug, presumably.) I really want focus to go to the end of the selection, but for now end of the message would be acceptable.

Oddly, without the help of any AppleScript, if I switch applications then go back to Mail, the message body does regain focus, but it’s at the beginning of the message not where the cursor had been. (Good enough – I can ⌘↓ to get to the end of the message, but really ugly.)

Hey Mitchell,

Classic layout or the newfangled one. (I use classic.)

set focused works but it would appear that get focused fails to return the proper value.

I'm puzzled that your macro is messing with Mail's focus.

Mine doesn't:

Link → Make from Selected URL.kmmacros (4.7 KB)

-Chris

My macro is similar, though a little more ambitious. The idea is to be in an application that uses ⌘K to create links, like Mail or TextEdit, switch to Safari, and type the hot key, at which point the macro switches to the previous application, pastes in the Safari page title, selects characters back to the beginning of the pasted text, add the link, and → to the end of the selection. The problem is that after adding the link, the focus is gone — typing a character just produces a beep. Switching away from the application. Yet, if I switch to another application then back, the focus comes back, but at the beginning of, say, the mail message, not where it should be. So I thought maybe doing something about the message body‘s focus would fix the problem.

I went back to the macro to re-enable some steps I had disabled in the process of debugging before posting it for you to see, but of course now it works. It will be interesting to see if and why the problem returns.