It would be better if I had a working document as close to the actual production file as you can supply to test against.
The script would need to act differently depending on:
The objects parent or spread.
If it is contained within a group, table, or master text frame.
If it has a paragraph or...