I'm trying to write a script that replaces xml element content with a user selection.
I have document containing a textframe with a "root" tag attached. In this frame I have 2 paragraphs, each tagged with a different tag.
I would like to replace the content of one of the paragraphs with the content of a text selection.
This piece of code doesn't seem to work:
myXMLElement.xmlContent = app.selection;
The problem is that almost all properties of the xmlelement (paragraphs, words, etc) are read-only.
Does anyone have any suggestion?
It depends on whether you want to maintain formatting. You can use
myXMLElement.contents = app.selection.contents;
in the (unlikely?) event that you don't mind blowing away the formatting. Otherwise you'll want to use something like:
myXMLElement.xmlContent.remove(); app.selection.duplicate(LocationOptions.AT_BEGINNING, myXMLElement.xmlContent);
Thanx for your great help. It works like a charm. The world is a better place with people like you...
Just one additional question: I also tried using it for a selected picture, but this doesn't seem to work. Only for text.
Do you know if there is a solution for an image?
If you want to insert the rectangle inline, substitute
for the second line of the snippet I posted above.
I'm sorry for the confusion but what I meant was:
I would also like to be able to duplicate a selected Image into a tagged graphic frame (maybe replacing a existing image within the tagged graphic frame)
Any feedback would be awsome.
Maybe rather than thinking about how to replace the contents of the element you should just get rid of the old one and use the tag to mark up a duplicate of the selection.
var oldFrame = myXMLElement.xmlContent.parent instanceof Rectangle ? myXMLElement.xmlContent.parent : myXMLElement.xmlContent; var newFrame = app.selection.duplicate(); var coordinates = [oldFrame.visibleBounds, oldFrame.visibleBounds]; oldFrame.remove(); newFrame.markup(myXMLElement); newFrame.move(coordinates);
If the old image and the new one aren't the same dimensions you'll have to work that out somehow...