Apple Mail: change sender

Summary

This text will be hidden

I have made a script, which uses a rtf-file as a mail body. For that, I uses NSSharingService’s. But, since I have multiple mail accounts, I want to change the sender of the mail depending on which file I use. For that, I use System Events. Now the problem is that the first time, the script works as a charm. But, the second time I perform the script, I get the error:

tell application “System Events”
click pop up button 1 of window 1 of process “Mail”
–> error number -1719 from pop up button 1 of window 1 of process “Mail”
end tell

My script so far is:
tell (current application’s NSSharingService’s sharingServiceNamed:“com.apple.share.Mail.compose”)
set its subject to “This is a test subject”
set its recipients to {“XXX”, “YYY”}
set bijlage to current application’s NSURL’s fileURLWithPath:theattachement
its performWithItems:{attrstring, bijlage}
end tell
tell application “System Events”
tell process “Mail”
click pop up button 1 of window 1
delay 0.5
click menu item 9 of menu 1 of pop up button 1 of window 1
end tell
end tell

Wat is causing the error? Can anyone help?

P.S. How can I insert code in a correct way on the forum, so that it is more readable.

Here are directions on how to format posts: