• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

How to get element text with ExtendScript ?

Community Beginner ,
Jul 21, 2020 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

636

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
Jul 21, 2020 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;    
}

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

LATEST

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?

 

Votes

Translate

Translate

Report

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