Assuming that the script in question is the one here, then I woud be putting in display dialog lines something like this:
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)
display dialog "theToRecipient is currently set as: #" & theToRecipient & "#"
display dialog "that is " & (count words of theToRecipient) & " words"
if (count words of theToRecipient) is greater than 0 then
display dialog word 1 of theToRecipient & " is the first word"
if (count words of theToRecipient) is greater than 1 then
display dialog word 2 of theToRecipient & " is the second word"
return word 1 of theToRecipient
Unfortunately I don’t use Mail on my Mac so can’t test the script (also I’m actually just jotting this down on iOS - so it is entirely untested) and without the ‘raw data’ that you’re dealing with in terms of what the script is picking up it would be hard to determine.
But hopefully the script above should pop up a window telling you the content of the
theToRecipient variable being picked up from the To field in mail which should be displayed between two hash/pound/sharp/octothorpe symbols … just to show if there’s actually any content at all. Then It should display the number of words making up that variable. If there’s at least one word it will then display that and on the off chance there is more than one it will also display the second … hopefully that will give you the key insight as to what is going on in relation to your recipients.
My stab in the dark would be that there is nothing being returned and you have zero words for the recipient. I think TextExpander would then be inserting nothing from this script and only the subsequent space followed by the comma. But the aim of the display dialogs is to give you that visual feedback.
Should you start writing lots of AppleScripts, then rather than using lots of display dialogs, using a script debugger is a quicker way to help fix issues before you start using a script within TextExpander.
Hope that clarifies enough for you to make some progress.