Skip to main content
Community Expert
February 5, 2025
Answered

How to remember the position of a TextLoc at the end of a paragraph

  • February 5, 2025
  • 1 reply
  • 330 views

Hi,

I have a TextLoc problem. Sorry, when this is to basic for some of you.

I have a paragraph with text.

My script does this:

1 Applies a condition to this text.

2 Adds new text after this text.

3 Then applies another condition to the new text.

I do not know, how I can remember the offset of the TextLoc at the end of the text after step 1.

I use Constants.FV_OBJ_END_OFFSET-1 for this TextLoc.

However, when I add the text, the offset is still at the end of the paragraph and not at the end of the first text.

When I store the offset in another variable, it's always at the end of this paragraph.

Is there another way to to count the offset?

Or do I have to use TextItems with GetText and somehow calculate the single text items?

This snippet illustrates what I mean. "789" should be inserted after "123". However, it is added at the end of the paragraph.

#target framemaker
"use strict";
#strict on

var loDoc, loPgf, loTLoc, loTLocStartOffset;

loDoc = app.ActiveDoc;
if (!loDoc.ObjectValid ()) {
    Alert ("Document required ", Constants.FF_ALERT_CONTINUE_NOTE);
}
loFlow = loDoc.MainFlowInDoc;
loPgf = loFlow.FirstTextFrameInFlow.FirstPgf;
loTLoc = new TextLoc ();
loTLoc.obj = loPgf;
loTLoc.offset = 0;
loDoc.AddText (loTLoc, "123");
loTLoc.offset = Constants.FV_OBJ_END_OFFSET-1;
loTLocStartOffset = loTLoc.offset;
$.writeln ("loTLocStart.offset: " + loTLocStart.offset);
loDoc.AddText (loTLoc, "456");
loTLoc.offset = loTLocStartOffset;
loDoc.AddText (loTLocStart, "789");

Thank you very much for your help!

Best regards, Winfried

    Correct answer Klaus Göbel

    Hi Winfried,

    try this:

     


    loFlow = loDoc.MainFlowInDoc;

    loPgf = loFlow.FirstTextFrameInFlow.FirstPgf;

    loTLoc = new TextLoc ();

    loTLoc.obj = loPgf;

    loTLoc.offset = 0;

    loDoc.AddText (loTLoc, "123");

    loTLoc.offset = loTLoc.offset + "123".length

    loTLocStartOffset = loTLoc.offset;

    loDoc.AddText (loTLoc, "456");

    loTLoc.offset += "456".length;

    loDoc.AddText (loTLoc, "789");

     

    You don't have to look for EndOfOffset

    Just "count"

    1 reply

    Klaus Göbel
    Klaus GöbelCorrect answer
    Legend
    February 5, 2025

    Hi Winfried,

    try this:

     


    loFlow = loDoc.MainFlowInDoc;

    loPgf = loFlow.FirstTextFrameInFlow.FirstPgf;

    loTLoc = new TextLoc ();

    loTLoc.obj = loPgf;

    loTLoc.offset = 0;

    loDoc.AddText (loTLoc, "123");

    loTLoc.offset = loTLoc.offset + "123".length

    loTLocStartOffset = loTLoc.offset;

    loDoc.AddText (loTLoc, "456");

    loTLoc.offset += "456".length;

    loDoc.AddText (loTLoc, "789");

     

    You don't have to look for EndOfOffset

    Just "count"

    Community Expert
    February 5, 2025

    Thank you very much!

    This will work, as I will not have any character formats applied in the text or any other non-string text item.

    However, is there also a general solution, when something like this would be in there? Would I have to count all text items and add the number of characters in all strings?

    Community Expert
    February 5, 2025

    Something so that ExtendScript automatically shows the number of text items (characters and all other text changes) from the beginning to this TextLoc?