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

[Script] How to get/ set measurements of a document in any units

Participant ,
Aug 07, 2022 Aug 07, 2022

Copy link to clipboard

Copied

Hi guys,

 

I'm struggling to understand how the get/ set things in various units, like inches, millimeters, points and so on in a script.

 

I've got 2 issues at the moment.

1- the basic following script shows me some information about the active Document. The problem is the datas are not in millimeters (like it is set on my ID), even despite my attempt to set it up in the script. I wonder what's wrong with it, moreover how to get the information set up in the active Document and use it.

DocInformation();

function DocInformation () {

// Boundaries of my document
var myDoc = app.activeDocument;

with(myDoc.viewPreferences){
horizontalMeasurementUnits = MeasurementUnits.MILLIMETERS;
verticalMeasurementUnits = MeasurementUnits.MILLIMETERS;
}

var myDocHeight = myDoc.documentPreferences.pageHeight;
var myDocWidth = myDoc.documentPreferences.pageWidth;
var myDocBleed = myDoc.documentPreferences.documentBleedTopOffset;
var myDocSize = myDoc.documentPreferences.pageSize;

alert ("Document information \nHeight : " + myDocHeight + "\rWidth : " + myDocWidth + "\rBleed : " + myDocBleed + "\rDocument size : " + myDocSize)

}

Doc_Information.jpg

 

 

2- I'm working on moving Rectangles as well and the units I'd like the script to use would be in whatever the page uses, no pixels by default.

 

Document and ID are set in millimeters but the following line understands the resize in pixels. How can it be ? I'm missing something and need your help.

myRectangle.resize(CoordinateSpaces.PARENT_COORDINATES, AnchorPoint.TOP_LEFT_ANCHOR, ResizeMethods.REPLACING_CURRENT_DIMENSIONS_WITH,[50,50], true, true);

 

As usual, thanks for the help,

 

Fred

TOPICS
Scripting

Views

230

Likes

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 1 Correct answer

Community Expert , Aug 07, 2022 Aug 07, 2022

Hi Fred, for issue two, resize always works in points (not pixels), so you need to convert your desired measurement to points before passing to the resize method.

For the first issue, try setting your script unit preferences to MM: 

app.scriptPreferences.measurementUnit = MeasurementUnits.MILLIMETERS;

 

Likes

Translate

Translate
Community Expert ,
Aug 07, 2022 Aug 07, 2022

Copy link to clipboard

Copied

Hi Fred, for issue two, resize always works in points (not pixels), so you need to convert your desired measurement to points before passing to the resize method.

For the first issue, try setting your script unit preferences to MM: 

app.scriptPreferences.measurementUnit = MeasurementUnits.MILLIMETERS;

 

Likes

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
Participant ,
Aug 07, 2022 Aug 07, 2022

Copy link to clipboard

Copied

Thank you very much for the clues,

 

First issue is fixed !

 

Second as well, once the values are multiplied by 2,834467120181406 (ratio between mm and points).

However, I find the the need of conversion quite weird. Is there no other solution that would set up all datas in millimeters ?

Likes

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 ,
Aug 07, 2022 Aug 07, 2022

Copy link to clipboard

Copied

Hi @-Dain- The 6th resize parameter is a boolean lets you change the default points to the current ruler units

 

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

Likes

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 ,
Aug 07, 2022 Aug 07, 2022

Copy link to clipboard

Copied

Not sure why it isn’t working, you have it set to true. From the API:

 

If true then a ruler location is interpreted using ruler units rather than points. The default value is false. This parameter has no effect unless the reference point is specified relative to a page. (Optional)

(default: false)

 

You can also change the width and height via geometricBounds:

 

 

 

var w = 50
var h = 50

//set scripting units
app.scriptPreferences.measurementUnit = MeasurementUnits.MILLIMETERS;
var myRectangle = app.documents[0].selection[0]

//set the width and height via bounds
var b = myRectangle.geometricBounds;
myRectangle.geometricBounds = [b[0], b[1], b[0]+h, b[1]+w]

//reset
app.scriptPreferences.measurementUnit = AutoEnum.AUTO_VALUE;

 

 

Likes

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
Participant ,
Aug 07, 2022 Aug 07, 2022

Copy link to clipboard

Copied

Thanks for your help,

I think the last bit will be of some use because I really have a hard time understanding why I had to manage datas  in both millimeters and points units, in the same script. It makes no sense to me. Maybe I'm doing something wrong. It seems like Resize Method can be used with whatever units the document uses, but the Move Method can only be used via "points" coordinates. Nothing that cannot be handled, but I found weird that it's not consistent.

 

I don't know why it wasn't working well either. When I saw the "consideringRulerUnits" and put it on "True",  I was thrilled that I found the solution by myself. But it didn't work so I still have no idea what is going on with that parameter.

 

