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

Using resolve() to get coordinates / position

Explorer ,
Oct 15, 2023 Oct 15, 2023

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" ?

TOPICS
Scripting

Views

665

Translate

Translate

Report

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

Community Expert , Oct 15, 2023 Oct 15, 2023

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])

 

Screen Shot 13.png

 

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))

 

Screen Shot 14.png

 

Votes

Translate

Translate
Community Expert , Oct 15, 2023 Oct 15, 2023

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() ? 

 

Votes

Translate

Translate
Community Expert ,
Oct 15, 2023 Oct 15, 2023

Copy link to clipboard

Copied

Hi @Øyvind235243823c6v , Try getting graphicLines:

 

var gl = app.activeDocument.graphicLines[0].geometricBounds

alert("X: " +gl[1]+ "  Y: " + gl[0])

 

Screen Shot 11.png

Votes

Translate

Translate

Report

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 ,
Oct 15, 2023 Oct 15, 2023

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:

 

geometricBounds.png

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?

Votes

Translate

Translate

Report

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
Community Expert ,
Oct 15, 2023 Oct 15, 2023

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() ? 

 

Votes

Translate

Translate

Report

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 ,
Oct 15, 2023 Oct 15, 2023

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 🙂

Votes

Translate

Translate

Report

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
Community Expert ,
Oct 15, 2023 Oct 15, 2023

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:

 

Screen Shot 15.png

 

Screen Shot 16.png

 

 

Votes

Translate

Translate

Report

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
Guide ,
Oct 16, 2023 Oct 16, 2023

Copy link to clipboard

Copied

Hi @Øyvind235243823c6v 

 

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( [ [x1y1], <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

Votes

Translate

Translate

Report

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
Community Expert ,
Oct 16, 2023 Oct 16, 2023

Copy link to clipboard

Copied

@Øyvind235243823c6v

 

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.

 

Votes

Translate

Translate

Report

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
Community Expert ,
Oct 16, 2023 Oct 16, 2023

Copy link to clipboard

Copied

LATEST

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:

 

Screen Shot 18.pngScreen Shot 19.png

Votes

Translate

Translate

Report

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
Community Expert ,
Oct 15, 2023 Oct 15, 2023

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])

 

Screen Shot 13.png

 

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))

 

Screen Shot 14.png

 

Votes

Translate

Translate

Report

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 ,
Oct 15, 2023 Oct 15, 2023

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

 

Votes

Translate

Translate

Report

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
Community Expert ,
Oct 15, 2023 Oct 15, 2023

Copy link to clipboard

Copied

Votes

Translate

Translate

Report

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