Skip to main content
Fred.L
Inspiring
August 7, 2022
Answered

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

  • August 7, 2022
  • 2 replies
  • 2147 views

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)

}

 

 

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

This topic has been closed for replies.
Correct answer brian_p_dts

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;

 

2 replies

m1b
Community Expert
Community Expert
August 7, 2022

Hi @Fred.L, 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

Fred.L
Fred.LAuthor
Inspiring
August 8, 2022

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. 

rob day
Community Expert
Community Expert
August 8, 2022

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

 

brian_p_dts
Community Expert
brian_p_dtsCommunity ExpertCorrect answer
Community Expert
August 7, 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;

 

Fred.L
Fred.LAuthor
Inspiring
August 7, 2022

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 ?

rob day
Community Expert
Community Expert
August 7, 2022

Hi @Fred.L 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