Responding to Emails

I am trying to amend the script David shared in the latest MacPowerUsers to automatically insert Hi and the persons name when replying to an email in Apple Mail. I want to run it from Keyboard Maestro instead of TextExpander. I can’t get it to work though. Is someone able to assist?

I’ve amended the Script to be:

tell application "System Events"
	tell process "Mail"
		tell text field "To:" of window 1
			if UI element 1 exists then
				set theToRecipient to (value of UI element 1)
				if ((count words of theToRecipient) is greater than 0) and (theToRecipient does not contain ",") then
					return word 1 of theToRecipient
					tell application "Keyboard Maestro Engine"
						try
							set value of variable "Result" to word 1
						end try
					end tell
				else if ((count words of theToRecipient) is greater than 0) and (theToRecipient contains ",") then
					return word 2 of theToRecipient
					tell application "Keyboard Maestro Engine"
						try
							set value of variable "Result" to word 2
						end try
					end tell
				end if
			end if
		end tell
	end tell
end tell

and I’ve used the following actions:

There may be more than one problem here but I think the one that is holding you up is here:

					return word 1 of theToRecipient
					tell application "Keyboard Maestro Engine"
						try
							set value of variable "Result" to word 1
						end try
					end tell

You are returning, so the tell application “Keyboard Maestro Engine” block is never executed.

Here’s how I got this working, kinda: