Depending on how you run the script, Adobe Illustrator may not always provide the same script execution process.
1) Launch via the File menu.
2) Embedding the script into an action.
3) Drag and drop the script file onto an Illustrator window.
4) Drag and drop the script file to the top panel of the Illustrator window (not to be confused with method 3).
In your case, the bug with launching the script from an action may be related to the fact that the scripts create a new document in which the manipulation with the object takes place.