Skip navigation
Currently Being Moderated

[CS5-5.5][JS] Replace xml element content with user selection

Aug 7, 2012 11:20 AM

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

 
Replies
  • Currently Being Moderated
    Aug 8, 2012 9:13 AM   in reply to JohnDevlon

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 9, 2012 11:41 AM   in reply to JohnDevlon

    If you want to insert the rectangle inline, substitute

     

    app.selection[0].anchoredObjectSettings.insertAnchoredObject(myXMLElement.insertionPoints[0], AnchorPosition.INLINE_POSITION);
    

     

    for the second line of the snippet I posted above.

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 10, 2012 5:06 PM   in reply to JohnDevlon

    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...

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points