Copy link to clipboard
Copied
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?
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
...
Copy link to clipboard
Copied
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;
Copy link to clipboard
Copied
My gosh, I've been banging my head against a wall for hours, nice, Thank you!
Copy link to clipboard
Copied
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;
Copy link to clipboard
Copied
So if I use scriptPreferences here do you really need to reset? Is that what m1b does?
Copy link to clipboard
Copied
I make a habit of resetting so that the script doesn’t affect other scripts that might run later.
Copy link to clipboard
Copied
Sometimes it doesn't always work.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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;
Copy link to clipboard
Copied
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;
Copy link to clipboard
Copied
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)
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
@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 )
Copy link to clipboard
Copied
Hi Laubender.
Thank you.
Copy link to clipboard
Copied
@dublove Completely rude and unnecessary comment.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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
Find more inspiration, events, and resources on the new Adobe Community
Explore Now