Not finding what you are looking for? Request a recipe!
Encapsulate the complicated JavaScript code in a simpler API that your Flash panel can call.
Always wrap your scripts in a try/catch block and then display a more useful error message in an alert dialog.
To maintain the selection, all you have to do is copy the current selection into an array and then restore it at the end of the command.