Anyway, I spent some time on it and made quite some progress. I should be able to show something, hopefully usefull to the community. It's all about Filling the Bleed of a document with a picture, by flipping and filling the space. We'll see soon enough

Likes

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 ,
Aug 07, 2022 Aug 07, 2022

Copy link to clipboard

Copied

I’m not sure what the API  means by reference in—the reference point is specified relative to a page—maybe someone else will.

 

Why not set you scripting preferences to points? Then any measurement you get or set will return as points—if you want to know what the document top bleed is set to you can get it as points via your .documentBleedOffset when the scripting units have been set as points.

 

Make sure you save and reset the current scripting units or you could break other scripts that set the scripting units.

 

Likes

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 ,
Aug 08, 2022 Aug 08, 2022

Copy link to clipboard

Copied

Hi all,

 

The consideringRulerUnits parameter only applies to “ruler locations” and has no other effect that interpreting such location with respect to ruler units. In the resize method, this can only relate to the “transform origin” (2nd parameter), but if that origin is supplied as a “space location” or as a “bounding box location” (like in your example), then consideringRulerUnits has no effect at all.

 

All other absolute values are expected in points, and those dimensions are understood in the perspective of a coordinate space that you can specify. Indeed, a length of 10 pt in space A is not necessarily equivalent to 10 pt in space B since the A→B matrix may contain SCALING attributes. That's why the [xy] arrays of the values parameter offer a third, optional coordinate space element. For example,

 

    [10, 15, CoordinateSpaces.PASTEBOARD_COORDINATES]

 

means x=10 (pt) and y=15 (pt) in the perspective of the pasteboard space.

 

Note. — For more detail on the three kinds of location specifiers, see www.indiscripts.com/blog/public/data/coordinate-spaces-and-transformations-5/CoordinateSpacesTransfo...

 

Best,

Marc

Likes

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
Participant ,
Aug 08, 2022 Aug 08, 2022

Copy link to clipboard

Copied

Thanks Marc,

It works as charm. As it happens, I downloaded your pdf a while ago, but let's say I understood the theory but it takes a lot more than just that for me to be able to actually use it.

 

if I may add, "CoordinateSpaces PASTEBOARD COORDINATES" are not to be included in the array of the coordinates, making the code as follow.

 

myRectangle.move(undefined,[x,y], CoordinateSpaces.PASTEBOARD_COORDINATES);

 

 

Anyway,

Thanks a lot for jumping in. You solved the problem (again)

Likes

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 ,
Aug 07, 2022 Aug 07, 2022

Copy link to clipboard

Copied

Hi @-Dain-, just wanted to add my own comment: I almost always just use points in scripts. I think it makes things easier. I only deal with units in two places:

 

1) from the script settings or user interface (convert from user's preferred unit to points), and

2) after the script, when returning values to user (convert from points to user's preferred units).

 

This keeps everything simple and robust, in my opinion. This is just my preference; it doesn't have to be yours. 🙂

- Mark

Likes

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
Participant ,
Aug 08, 2022 Aug 08, 2022

Copy link to clipboard

Copied

Thanks for your contribution 😉

 

It makes sense I guess. I probably have to write some more scripts to better understand the real added-value of it. I'll see soon enough. 

Likes

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 ,
Aug 08, 2022 Aug 08, 2022

Copy link to clipboard

Copied

LATEST

I also prefer setting points via the scriptingPreferences, then returning the measurement units to AUTO_VALUE so other sripts are not affected.

 

Might be worth noting that there is a subtle difference between setting the ruler units vs. scripting units. When you set the scripting units, type measurements are affected—set  millimeters, and a text’s pointSize will be set as millimeters, but the UI will show points:

 

app.scriptPreferences.measurementUnit = MeasurementUnits.MILLIMETERS;

var doc = app.documents[0];

//some selected text
var t = doc.selection[0];
t.pointSize = 50;

$.writeln(t.pointSize);
//returns 50, UI shows 141.73

//reset units to auto
app.scriptPreferences.measurementUnit = AutoEnum.AUTO_VALUE;

$.writeln(t.pointSize)
//returns 141.73, UI shows 141.73

 

If I set the ruler units to millimeters, type sets as points not millimeters

 

var doc = app.documents[0];
//save ruller settings
var vp = doc.viewPreferences.properties;
//set rulers to millimeters
doc.viewPreferences.properties = {horizontalMeasurementUnits:MeasurementUnits.MILLIMETERS, verticalMeasurementUnits:MeasurementUnits.MILLIMETERS};

//some selected text
var t = doc.selection[0];
t.pointSize = 50;

$.writeln(t.pointSize);
//returns 50, UI shows 50

//reset rulers
doc.viewPreferences.properties = vp
$.writeln(t.pointSize);
//returns 50

 

Likes

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