How to get element text with ExtendScript ?

Community Beginner ,
Jul 21, 2020

Copy link to clipboard

Copied

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 ?

 

Views

96

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Mentor ,
Jul 21, 2020

Copy link to clipboard

Copied

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;    
}

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Community Beginner ,
Jul 21, 2020

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Adobe Community Professional ,
Jul 21, 2020

Copy link to clipboard

Copied

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?

 

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more