Skip to main content
Inspiring
June 28, 2015
Answered

Working with different unit types and measurements

  • June 28, 2015
  • 3 replies
  • 791 views

When working with objects such as table cells you sometimes need to combine measurements of different unit types. For example, if I wanted to calculate the width of a cell including the cell stroke I could possibly be trying to combine the width in millimeters to the stroke in points.

I could change the users viewPreferences to a single unit type then change them back afterwards but this seems very clunky and if there are problems with the script it leaves the users InDesign settings in a bit of a mess.

What is the best way to work with measurements in this way?

This topic has been closed for replies.
Correct answer Laubender

Then you should have a look at:

CS3 JS: UnitValue

Uwe

3 replies

McShamanAuthor
Inspiring
June 30, 2015

Laubender UnitValue seems to be the best solution. Its only downfall appears to be that it does not support all unit types e.g. Agates. So before running a conversion you need to test that a unit type is supported which could add unnecessary overhead. Especially if you have to run the test over a large collection of objects. Adobe should support all its native unit values don't you think? Is there some place I can put in a scripting feature request?

Kasyan Servetsky‌ I read through the Sidenotes source and found that it appeared to be doing all the calculations between unit types manually.

Kasyan Servetsky
Legend
June 29, 2015

Check out the Sidenotes script written by Peter Kahrel. You can enter any units into the dialog box and they are converted to the default unit automatically. The code is open so you can see how he did it.

McShamanAuthor
Inspiring
June 29, 2015

Thanks I will have a look.

Community Expert
June 29, 2015

@McShaman – You could use a string representation to assign the values together with the measurement unit:

app.documents[0].stories[0].tables[0].cells.everyItem().bottomEdgeStrokeWeight = "3mm"; //MILLIMETERS in this case

Or you could save the properties of the user into a variable and re-apply the values later after doing the rest of your script:

var myUserSettings = app.documents[0].viewPreferences.properties;

doingSomething();

app.documents[0].viewPreferences.properties = myUserSettings;

function doingSomething(){

    //Dummy function:

    //do your stuff here:

  

    app.documents[0].viewPreferences.properties = {

        horizontalMeasurementUnits : MeasurementUnits.MILLIMETERS,

        verticalMeasurementUnits : MeasurementUnits.MILLIMETERS,

        strokeMeasurementUnits : MeasurementUnits.MILLIMETERS

        };

  

    app.documents[0].stories[0].tables[0].cells.everyItem().bottomEdgeStrokeWeight = 3; //mm in this case

  

    };

Uwe

McShamanAuthor
Inspiring
June 29, 2015

As I mentioned in my OP I want to work with multiple unit types... So for example adding the width of a cell (in 'mm') to the stroke of a cell (in 'pt'). Also as I mentioned I don't want to change the users viewPreferences.

LaubenderCommunity ExpertCorrect answer
Community Expert
June 29, 2015

Then you should have a look at:

CS3 JS: UnitValue

Uwe