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

Storing the MeasurementUnits, changing them for the script and restoring them

Engaged ,
Feb 17, 2023 Feb 17, 2023

I keep running into an issue where people have their default units set to different measurements and it keeps breaking my scripts. I would like to set the measurement at the beginning of each script and then change them back but nothing seems to work, does someone know how this can be done? 

TOPICS
Scripting
1.1K
Translate
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 , Feb 17, 2023 Feb 17, 2023
var currentHorPrefs = app.viewPreferences.horizontalMeasurementUnits;
var currentVertPrefs = app.viewPreferences.verticalMeasurementUnits;
var currentScriptUnits = app.scriptPreferences.measurementUnit; 

app.scriptPreferences.measurementUnit = MeasurementUnits.POINTS;
app.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.POINTS;
app.viewPreferences.verticalMeasurementUnits = MeasurementUnits.POINTS;

//your script

app.scriptPreferences.measurementUnit = currentScriptUnits;
app.view
...
Translate
Community Expert ,
Feb 17, 2023 Feb 17, 2023
var currentHorPrefs = app.viewPreferences.horizontalMeasurementUnits;
var currentVertPrefs = app.viewPreferences.verticalMeasurementUnits;
var currentScriptUnits = app.scriptPreferences.measurementUnit; 

app.scriptPreferences.measurementUnit = MeasurementUnits.POINTS;
app.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.POINTS;
app.viewPreferences.verticalMeasurementUnits = MeasurementUnits.POINTS;

//your script

app.scriptPreferences.measurementUnit = currentScriptUnits;
app.viewPreferences.horizontalMeasurementUnits = currentHorPrefs;
app.viewPreferences.verticalMeasurementUnits = currentVertPrefs;

Translate
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
Engaged ,
Feb 17, 2023 Feb 17, 2023

My gosh, I've been banging my head against a wall for hours, nice, Thank you! 

Translate
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 ,
Feb 17, 2023 Feb 17, 2023

Hi @davidn5918184 , As Mark suggests you can set the scripting preference for units rather than deal with the user’s settings, and the reset is also easy. There are some methods that use points no matter what the units are set to:

 

app.scriptPreferences.measurementUnit = MeasurementUnits.POINTS;
//Do something
//reset
app.scriptPreferences.measurementUnit = AutoEnum.AUTO_VALUE;

 

Translate
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
Engaged ,
Feb 18, 2023 Feb 18, 2023

So if I use scriptPreferences here do you really need to reset? Is that what m1b does? 

Translate
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 ,
Feb 18, 2023 Feb 18, 2023

I make a habit of resetting so that the script doesn’t affect other scripts that might run later.

Translate
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 04, 2025 Oct 04, 2025

Sometimes it doesn't always work.

Translate
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 04, 2025 Oct 04, 2025

Sometimes it doesn't always work

 

There are some methods that by default return the native POINTS—like the dialog’s ambiguous MeasurementEditbox, or  transform(). For that reason I generally prefer to use POINTS when setting a script’s measurementUnits

 

Translate
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 04, 2025 Oct 04, 2025

Hi rob day

Is it possible to set the default units for new documents using a script?
I feel like sometimes it changes to POINTS.
I need it to stay in millimeters.

 

I know that turning everything off allows for manual configuration.

dublove_0-1759587735126.jpeg

 

Translate
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 04, 2025 Oct 04, 2025

You can set ruler units for the application of a document. Or, you can set the script’s measurement unit, which ignores the user’s ruler units—you would have to watch out for the few methods that always return points. See @brian_p_dts and @m1b  replies

 

//sets the application’s ruler units—the ruler units initially used for any new documents
app.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.MILLIMETERS;
app.viewPreferences.verticalMeasurementUnits = MeasurementUnits.MILLIMETERS;

//sets the ruler units for the active document
app.activeDocument.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.MILLIMETERS;
app.activeDocument.viewPreferences.verticalMeasurementUnits = MeasurementUnits.MILLIMETERS;

//sets the measurement units used by the script ignores the current ruler unit setting
//a few methods always returns points you would have to coerce points to millimeters—multiple by 2.83465
app.scriptPreferences.measurementUnit = MeasurementUnits.MILLIMETERS;
Translate
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 04, 2025 Oct 04, 2025

I understand that now.
I'm currently using point calculations, and then displaying the results in millimeters.

 

I've also seen you use this before—it should have the same effect as app.activeDocument, right?

    app.documents.item(0).viewPreferences.horizontalMeasurementUnits = MeasurementUnits.MILLIMETERS;
    app.documents.item(0).viewPreferences.verticalMeasurementUnits = MeasurementUnits.MILLIMETERS;

 

Translate
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 04, 2025 Oct 04, 2025

I've also seen you use this before—it should have the same effect as app.activeDocument, right?

 

I never set the rulers for my scripts anymore—always use app.scriptPreferences.measurementUnit to set the scripting units, and usually use POINTS

 

These all should return the same result:

 

var d = app.activeDocument;
var dd = app.documents[0]
var ddd = app.documents.item(0)
$.writeln(d.name)
$.writeln(dd.name)
$.writeln(ddd.name)

 

 

 

 

Translate
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 04, 2025 Oct 04, 2025

By @rob day

I never set the rulers for my scripts anymore—always use app.scriptPreferences.measurementUnit to set the scripting units, and usually use POINTS

 

This is a bit unbelievable.


Sometimes I need to input 8mm, so I set the unit to mm.

If no input is needed, I use POINTS—which is definitely the most convenient option.

Translate
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 04, 2025 Oct 04, 2025

Sometimes I need to input 8mm, so I set the unit to mm.

 

If by input you are referring to InDesign’s native dialog’s measurementEditboxes object, it returns Points—doesn’t matter what you have your ruler units, or measurementUnit script preferences set to. But it’s easy to convert the returned Points to Millimeters.

Translate
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 05, 2025 Oct 05, 2025

@dublove said: "… Sometimes I need to input 8mm, so I set the unit to mm."

 

You could use a string like "8 mm" for input. It will translate to the current set of ruler measurement units.

Example:

app.documents[0].rectangles.add
(
{
	geometricBounds : [ "0 mm" , "0 mm" , "8 mm" , "8 mm" ] ,
	strokeWeight : "0 mm" ,
	strokeColor : "None"
}
);

 

Kind regards,
Uwe Laubender
( Adobe Community Expert )

Translate
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 08, 2025 Oct 08, 2025

Hi Laubender.
Thank you.

Translate
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 08, 2025 Oct 08, 2025
LATEST

@dublove Completely rude and unnecessary comment.

Translate
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 04, 2025 Oct 04, 2025

Also, if you are going to change the user’s ruler units, you should reset them as a courtesy to the user—@brian_p_dts is doing that in his example

Translate
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 ,
Feb 17, 2023 Feb 17, 2023

In case you haven't seen it, have a look at the ScriptPreferences documentation. You can set a few useful things there. Note that the docs call is ScriptPreference (singular) but it must be plural like Brian used it. Also, just to add my personal experiences: I never bother to set ScriptPreferences back to their original settings, because I expect any script to set them how they need them, and also in most cases you only need to set script preferences, and don't need to mess with any other preferences. - Mark

Translate
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