Getting width and Height in specific units

I am currently using the code below to retrieve the width and height of a document. The values are coming back in “distance unit”. Is there a way to specify the units or a method to convert between units?

let doc = await batchPlay(
    [
       {
          "_obj": "get",
          "_target": [
             {
                "_ref": "document",
                "_id": 240
             }
          ],
          "_options": {
             "dialogOptions": "dontDisplay"
          }
       }
    ],{
       "synchronousExecution": false,
       "modalBehavior": "fail"
    });
    let width = doc[0].width._value;
    let height = doc[0].height._value;
    let units = doc[0].width._unit;

I’m not aware of any option to specify the unit of the returned value, but distance unit is essentially just factoring in the current resolution of your document (pixels/inch), relative to the standard resolution of 72ppi.

The formula is distance * ppi / 72 to convert it to pixels and pixels / ppi * 72 for the other way around.

So if your document is 100px wide at 144ppi, the width returned width will be 50 (distance units).
I’ve recently made my unit conversion functions available in a repo, maybe that helps.

Btw, you can tell batchPlay to only get a specific property, which will run faster than getting the whole document descriptor. There’s also multiGet to specify more than one property:

photoshop.action.batchPlay(
    [
      {
        _obj: 'multiGet',
        _target: { _ref: 'document', _enum: 'ordinal', _value: 'targetEnum' },
        extendedReference: [['width', 'height', 'resolution']],
      },
    ],
    { synchronousExecution: true }
  )
1 Like

That will work. thanks. I had to modify the code snippet slightly to the following

require("photoshop").action.batchPlay(
    [
      {
        "_obj": "multiGet",
        "_target": [
             {
                "_ref": "document",
                "_id": 240
             }
          ],
        extendedReference: [['width', 'height', 'resolution']]
      }
    ],
    { synchronousExecution: true }
  );

Oh, right, I just copy/pasted it from my project, so I accidently left the docRef variable in there, which is just { _ref: 'document', _enum: 'ordinal', _value: 'targetEnum' }. I’ll edit it in the post above.

If you reference the document by ID, your code will only work for just that one document, but not for other documents.