Below there a link to an attachment that contains an indd file, a script and an image:
You create a textfield, large enough for several paragraphs.
Select the textfield
run the code.
The code will create a copy of the textfield you've selected, it fills the field with text, applies styles to it and copies it back to the original textfield.
Once you arrive at the property which contains te image, it will create an anchored object and places the image inside. done.
If you insert the texts once, all is good.
If you insert the texts twice, you'll see that the last added item gets the anchored object.
What am I doing wrong?
Updated the script; it had some errors...
Should be okay now.