Skip to main content
Known Participant
April 21, 2022
Answered

How to call for graphic line length using extendscript?

  • April 21, 2022
  • 3 replies
  • 767 views

So for example, if you run this code:

myLayer = app.activeDocument.layers.add({name:"Line"})
app.activeDocument.graphicLines.add(myLayer, undefined, undefined, {geometricBounds: [10,10, 13, 14]})

 You see it creates a line. If you go into the properties of the line you can see that the line's length is 5.

How can I call for this length value in Extendscript? Sure, I can do some math using the geometric bounds but I'm curious if there's a way to call it directly?

 

Thanks,

 

Ben

This topic has been closed for replies.
Correct answer rob day

Sorry, my function had x,y reversed, should be this:

 

 

 

 

/**
* Returns geometric bounds for a defined width 
* @ param x the line’s x start
* @ param y the line’s y start
* @ param w the line’s width
* @ return geometric bounds array 
* 
*/
function setLineWidth(x,y,w){
    return [y,x,y,x+w]
};

 

 

 

3 replies

Peter Kahrel
Community Expert
Community Expert
April 22, 2022

Graphic lines don't have a length property, but they do have a strokeWeight property. Rob's approach -- to use the geometricBounds of a line to set its dimensions -- works perfectly fine, but for completeness' sake here is how you'd add a line and use its weight property:

line = app.activeDocument.graphicLines.add (myLayer, {strokeWeight: 2});
line.paths[0].entirePath = [[10,15],[35,15]];

So you create a line, then say that its path is between two points.

P.

rob day
Community Expert
rob dayCommunity ExpertCorrect answer
Community Expert
April 21, 2022

Sorry, my function had x,y reversed, should be this:

 

 

 

 

/**
* Returns geometric bounds for a defined width 
* @ param x the line’s x start
* @ param y the line’s y start
* @ param w the line’s width
* @ return geometric bounds array 
* 
*/
function setLineWidth(x,y,w){
    return [y,x,y,x+w]
};

 

 

 

rob day
Community Expert
Community Expert
April 21, 2022

Hi @Ben-ind , there’s no width or length property, so you’ll have to set the bounds. Here’s the API property listing:

 

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#GraphicLine.html

 

You could write and call helper functions to make it easier. Something like this where my setLineWidth function defines the x,y start and the width for the line—25pts in this case:

 

Edited code:

 

 

app.scriptPreferences.measurementUnit = MeasurementUnits.POINTS;
myLayer = app.activeDocument.layers.add({name:"Line"})
app.activeDocument.graphicLines.add(myLayer, undefined, undefined, {geometricBounds: setLineWidth(10,15,25), strokeColor:"Black"})





/**
* Returns geometric bounds for a defined width 
* @ param x the line’s x start
* @ param y the line’s y start
* @ param w the line’s width
* @ return geometric bounds array 
* 
*/
function setLineWidth(x,y,w){
    return [y,x,y,x+w]
}