Skip to main content
Community Expert
October 21, 2025
Answered

ExtendScript: How to get the distance to the side of the text frame

  • October 21, 2025
  • 1 reply
  • 148 views

Hi,
I have an index. The page numbers are right-aligned with a tab to the right side of the text frame.
When the text extends too far to the page number area, I want to insert a line break.
I can get the TextRange or the TextLoc of the single characters in the text.
However, I do not know how to get the distance to the text frame border.
I did not find any LocX property of the TextRange or the TextLoc.
How can I tackle this? Which object and which property do I have to check?
Thank you very much for your help!
Best regards, Winfried

    Correct answer frameexpert

    I found this function in my collection. It takes a text range--for example, the tab before the page number--and calculates its width. If it's smaller than a certain threshhold, then you can do something like break the line.

    function getTextRangeWidth (textRange, doc) {
    	
        var PT, prop, beginOffset, endOffset;
    	
        PT = 65536; // 1 point.
        prop = doc.GetTextPropVal (textRange.beg, Constants.FP_LocX);
        beginOffset = prop.propVal.ival / PT;
        prop = doc.GetTextPropVal (textRange.end, Constants.FP_LocX);
        endOffset = prop.propVal.ival / PT;
    
        return endOffset - beginOffset;
    }
    

    1 reply

    frameexpert
    Community Expert
    Community Expert
    October 21, 2025

    Getting properties from a TextLoc require the GetTextPropVal document function:

    // doc = Doc object; and textLoc = TextLoc object.
    prop = doc.GetTextPropVal (textLoc, Constants.FP_LocX);
    // Get the value in metric units.
    value = prop.propVal.ival;
    // Convert it to points.
    value = value / 65536;

     

    Community Expert
    October 22, 2025

    Hi Rick,

    Thank you very much! This seems to be exactly what I need!

    I will test this hopefully later today.

    Best regards, Winfried

    frameexpert
    Community Expert
    frameexpertCommunity ExpertCorrect answer
    Community Expert
    October 22, 2025

    I found this function in my collection. It takes a text range--for example, the tab before the page number--and calculates its width. If it's smaller than a certain threshhold, then you can do something like break the line.

    function getTextRangeWidth (textRange, doc) {
    	
        var PT, prop, beginOffset, endOffset;
    	
        PT = 65536; // 1 point.
        prop = doc.GetTextPropVal (textRange.beg, Constants.FP_LocX);
        beginOffset = prop.propVal.ival / PT;
        prop = doc.GetTextPropVal (textRange.end, Constants.FP_LocX);
        endOffset = prop.propVal.ival / PT;
    
        return endOffset - beginOffset;
    }