MacMail Subject

Even though I've had KM for a bit now, I'm still relatively new. Today I've tried to work on a series of actions using macmail, one of which will use the subject of a reply-mail, but I'm finding that when I try to use %MailSubject% it's drawing on the information of the mail selected in the main window behind it, not the active reply window. So if for some reason the main window is minimized, or there's more than one window, the series of actions don't work.

Is there a solution to this? I've looked but can't see one (well, one within my scope of comprehension anyway).

Thanks for any help



The Apple Mail Reply window is NOT available to normal scripting, so KM doesn't have any access to it.

However, this UI script should return the Subject of the open Apple Mail Reply Message that currently has focus. Just put this script in a KM Execute an AppleScript action, and set the results to a KM Variable of your choosing.

property ptyScriptName : "Get Subject of Apple Mail Reply Message"
property ptyScriptVer : "2.0"
property ptyScriptDate : "2018-06-30"
property ptyScriptAuthor : "JMichaelTX"

tell application "System Events"
  tell application process "Mail"
    tell (first window whose subrole is "AXStandardWindow")
      tell text field "Subject:"
        set subjectStr to its value
      end tell
    end tell
  end tell
end tell

return subjectStr


Here is a list of all UI elements of the Mail Reply Window.
Using this data, you should be able to construct a UI script addressing any of these elements.

application Process "Mail"
	window Re: test from outlook
  static text "To:"
  text field "To:"
   text field 1
  button 1
  static text "Cc:"
  text field "Cc:"
  button 2
  static text "Subject:"
  text field "Subject:"
  scroll area 1
   UI element 1
    group 1
    group 2
     static text "On Nov 9, 2012, at 3:16 PM, FirstName LastName <> wrote:"
    group 3
    group 4
     static text "This is a test."
    group 5
    group 6
    group 7
     static text "Best Regards,"
    group 8
     static text " "
    group 9
     static text "FirstName LastName"
    group 10
    group 11
  button 3
  button 4
  button 5
  group 1
   radio button "Re: test from outlook"
    button "close tab"
  toolbar 1
   button 1
   menu button 1
    group 1
     menu button 1
   button 2
   button 3
   button 4
   button 5
   button 6
   button 7

1 Like

You should be able to get the subject of a message being composed in Apple Mail by retrieving the name property of the window in AppleScript:

tell application "Mail" to get the name of the front window

Of course, that appears to be the only field one can get in that manner, and @JMichaelTX's demonstrated nicely how other fields can be retrieved through accessibility hooks.

Thanks so much. Sorry to ask though, how do I now send it to a variable?

The instructions are in my post.

Thanks @JMichaelTX

You've been really helpful. I didn't catch the extra submenu - I was thinking that I had to do something with your result as a step. This link spelt it out for me Variable: Applescript to KM where he points out the submenu. All good now - I've got it going to a clipboard.

Again, really appreciate your help - it's a great community here.