Problem with AppleScript in TextExpander- David's greeting script

Hello all,
I’ve been using @MacSparky’s AppleScript to get the recipient’s name and insert it into an email. However, since updating to Catalina, it has completely stopped working. Does anyone have any thoughts or solutions? I’ve copied and pasted the script below:

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
else if ((count words of theToRecipient) is greater than 0) and (theToRecipient contains “,”) then
return word 2 of theToRecipient
end if
end if
end tell
end tell
end tell

Could be a permissions issue. Check the “Accessibility” and “Automation” sections of the Privacy tab in System Preferences > Security & Privacy panel to see if there is one or more unchecked checkbox(es) next to an app name. Likely at one point a message popped up asking you to give TextExpander permission to control Mail, or something like that.

Katie

Good idea! But I just checked it and TextExpander has full permissions, including mail.

I couldn’t get it to work either. I ended up having to modify it to this:

   tell application "System Events"
	tell process "Mail"
		tell text field 1 of splitter group 1 of window 1
			set theToRecipient to (value of text field 1)
			if ((count words of theToRecipient) is greater than 0) and (theToRecipient does not contain ",") then
				return word 1 of theToRecipient
			else if ((count words of theToRecipient) is greater than 0) and (theToRecipient contains ",") then
				return word 2 of theToRecipient
			end if
		end tell
	end tell
end tell

This works!! Thank you!