Copy link to clipboard
Copied
I'm trying the get positions of a line, and since geometricBounds / visibleBounds just give me the frame position I thought I could get it through "resolve()". But I'm apparently missing something; every example I find for resolve() uses AnchorPoint and CoordinateSpaces, but I'm not sure how to reference them
Example:
const id = require("indesign");
const app = id.app;
// We only have one item (line) in the document
const line = app.activeDocument.pageItems.item(0);
// Use resolve method, where all examples look like this:
const position = line.resolve(AnchorPoint.TOP_LEFT_ANCHOR, CoordinateSpaces.SPREAD_COORDINATES)[0];
console.log(position);
Which gives
ReferenceError: AnchorPoint is not defined
How do I set up my code so that it gets the correct "AnchorPoint.TOP_LEFT_ANCHOR" and "CoordinateSpaces.SPREAD_COORDINATES" ?
Or, if the line is pasted into a pageitem:
var gl = app.activeDocument.pageItems[0].graphicLines[0].geometricBounds
alert("X: " +gl[1]+ " Y: " + gl[0])
To use resolve try this:
var gl = app.activeDocument.pageItems[0].graphicLines[0]
var a = gl.paths[0].pathPoints[0].anchor
//returns points
alert(gl.resolve(a,CoordinateSpaces.PARENT_COORDINATES))
As @rob day pointed out - you need to get into PathPoints collection of the Path(s) of the Object.
But I don't think you need to use resolve() ?
Copy link to clipboard
Copied
Hi @Øyvind235243823c6v , Try getting graphicLines:
var gl = app.activeDocument.graphicLines[0].geometricBounds
alert("X: " +gl[1]+ " Y: " + gl[0])
Copy link to clipboard
Copied
Yes, that gives me the frame position, but I need to get the position for the contents of the frame, i.e for each line start and end
E.g. these two lines will give me the same geometricBounds:
I need to find the find the x1,y1,x2,y2 point of each line. The x1,y1 of the blue line is not the x1,y1 of the black line
I was probably a bit unclear describing my problem 🙂
I've looked through all attributes and cannot find any reference, so I thought I needed to use a function like resolve()
Is there any other attributes on
app.activeDocument.graphicLines
that show these values?
Copy link to clipboard
Copied
As @rob day pointed out - you need to get into PathPoints collection of the Path(s) of the Object.
But I don't think you need to use resolve() ?
Copy link to clipboard
Copied
Ah, of course. Resolve was a red herring I guess
I can get the points paths using this
app.activeDocument.graphicLines.item(0).paths.item(0).entirePath
Thanks to both of you 🙂
Copy link to clipboard
Copied
In case you didn’t catch it, entirePath is a nested array of 2 x,y coordinate pairs, so I think you might run into a problem depending on how the lines were rotated or drawn.
app.activeDocument.graphicLines.item(0).paths.item(0).entirePath[0]
Here 45° returns a different value than -135° for the black line because the start of the black line has been rotated 180°. Same problem with a 0° rotation when I draw the line up from the lower right vs. down from the upper left:
Copy link to clipboard
Copied
Just a quick note about the resolve approach.
1. As Rob and Robert said, if you need the (x1, y1) and (x2, y2) of a GraphicLine according to the current ruler coordinate system, best is to parse the array
myGraphicLine.paths[0].entirePath
as it already returns coordinates in that system. As long as you have full control over the user-chosen ruler units and custom center point, these coordinates are fine.
2. In a few cases, depending on your goal, you may want to get and express the bounds of the GraphicLine in a pure coordinate space, which can be done from the ruler coordinates using the complicated syntax
myGraphicLine.resolve( [ [x1, y1], <page_index>], <dest_space>, true )[0]
But it turns out that we can obtain this information more simply. Indeed, the inner bounding box of a regular GraphicLine is ‘flat’ (since it does not depend on its transform state), so you can use
myGraphicLine.resolve(AnchorPoint.topLeftAnchor, <dest_space>)[0]
and
myGraphicLine.resolve(AnchorPoint.bottomRightAnchor, <dest_space>)[0]
to get the coordinates in whatever <dest_space>, e.g. CoordinateSpaces.spreadCoordinates.
Note, however, that these anchor points reflect the visible inner bounding box of the graphic line, so they are still affected by the Weight and Cap settings of the line. If you need the coordinates of the geometric path points, you have to change the location parameters AnchorPoint.topLeftAnchor (resp. AnchorPoint.bottomRightAnchor) into
[ AnchorPoint.topLeftAnchor, BoundingBoxLimits.geometricPathBounds, CoordinateSpaces.innerCoordinates ]
resp.
[ AnchorPoint.bottomRightAnchor, BoundingBoxLimits.geometricPathBounds, CoordinateSpaces.innerCoordinates ]
as detailed, syntax 2.3, in this PDF.
Best,
Marc
Copy link to clipboard
Copied
In case of understanding InDesign's geometrics - @Marc Autret is THE expert, and you HAVE to read his PDF, linked at the end of his reply.
Copy link to clipboard
Copied
Hi Marc, Thanks for posting. I was assuming @Øyvind235243823c6v was looking for the top left and right points of the "X"? So how the line is drawn would affect the returned points. Here‘s an example:
app.scriptPreferences.measurementUnit = MeasurementUnits.POINTS;
var d = app.activeDocument
d.viewPreferences.rulerOrigin = RulerOrigin.PAGE_ORIGIN;
d.zeroPoint = [0, 0];
var gl = app.activeDocument.graphicLines;
var b, p, mrk
for (var i = 0; i < gl.length; i++){
b = gl[i].paths[0].entirePath;
//the path’s first anchor point
p=b[0]
//draw a circle at the point
mrk = gl[i].parentPage.ovals.add({geometricBounds:[0,0,10,10], fillColor:"None", fillStroke:"Black", strokeWeight:1})
mrk.move([p[0]-5,p[1]-5])
};
app.scriptPreferences.measurementUnit = AutoEnum.AUTO_VALUE;
Where I get this—the black line in the righthand "X" was drawn right to left:
Copy link to clipboard
Copied
Or, if the line is pasted into a pageitem:
var gl = app.activeDocument.pageItems[0].graphicLines[0].geometricBounds
alert("X: " +gl[1]+ " Y: " + gl[0])
To use resolve try this:
var gl = app.activeDocument.pageItems[0].graphicLines[0]
var a = gl.paths[0].pathPoints[0].anchor
//returns points
alert(gl.resolve(a,CoordinateSpaces.PARENT_COORDINATES))
Copy link to clipboard
Copied
Yes, this is probably what I'm after 🙂
But how do I find the correct CoordinateSpace enum?
Example, when I run this:
var gl = app.activeDocument.pageItems[0].graphicLines[0]
var a = gl.paths[0].pathPoints[0].anchor
//returns points
alert(gl.resolve(a,CoordinateSpaces.PARENT_COORDINATES))
I get a value for "a", but I get this error for the second parameter:
ReferenceError: CoordinateSpaces is not defined
Copy link to clipboard
Copied
But how do I find the correct CoordinateSpace enum?
https://www.indesignjs.de/extendscriptAPI/indesign-latest/#CoordinateSpaces.html#d1e82908