Skip to main content
Inspiring
Feb 17, 2023 8:38 PM UTC
Answered

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

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? 

brian_p_dts
brian_p_dtsCorrect answer
Community Expert
Feb 17, 2023 8:41 PM UTC
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;

18 replies

m1b
Community Expert
Community Expert
Feb 17, 2023 9:05 PM UTC

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
brian_p_dts
Community Expert
brian_p_dtsCommunity ExpertCorrect answer
Community Expert
Feb 17, 2023 8:41 PM UTC
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
Inspiring
Feb 17, 2023 8:46 PM UTC

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

Translate
rob day
Community Expert
Community Expert
Feb 17, 2023 11:48 PM UTC

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