Hi,
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[0];
The problem is that almost all properties of the xmlelement (paragraphs, words, etc) are read-only.
Does anyone have any suggestion?
Thanx
It depends on whether you want to maintain formatting. You can use
myXMLElement.contents = app.selection[0].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[0].duplicate(LocationOptions.AT_BEGINNING, myXMLElement.xmlContent);
Jeff
Hi Jeff,
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?
Kind regards
John
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[0].duplicate();
var coordinates = [oldFrame.visibleBounds[1], oldFrame.visibleBounds[0]];
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...
North America
Europe, Middle East and Africa
Asia Pacific