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

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
  
  -- Just to demonstrate.  I'd probably use KM for this.
  -- delay 0.05
  -- 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

3 Likes

I highly recommend UIBrowser for GUI scripting.

Enrique T

3 Likes

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

-ccs

1 Like

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.

Hey Mitchell,

Any special reason you're switching to Safari?

In order for AppleScript to work correctly with Safari's front window the app needs to be visible and not hidden. The “Show” action in the following macro unhides Safari if it is hidden – but does NOT activate it – and saves any need to switch to it.

Using System Events to fill the link dialog makes this pretty fast.

Apple Mail → Insert Safari URL from active tab.kmmacros (6.6 KB)

Much more efficient (if rather more arcane) is to create the RTF link directly on the clipboard:

Paste RTF Link on Clipboard from Safari Active Tab Title and URL.kmmacros (7.6 KB)

-Chris

By “switching to Safari” I meant manually — it isn't part of the macro. My description was misleading. Sometimes I am in Mail (or TextEdit, or whatever) when I want to gather the link, and sometimes I am in Safari when I want to add the link to the message (or TextEdit file). I didn't bother making macros for each application I start in, but if I did, then your suggestion to Show instead of Switch would be an improvement.

Also, sometimes I have to switch to Safari from Mail (TextEdit) anyway, because I root around for the tab whose link I want — it‘s not necessarily the one currently frontmost.

In any case, I didn‘t even know that KM had Show actions, so thanks.

Hey Mitchell,

Ah, I see.

Your simplest option then would be to put the RTF link on the clipboard using a global keyboard shortcut and manually pasting it into the app where you want the link.

-Chris

1 Like

Hi @ccstone you had helped my by providing the following. I now get an error.
I'm sure it's because I updated 'mail butler' and I can't remove it because I use one of it's features. I'm sure it has something to do with that.
It's not mission critical, but it was helping me quite a bit.
I appreciate your time.


tell application "Mail"
	activate
end tell

tell application "System Events"
	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
		end tell
	end tell
end tell