The ideas here are sound, but we must be careful to maintain the control structures, that is, to make sure that we start the script with "on" and end it with "end".
So it is important to test this, and generically, to place the new modified code BEFORE the last line of the existing script. That usually entails a generous use of returns, and specifically, the insertion of a return, prepended to the new code snippet, AFTER line "-2" of the existing code:
put return & yourNewCode after line -2 of yourOldScript
set the script of yourObject to yourOldScript
Craig Newman