Skip to main content
dublove
Legend
August 21, 2026
Answered

How to obtain the longest character with ":" and the offset value of the first character after ":"?

  • August 21, 2026
  • 4 replies
  • 83 views

I have chosen some regular text, and I want to get the horizontalOffset of the first character after the longest character ':'.

The horizontal offset of the word I circled in the picture.
May I ask how to write the code? 

Thank you very much.

 

    Correct answer Peter Kahrel

    Select the text frame with the colons (or select a few lines), then…

    app.findGrepPreferences = null;
    app.findGrepPreferences.findWhat = ': \\K';
    x = app.selection[0].findGrep();

    max = 0;
    for (i = 0; i < x.length; i++) {
    max = Math.max (x[i].horizontalOffset);
    }

    alert (max);

     

    4 replies

    Peter Kahrel
    Community Expert
    Peter KahrelCommunity ExpertCorrect answer
    Community Expert
    August 21, 2026

    Select the text frame with the colons (or select a few lines), then…

    app.findGrepPreferences = null;
    app.findGrepPreferences.findWhat = ': \\K';
    x = app.selection[0].findGrep();

    max = 0;
    for (i = 0; i < x.length; i++) {
    max = Math.max (x[i].horizontalOffset);
    }

    alert (max);

     

    dublove
    dubloveAuthor
    Legend
    August 21, 2026

    Hi ​@Peter Kahrel 

    I tested it
    alert(x[i].horizontalOffset);
    It seems that none of the three values are correct.

    Peter Kahrel
    Community Expert
    Community Expert
    August 21, 2026

    Well, you wanted the biggest value, so there's only one correct value.

    In what way is it not correct?