Skip to main content
Inspiring
February 17, 2023
Answered

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

  • February 17, 2023
  • 2 replies
  • 1413 views

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? 

Correct answer brian_p_dts
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;

2 replies

m1b
Community Expert
Community Expert
February 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

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

Inspiring
February 17, 2023

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

rob day
Community Expert
Community Expert
February 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;