(JavaScript/CEP) Is it possible to get the origin of my page on InDesign?

431 views Asked by At

I'm working on an InDesign plugin and I would like to know if it is possible to get the origin of my page?

I was thinking about something like this :

var origin = app.activeDocument.pages[0].getOrigin();

Thank you in advance for your help !

EDIT:

I will explain my problem more precisely:

The mission I have to accomplish here is to display shapes using coordinates that I receive through an API call.

There's a problem with this: the user can edit the origin (x, y) on InDesign. If it changes the origin of the board, the shapes no longer end up where they should be placed normally on the page.

Here in red, the shapes are in the right place.

In blue, they are not in the right place because I modified the origin of the board. enter image description here So I need to recover the origin of the page to be able to calculate the difference between the origin of my board and the origin of my page. This will allow my shapes to always be in the right place.

2

There are 2 answers

0
RobC On BEST ANSWER

The value of a documents zeroPoint property is an array of two numbers.

  • The number at index 0 is the zero point on the X axis
  • The number at index 1 is the zero point on the Y axis.

For example:

app.activeDocument.zeroPoint;

yields [0,0] for a newly created document, i.e. for a document whose zero point has not been changed from its default position.


Example usage:

When creating a new page item it's necessary to calculate the zeroPoint values to ensure you are positioning it at the desired location.

Example A:

Let's say we want to add a new square shaped text frame on the first page of a document that's always positioned at the top-left corner of the page regardless of the current zero point position.

In which case our code will be akin to the following:

var doc = app.activeDocument;

// 1. Obtain the zero point on the x axis.
var zeroPointX = doc.zeroPoint[0];

// 2. Obtain the zero point on the y axis.
var zeroPointY = doc.zeroPoint[1];

// 3. Create a new text frame that is always positioned at the top left corner
//    of the first page, regardless of the zero point position.
doc.pages.item(0).textFrames.add({
  geometricBounds: [
    0 - zeroPointY,
    0 - zeroPointX,
    50 - zeroPointY,
    50 - zeroPointX
  ],
  contents: "Lorem ipsum dolore"
});

As you can see, in the preceding example, we subtract the zeroPointX and zeroPointY values from the desired geometricBounds values accordingly when defining them for the new text frame.

Example B:

Calculating the the zeroPoint values when setting the geometricBounds can become tedious, particularly when creating multiple page items. It's often simpler and more efficient to:

  1. Obtain the current zero points [x,y].
  2. Set the x and y zero points both to zero.
  3. Then create/add the new page item(s).
  4. Finally, revert the zero points to their original position.

The following gist demonstrates the aforementioned approach:

var doc = app.activeDocument;

// 1. Obtain the original zero point.
var originalZeroPoints = doc.zeroPoint;

// 2. Set x and y zero points both to zero.
doc.zeroPoint = [0,0];

// 3. Create a new text frame that is always positioned at the top left corner
//    of the first page, regardless of the zero point position.
doc.pages.item(0).textFrames.add({
  geometricBounds: [0, 0, 50, 50],
  contents: "Lorem ipsum dolore"
});

// 4. Revert the zero point to original position.
doc.zeroPoint = originalZeroPoints;

As you can see, in this preceding example it produces the same result as "Example A", however we don't do any calculations when setting the geometricBounds, instead we simply define our values as desired, i.e. geometricBounds: [0, 0, 50, 50]

1
Yuri Khristich On

rulerOrigin (if you meant this) is a feature of a document a whole. It's not a feature of a single page.

For example, you can get and see the rulerOrigin of current document this way:

alert(app.activeDocument.viewPreferences.rulerOrigin);

And the same way you can change this feature:

app.activeDocument.viewPreferences.rulerOrigin = RulerOrigin.SPREAD_ORIGIN;

https://documentation.help/InDesign-CS5/pe_RulerOrigin.html