Skip to main content
Known Participant
March 6, 2024
Answered

how to add element and text?

  • March 6, 2024
  • 1 reply
  • 549 views

Hello, I struggle with this issue for long but still I can not have answer.
so I wish I can get some hints form here.

I understand this to put element in document.

 

var doc = app.ActiveDoc;

var elementDef = doc.GetNamedElementDef ("p");

var element = elementDef.NewElement (doc.TextSelection.beg);


but how can I put element in parent element?
for exaplme, putting "p" in "body"
and after I put p, how can I add text?


I really wish to get some hints.
thank you.

 

 

This topic has been closed for replies.
Correct answer frameexpert

Adding elements and text is not easy to understand and it takes quite a bit of code. I have a bunch of generatized functions that help me with this. Here is the basic code that will add a p element (and some text) to a selected body element. You will need to generalize this, but it should get you started.

#target framemaker

main ();

function main () {
    
    var doc;
    
    doc = app.ActiveDoc;
    if (doc.ObjectValid () === 1) {
        processDoc (doc);
    }
}

function processDoc (doc) {
    
    var element, elementDef, elementRange, newElement, textLoc;
    
    // Work with the selected element to see how it works.
    element = doc.ElementSelection.beg.child;
    if ((element.ObjectValid () === 1) && (element.ElementDef.Name === "body")) {
        // Get the p element definition.
        elementDef = doc.GetNamedElementDef ("p");
        if (elementDef.ObjectValid () === 1) {
            // Make an element range at the end of the element.
            elementRange = getElementRangeAtEnd (element);
            // Add the element.
            newElement = elementDef.NewElement (doc.ElementLocToTextLoc (elementRange.beg));
            if (newElement.ObjectValid () === 1) {
                // Add the new text to the element.
                elementRange = getElementRangeInside (newElement);
                textLoc = doc.ElementLocToTextLoc (elementRange.beg);
                doc.AddText (textLoc, "Element text!");                
            }
        }
    }
}

function getElementRangeAtEnd (element) {
    
    var elementRange;
    
    elementRange = new ElementRange ();
    elementRange.beg.parent = element;
    elementRange.beg.offset = Constants.FV_OBJ_END_OFFSET;
    elementRange.end.parent = element;
    elementRange.end.offset = Constants.FV_OBJ_END_OFFSET;

    return elementRange;
}


function getElementRangeInside (element) {
    
    var elementRange;
    
    elementRange = new ElementRange ();
    elementRange.beg.parent = element;
    elementRange.beg.offset = 0;
    elementRange.end.parent = element;
    elementRange.end.offset = Constants.FV_OBJ_END_OFFSET;

    return elementRange;
}

1 reply

Known Participant
March 6, 2024

var doc = app.ActiveDoc;

// Get the parent element of the new element
var parentElement = doc.MainFlowInDoc.HighestLevelElement;

// Check if the parent element is valid
if (parentElement.ObjectValid()) {
// Get the element definition for the parent element
var parentElementDef = parentElement.ElementDef;

// Check if the element definition is valid
if (parentElementDef.ObjectValid()) {
// Create a new element based on the parent element definition
var newElement = parentElementDef.NewElement(doc.TextSelection.beg);

// Insert the new element into the parent element
var element = elementDef.NewElement (doc.TextSelection.beg);

// Notify the user about the successful insertion
alert("New element added as a child within the parent element.");
} else {
alert("Element definition for parent element not found.");
}
} else {
alert("No parent element found in the document.");
}
I made script like this and I know I did wrong to define
parent element but even I tried to fix it myself.
I still couldn't do it. 


frameexpert
Community Expert
frameexpertCommunity ExpertCorrect answer
Community Expert
March 6, 2024

Adding elements and text is not easy to understand and it takes quite a bit of code. I have a bunch of generatized functions that help me with this. Here is the basic code that will add a p element (and some text) to a selected body element. You will need to generalize this, but it should get you started.

#target framemaker

main ();

function main () {
    
    var doc;
    
    doc = app.ActiveDoc;
    if (doc.ObjectValid () === 1) {
        processDoc (doc);
    }
}

function processDoc (doc) {
    
    var element, elementDef, elementRange, newElement, textLoc;
    
    // Work with the selected element to see how it works.
    element = doc.ElementSelection.beg.child;
    if ((element.ObjectValid () === 1) && (element.ElementDef.Name === "body")) {
        // Get the p element definition.
        elementDef = doc.GetNamedElementDef ("p");
        if (elementDef.ObjectValid () === 1) {
            // Make an element range at the end of the element.
            elementRange = getElementRangeAtEnd (element);
            // Add the element.
            newElement = elementDef.NewElement (doc.ElementLocToTextLoc (elementRange.beg));
            if (newElement.ObjectValid () === 1) {
                // Add the new text to the element.
                elementRange = getElementRangeInside (newElement);
                textLoc = doc.ElementLocToTextLoc (elementRange.beg);
                doc.AddText (textLoc, "Element text!");                
            }
        }
    }
}

function getElementRangeAtEnd (element) {
    
    var elementRange;
    
    elementRange = new ElementRange ();
    elementRange.beg.parent = element;
    elementRange.beg.offset = Constants.FV_OBJ_END_OFFSET;
    elementRange.end.parent = element;
    elementRange.end.offset = Constants.FV_OBJ_END_OFFSET;

    return elementRange;
}


function getElementRangeInside (element) {
    
    var elementRange;
    
    elementRange = new ElementRange ();
    elementRange.beg.parent = element;
    elementRange.beg.offset = 0;
    elementRange.end.parent = element;
    elementRange.end.offset = Constants.FV_OBJ_END_OFFSET;

    return elementRange;
}