Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
0

How to get each line first character position and width of each line in extendscript?

Explorer ,
Apr 10, 2024 Apr 10, 2024

Hi,

 

I need to get each lines first character position(top and left) and width of each line in textrange(text frame) using extendscript.

 

Arunkumar25715058ufpl_0-1712759746409.pngexpand image

Please help.

TOPICS
How-to , Scripting
479
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 2 Correct answers

Explorer , Apr 11, 2024 Apr 11, 2024

Hi RobOctopus,

 

Yes, but I try to achieve the new text frame create based on the this inputs(left, top, width and height).

 

 

// select 1 textFrame
var doc = app.activeDocument;

var frames = doc.selection[0];

var getTextRange = frames.textRange;
var lineArr = [];
for (var i = 0; i < getTextRange.lines.length; i++) {
    var line = getTextRange.lines[i];
	lineArr.push(line.contents);
}
	
var replica = frames.duplicate(frames, ElementPlacement.PLACEAFTER);
var item = replica.createOutline();
var ob
...
Translate
Participant , Apr 12, 2024 Apr 12, 2024

so you want to take the information from 1 file. hold it into a script and recreate on another machine. This, i'm sure makes copy paste information unusable. I would think you would have an easier time getting the path of the object and contents (size, leading, etc) and then recreating that way

psuedo code
get text frames
 for each text frame
  get all path points along their text path (to recreate the path items, allows for irregular shapes)
  get the contents of the text frame
   for each chara
...
Translate
Adobe
Participant ,
Apr 10, 2024 Apr 10, 2024

does this solution not already answer your question you've asked before?

https://community.adobe.com/t5/illustrator-discussions/how-to-get-textrange-each-line-start-and-end-...

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 11, 2024 Apr 11, 2024

Hi RobOctopus,

 

Yes, but I try to achieve the new text frame create based on the this inputs(left, top, width and height).

 

 

// select 1 textFrame
var doc = app.activeDocument;

var frames = doc.selection[0];

var getTextRange = frames.textRange;
var lineArr = [];
for (var i = 0; i < getTextRange.lines.length; i++) {
    var line = getTextRange.lines[i];
	lineArr.push(line.contents);
}
	
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});
	var getpos = temp[i][0];
	//$.writeln(getpos.position);
    output.push(temp[i][0]);
    //output.push(temp[i][temp[i].length - 1]);
}


var myDocument = app.documents.add();
// e.g. of using "ouput", marking the positions of its elements (with red dots)
for (var i = 0; i < output.length; i++) {
    var myTextFrame = myDocument.textFrames.add();
	myTextFrame.position = [output[i].position[0],output[i].position[1]];
	myTextFrame.contents = lineArr[i];
}

 

 

 

But it show empty in new document. This reason only i create the ticket. Please help.

Arunkumar25715058ufpl_0-1712850265547.pngexpand image

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Apr 11, 2024 Apr 11, 2024

If you zoom out, you should see the text showing up (at least if the layer structure is showing there should be text). based on my testing, you need to change your rulerOrigin for the new document.

 

the script is getting the coordinates of the text frame based on 1 rulerOrigin, then the next document is using a different rulerOrigin, so the coordinates are not matching.

I added 

myDocument.rulerOrigin = [0,myDocument.artboards[0].artboardRect[1]]

to the code directly after line 55 when myDocument is added and the text was placing in the same fashion as the original document.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 12, 2024 Apr 12, 2024

Hi RobOctopus,

 

Thanks, Now it is working. But little bit different from original in top position for each line..

Arunkumar25715058ufpl_0-1712913349085.pngexpand image

How to fetch width of each lines?

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Apr 12, 2024 Apr 12, 2024

to get the width of the line add a width statement of the textframe you just created and added contents to

myTextFrame.contents = lineArr[i];
thisWidth = myTextFrame.width

 

in regards to the lines not matching exactly

the script is outlining text to get exact coordinates of the text objects

then duplicating the text by each line to a new frame, but using the coordinates of the text outlines. text frames however include other space above the text which should be the ascender height of the point size, which can be irregular.

 

I am unsure what your end goal is with the script, but it currently seems like it is complicating a process and getting undesired results due to the overcomplication

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 12, 2024 Apr 12, 2024

Hi RobOctopus,

 

Thanks, I am try to recreate a new document with this type text frame in another machine using javascript api and extendscript. In this reason only i get all information of each line like left, top, width etc.,

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Apr 12, 2024 Apr 12, 2024
LATEST

so you want to take the information from 1 file. hold it into a script and recreate on another machine. This, i'm sure makes copy paste information unusable. I would think you would have an easier time getting the path of the object and contents (size, leading, etc) and then recreating that way

psuedo code
get text frames
 for each text frame
  get all path points along their text path (to recreate the path items, allows for irregular shapes)
  get the contents of the text frame
   for each character house loop through and save the size,leading and other pertinent styling options, along with font
all of the above information would be housed in some array format
new doc loop through array
 create path item from coordinates
  add text contents to path
  loop through all character styling and apply correct styles to each

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines