TextExpander Apple Script to Insert Name

I’ve been using @MacSparky’s Apple Script to insert the name of the recipient into an email but since upgrading to Ventura this doesn’t seem to work.

Does anyone have a fix?

tell application "System Events"
tell process "Mail"
   -- tell text field "To:" of window 1
   -- tell text area 1 of group 2 of group 1 of scroll area 1 of splitter group 1 of window 1
      tell text field 1 of splitter group 1 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 then return word 1 of theToRecipient
        end if
    end tell
end tell
end tell

Hi,

I use this code (in Typinator) and have no problem:

tell application "System Events"
	tell process "Mail"
		tell text field "A:" 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

Thanks. Although I can’t seem to get this to work in TextExpander.