I think manipulating the envelopes is still a sore point not only in ReaScript but also for C++ extensions... :-/ It involves getting, parsing and setting the envelope state text chunk.
Also, I don't think there's an API to add a new envelope to a track, so still more manipulation of the text state chunks is needed. (To add envelopes, then of the track state text.)