Skip to main content
andranika45761163
Known Participant
July 21, 2020
Question

How to get element text with ExtendScript ?

  • July 21, 2020
  • 1 reply
  • 1034 views

Hi,

 

do you know how can i get element text ?

 

ex. I need to get "text" <concept><title>text</title></concept>

 

HighestLevelElement.FirstChildElement  - can you help to proceed this ?

 

    This topic has been closed for replies.

    1 reply

    Legend
    July 21, 2020

    Hi,

     

    Here is a function I use to do it. It only works for container and table cell elements. It's surprisingly complicated to work with text in FrameMaker, but I think it's just the nature of text. The function is not commented, so I hope you can make some sense of it.

     

    Russ

     

            
    //if you want to preserve pgf marks with some sort of character (\n, etc.)
    //send it for pgfMarkSubstitution, otherwise send an empty string.
    function elem_GetElementText(doc, elem, pgfMarkSubstitution)
    {
        var text = "";
        
        if(!doc.ObjectValid()) doc = app.ActiveDoc;
        if(!doc.ObjectValid()) return "";
        
        if(!elem.ObjectValid()) return "";
        
        var tr = new TextRange();
        
        if(elem.ElementType == Constants.FV_FO_TBL_CELL)
        {
            tr.beg.obj = elem.Object.FirstPgf;
            tr.beg.offset = 0;
            tr.end.obj = elem.Object.FirstPgf;
            tr.end.offset = Constants.FV_OBJ_END_OFFSET;
        }
            
        else tr = elem.TextRange;
        
        var ti = doc.GetTextForRange(tr, Constants.FTI_String | Constants.FTI_PgfEnd);
        
        for(var i = 0; i < ti.len; i++)
        {
            if(ti[i].dataType == Constants.FTI_PgfEnd && pgfMarkSubstitution != "")
                text += pgfMarkSubstitution;
            else if(ti[i].dataType == Constants.FTI_String)
                text += ti[i].sdata;
        }
    
        return text;    
    }
    
    andranika45761163
    Known Participant
    July 21, 2020

    why the simple thing has complex solution ? is there a simple way for this ?

    frameexpert
    Community Expert
    Community Expert
    July 21, 2020

    Here is what I use:

     

    #target framemaker
    
    var doc, element;
    
    doc = app.ActiveDoc;
    element = doc.ElementSelection.beg.child;
    
    alert (getText (element, doc));
    
    function getText (textObj, doc) {
        
        // Gets the text from the text object or text range.
    
        var text = "", textItems, i;
        
        // Get a list of the strings in the text object or text range.
        if (textObj.constructor.name !== "TextRange") {
            textItems = textObj.GetText(Constants.FTI_String);
        } else {
             textItems = doc.GetTextForRange(textObj, Constants.FTI_String);
        }
        // Concatenate the strings.
        for (i = 0; i < textItems.len; i += 1) {
            text += (textItems[i].sdata);
        }
        return text; // Return the text
    }

     

    Who cares if it is simple or complex? Just use it like a black box. Copy the function into your script and just call it. All it requires is the text object or range (in your case, the element object) and the document object and you will get back the text. What could be simpler than that?

     

    www.frameexpert.com