Skip to main content
Known Participant
February 14, 2024
Answered

How to get textrange each line start and end character cooridnates using extendscript?

  • February 14, 2024
  • 1 reply
  • 516 views

Hi,

 

I need to get each lines start and end character cooridnates points in textrange(text frame) using extendscript.

 

Please help.

This topic has been closed for replies.
Correct answer femkeblanco

Not extensively tested, and not tested with sizeable text.  "ouput" is an array of the left-most and right-most characters.  You can access their position properties. 

// select 1 textFrame
var doc = app.activeDocument;
var frames = doc.selection[0];
var replica = frames.duplicate(frames, ElementPlacement.PLACEAFTER);
var item = replica.createOutline();
var objects = [];
for (var i = 0; i < item.pageItems.length; i++) {
   objects.push(item.pageItems[i]);
}
item.remove();

var temp = [];
for (var i = 0, counter = 1; i < objects.length; i += counter) {
    counter = 1;
    var row = [];
    row.push(objects[i]);
    for (var j = i + 1; j < objects.length; j++) {
        if (horizontalOverlap(objects[i], objects[j])) {
            row.push(objects[j]);
            counter++;
        }
    }
    temp.push(row);
}

function horizontalOverlap(obj1, obj2) {
   var top1 = obj1.geometricBounds[1];
   var bottom1 = obj1.geometricBounds[3];
   var top2 = obj2.geometricBounds[1];
   var bottom2 = obj2.geometricBounds[3];
   return ((top1 >= top2 && bottom1 <= top2) || 
           (top1 <= top2 && top1 >= bottom2));
}

// "ouput" is an array of the left-most and right-most chars
var output = [];
for (var i = 0; i < temp.length; i++) {
    temp[i].sort(function (a, b) {return a.left - b.left});
    output.push(temp[i][0]);
    output.push(temp[i][temp[i].length - 1]);
}

// e.g. of using "ouput", marking the positions of its elements (with red dots)
for (var i = 0; i < output.length; i++) {
    var d = 5;
    var item = doc.pathItems.ellipse(
        output[i].position[1] + 5/2, output[i].position[0] - d/2, d, d);
    var color1 = new RGBColor;
    color1.red = 255;
    color1.green = color1.blue = 0;
    item.stroked = false;
    item.fillColor = color1;
}

 

1 reply

femkeblanco
femkeblancoCorrect answer
Brainiac
February 14, 2024

Not extensively tested, and not tested with sizeable text.  "ouput" is an array of the left-most and right-most characters.  You can access their position properties. 

// select 1 textFrame
var doc = app.activeDocument;
var frames = doc.selection[0];
var replica = frames.duplicate(frames, ElementPlacement.PLACEAFTER);
var item = replica.createOutline();
var objects = [];
for (var i = 0; i < item.pageItems.length; i++) {
   objects.push(item.pageItems[i]);
}
item.remove();

var temp = [];
for (var i = 0, counter = 1; i < objects.length; i += counter) {
    counter = 1;
    var row = [];
    row.push(objects[i]);
    for (var j = i + 1; j < objects.length; j++) {
        if (horizontalOverlap(objects[i], objects[j])) {
            row.push(objects[j]);
            counter++;
        }
    }
    temp.push(row);
}

function horizontalOverlap(obj1, obj2) {
   var top1 = obj1.geometricBounds[1];
   var bottom1 = obj1.geometricBounds[3];
   var top2 = obj2.geometricBounds[1];
   var bottom2 = obj2.geometricBounds[3];
   return ((top1 >= top2 && bottom1 <= top2) || 
           (top1 <= top2 && top1 >= bottom2));
}

// "ouput" is an array of the left-most and right-most chars
var output = [];
for (var i = 0; i < temp.length; i++) {
    temp[i].sort(function (a, b) {return a.left - b.left});
    output.push(temp[i][0]);
    output.push(temp[i][temp[i].length - 1]);
}

// e.g. of using "ouput", marking the positions of its elements (with red dots)
for (var i = 0; i < output.length; i++) {
    var d = 5;
    var item = doc.pathItems.ellipse(
        output[i].position[1] + 5/2, output[i].position[0] - d/2, d, d);
    var color1 = new RGBColor;
    color1.red = 255;
    color1.green = color1.blue = 0;
    item.stroked = false;
    item.fillColor = color1;
}

 

Known Participant
February 16, 2024

Hi Femkeblanco,

 

It is working. Thanks for your help.