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