0

I am creating a script that will place a text box. I want to place the text box in the upper left corner, or (0,0).

When I set the position of the text box to (0,0) in the script the text box is actually placed at (0,-63.7). So the x coordinate is correct, but I don't know what the y coordinate is doing.

Any help would be greatly appreciated, thank you!

Here is my script:

    //get the text field out of the json array
    var textField = json.personalizationTextFields[0];

    //create a new layer and set the layer to text
    newTextLayer = docRef.artLayers.add();
    newTextLayer.kind = LayerKind.TEXT;

    //set the properties of the text field
    newTextLayer.textItem.contents = 'PTS ' + i;
    newTextLayer.textItem.size = textField.fontSize;
    textColor = new RGBColor;
    textColor.hexValue = "FF0000";
    newTextLayer.textItem.color.rgb = textColor;
    newTextLayer.textItem.position = Array(0, 0);
0

When you offset 0,0 then Photoshop moves your object upwards on Y as exactly the height of visual bounds.

Remeber I said it's move object by Visual Bounds, This is the bound which you can see when your smart guides are on or when you see upon selecting objects bound by Ctrl+Thumbnail Click, While the Physical Bound is we can see what we get while doing transformation or pressing Ctrl+T.

So I've found out that every time you set co-ordinates to 0,0 it places x right but always moves your object upwards exactly by your visual height. So all I did was increasing the objects visual height instead of "0" and it seems working.

P.S. If you want to get exact (0,0) position while pressing Ctrl+T, You need to find height of it's Physical bound which seems I can't find and nor you should because it'll move your object way down which will create visual imbalance.

// create a new layer and set the layer to text
newTextLayer = app.activeDocument.artLayers.add();
newTextLayer.kind = LayerKind.TEXT;

//set the properties of the text field
newTextLayer.textItem.contents = 'PTS ' + "Test";
newTextLayer.textItem.size = 50; //dummy size
textColor = new RGBColor;
textColor.hexValue = "FF0000";
newTextLayer.textItem.color.rgb = textColor;

//above code were perfect but photoshop does have anchoring issues maybe and reduces height into y thus it gets moves upside on y axis.
var height = newTextLayer.bounds[3].value - newTextLayer.bounds[1].value;
newTextLayer.textItem.position = Array(0, height);
| improve this answer | |
  • 1
    Aaaaand boom goes the dynamite, this works perfect, thank you! – user531100 Apr 15 at 14:43
0

The y-coordinate is set in such a way that the baseline of your text is at position 0, that's why the top of the text box is a bit higher than that.

I'm not very experienced with Photoshop Scripting, so I don't know how to calculate the difference to adjust for this shift, but I found out that you can set the text item's kind property to paragraph text and then it places the text box as you originally intended. The only thing is that it has (at least in my tests) a very small bouding box for default, so that – depending on your point size – most of the text will be cropped. So you have to make sure to adjust the width and the height of the text item, to allow your text to be visible. In the following example I simply set it to the document's width and height in the end.

#target photoshop

// create a new doc
var docRef = app.documents.add();

// create a new layer and set the layer to text
var textLayer = docRef.artLayers.add();
textLayer.kind = LayerKind.TEXT;

var textColor = new RGBColor;
textColor.hexValue = "FF0000";

var ti = textLayer.textItem;

//set the properties of the text field
ti.kind = TextType.PARAGRAPHTEXT;
ti.contents = 'Lorem Ipsum';
ti.size = 24;
ti.color.rgb = textColor;
ti.position = Array(0, 0);
ti.width = docRef.width;
ti.height = docRef.height;
| improve this answer | |
  • You can add layer height to Y position (textLayer.bounds[3]-textLayer.bounds[1]) instead. This however still will have some offset from a visual top-left coordinate. Another option is load layer as selection and use bounds of a selected area for Y — height of the selection will be exact visual height of the layer. – Sergey Kritskiy Apr 14 at 21:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.