1

What am I doing

Using the javascript API of the Adobe Photoshop CC 2018 scripting engine:

app.preferences.rulerUnits = Units.MM;
app.preferences.typeUnits = TypeUnits.MM;
app.activeDocument.selection.select([
    [0,0],
    [0, UnitValue(150, "mm")],
    [UnitValue(150, "mm"), UnitValue(150, "mm")],
    [UnitValue(150, "mm"), 0]
]);

What am I expecting to happen?

Photoshop creates a selection of 15×15 cm positioned at the top left.

What is actually happening?

Photoshop creates a selection of 12.7×12.7 pixels? positioned I'm not even sure where exactly.

image

The document's size is 29.7×29.7 cm, so the selection is expected to take half of the artboard. Moreover, the "marching ants" aren't even set to the top left corner of the artboard.

I read most of the referrence plus tried to Google a lot, but the results I'm getting are misleading

UPDATE may not be in pixels after all because UnitValue(150, 'mm').as('px') outputs 425.196850393701 and not 12.7 which I believed to be the pixels value.

UPDATE tried setting the PPI to 300 to match the document's, according to this answer, but getting the same behaviour.

var startUnit=UnitValue(150, "mm");
startUnit.baseValue = UnitValue(1 / 300, "in");
ᴍᴇʜᴏᴠ
  • 4,804
  • 4
  • 44
  • 57

1 Answers1

0

Photoshop likes to work with pixels, the use of inches, milimeters and centimeters is just for the convenience of us humans. Try:

var myDocument = app.activeDocument;
app.preferences.rulerUnits = Units.MM;
var myMM = Number(myDocument.width);
app.preferences.rulerUnits = Units.PIXELS;
var myPixels = Number(myDocument.width);

myDocument.selection.select([
    [0,0],
    [0, 150*myPixels/myMM],
    [150*myPixels/myMM, 150*myPixels/myMM],
    [150*myPixels/myMM, 0]
]);

app.preferences.rulerUnits = Units.MM;

Where myMM is the width of the document in mm, and myPixels is the width of the document in pixels.

cybernetic.nomad
  • 6,100
  • 3
  • 18
  • 31
  • Does it? I find that while defining a path, the unit always is Points (1/72th inch) irrespective the setting of rulerUnits. – user508402 Jan 11 '21 at 14:57