Skip to main content
Participant
May 31, 2024
Answered

Get the first character in a textitem

  • May 31, 2024
  • 1 reply
  • 335 views

I'm looping through the paragraphs in a document, checking for a paragraph name, then I want to check if each new line (other than the first) starts with a specified character, if it does then I'll do other stuff, but I'm feeling dense and can't figure out how to get the first character based on what I have below. 

 

    
    var doc = app.ActiveDoc;
    var pgf = doc.MainFlowInDoc.FirstTextFrameInFlow.FirstPgf;
    while(pgf.ObjectValid() === 1) {
        if (pgf.Name === 'SomePgf') {
            var textList = pgf.GetText(Constants.FTI_LineBegin);
            for (var i = 1; i < textList.length; i++) {
                // how do i get the first character of what is in the text item?
            }
        }
        pgf = pgf.NextPgfInFlow;
    }

 

 I greatly appreciate any help.

Correct answer frameexpert

Here is what you need:

#target framemaker

var doc, pgf, textList, count, i, firstChar;

doc = app.ActiveDoc;
pgf = doc.MainFlowInDoc.FirstTextFrameInFlow.FirstPgf;
if (pgf.Name === 'SomePgf') {
    textList = pgf.GetText (Constants.FTI_LineBegin | Constants.FTI_String);
    count = textList.length;
    for (i = 0; i < count; i += 1) {
        // Test for a string.
        if (textList[i].dataType === Constants.FTI_String) {
            // Is the string at the beginning of the line?
            if (textList[i-1].dataType === Constants.FTI_LineBegin) {
                // Get the first character.
                firstChar = textList[i].sdata.slice (0, 1);
                alert (firstChar);
            }
        }
    }
}

 

1 reply

frameexpert
Community Expert
frameexpertCommunity ExpertCorrect answer
Community Expert
May 31, 2024

Here is what you need:

#target framemaker

var doc, pgf, textList, count, i, firstChar;

doc = app.ActiveDoc;
pgf = doc.MainFlowInDoc.FirstTextFrameInFlow.FirstPgf;
if (pgf.Name === 'SomePgf') {
    textList = pgf.GetText (Constants.FTI_LineBegin | Constants.FTI_String);
    count = textList.length;
    for (i = 0; i < count; i += 1) {
        // Test for a string.
        if (textList[i].dataType === Constants.FTI_String) {
            // Is the string at the beginning of the line?
            if (textList[i-1].dataType === Constants.FTI_LineBegin) {
                // Get the first character.
                firstChar = textList[i].sdata.slice (0, 1);
                alert (firstChar);
            }
        }
    }
}

 

Participant
June 1, 2024

Great, thank you very much Rick!