Skip to main content
Participant
December 9, 2014
Answered

Working with elements from ExtendScript

  • December 9, 2014
  • 1 reply
  • 1649 views

Hello!

I have a couple of questions concerning working with elements form ExtendScript via FM API.

1) How to assign a text value to an element?

2) Can I insert a new element to an existing element? If it is possible then what objects or functions should I use to do this? They write in a reference that the Element object has Delete() method but I failed to find Insert() method or anything looking like this.

Thank you!

This topic has been closed for replies.
Correct answer Russ Ward

Michael,

These samples do not contain specific examples for what you want to do. To supplement them, here is a function that inserts a new element and adds text to it. Arguments are:

doc (Object) - A valid document object.

parentElem (Object) - The element into which you want to insert the new element. The new element is inserted at the beginning.

newElemTag (String) = The tag for the new element.

newElemText (String) = The text for the new element.

Note that:

  - This function has no error checking, which would be a good idea for a more robust script.

  - When adding elements and inserting text, you have to deal with text and element locations. These can be complicated to understand for a beginner.

Russ

function insertElementAndAddText(doc, parentElem, newElemTag, newElemText)

{

    //Get the element definition from the EDD for a <p> element.

    var elementDef = doc.GetNamedElementDef(newElemTag);

    //Set up the element location for the insertion...

    //the beginning of the current element branch.

    var elementLoc = new ElementLoc();

    elementLoc.parent = parentElem;

    elementLoc.child = parentElem.FirstChildElement;

    elementLoc.offset = 0;

   

    //Insert the new element

    var newElem = elementDef.NewElementInHierarchy(elementLoc);

   

    //Adjust the element location to be at the beginning of the

    //new element branch

    elementLoc.parent = newElem;

    elementLoc.child = newElem.FirstChildElement;

    elementLoc.offset = 0;

    //Set up the text location required to add the text, then add it.

    textLoc = doc.ElementLocToTextLoc(elementLoc);

    doc.AddText(textLoc, newElemText);

}

1 reply

ScottPrentice
Inspiring
December 9, 2014

The answers to all of your questions are provided in these great samples from West Street Consulting ..

     http://www.weststreetconsulting.com/WSC_ExtendScriptSamples.htm

Cheers!

...scott

Participant
December 9, 2014

Thank you very much!

Russ WardCorrect answer
Legend
December 9, 2014

Michael,

These samples do not contain specific examples for what you want to do. To supplement them, here is a function that inserts a new element and adds text to it. Arguments are:

doc (Object) - A valid document object.

parentElem (Object) - The element into which you want to insert the new element. The new element is inserted at the beginning.

newElemTag (String) = The tag for the new element.

newElemText (String) = The text for the new element.

Note that:

  - This function has no error checking, which would be a good idea for a more robust script.

  - When adding elements and inserting text, you have to deal with text and element locations. These can be complicated to understand for a beginner.

Russ

function insertElementAndAddText(doc, parentElem, newElemTag, newElemText)

{

    //Get the element definition from the EDD for a <p> element.

    var elementDef = doc.GetNamedElementDef(newElemTag);

    //Set up the element location for the insertion...

    //the beginning of the current element branch.

    var elementLoc = new ElementLoc();

    elementLoc.parent = parentElem;

    elementLoc.child = parentElem.FirstChildElement;

    elementLoc.offset = 0;

   

    //Insert the new element

    var newElem = elementDef.NewElementInHierarchy(elementLoc);

   

    //Adjust the element location to be at the beginning of the

    //new element branch

    elementLoc.parent = newElem;

    elementLoc.child = newElem.FirstChildElement;

    elementLoc.offset = 0;

    //Set up the text location required to add the text, then add it.

    textLoc = doc.ElementLocToTextLoc(elementLoc);

    doc.AddText(textLoc, newElemText);

}