Before we try to solve this scripting issue, I'd just like to ask if you've come across @DanThomas's excellent KMFAM before:
It's designed to let you store and insert custom actions, and is much faster than writing a new script or tweaking an existing one every time you want to make a new one. It also selects the newly inserted action(s) in the way you describe. If you haven't already, I highly suggest giving it a try and seeing if it doesn't work better for your needs.