Skip to main content
Inspiring
December 23, 2021
Question

Making a text layer with specific size

  • December 23, 2021
  • 3 replies
  • 450 views

I try to make a text layer with both a font and layer size.

I made this test script...

 

var startRulerUnits = app.preferences.rulerUnits; 
app.preferences.rulerUnits = Units.PIXELS; 

var layers = app.activeDocument.artLayers;
var layer = layers.add();
layer.kind = LayerKind.TEXT;
    
var textItem = layer.textItem;
textItem.kind = TextType.PARAGRAPHTEXT;
textItem.size = new UnitValue(100, 'px');
textItem.position = [0, 0];
textItem.contents = "test";

textItem.width = new UnitValue(100, 'px');
textItem.height = new UnitValue(100, 'px');
    
app.preferences.rulerUnits=startRulerUnits; 

 

But I noticed the sizes are not correct. It seems that even setting the unit to "px", the size is only correct on 72 dpi. When the document is set at 300 dpi, the layer size of 100 px becomes 416 px when running this script. I also tried setting the rulerUnits but that does not seem to solve the issue.

I also tried changing "px" to "pt" and that doesn't change anything, same result in size. Maybe this is a bug when PS always uses this unit as "pt", even when it's set to "px"?

This topic has been closed for replies.

3 replies

Legend
December 23, 2021

Sets text size to 100px

 

app.preferences.typeUnits = TypeUnits.PIXELS; // optional

var d = new ActionDescriptor();
var r = new ActionReference();
r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("textStyle"));
r.putEnumerated(stringIDToTypeID("textLayer"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));
d.putReference(stringIDToTypeID("null"), r);
var d1 = new ActionDescriptor();
d1.putUnitDouble(stringIDToTypeID("size"), stringIDToTypeID("pixelsUnit"), 100);  // 100px
d.putObject(stringIDToTypeID("to"), stringIDToTypeID("textStyle"), d1);
executeAction(stringIDToTypeID("set"), d, DialogModes.NO);
Kukurykus
Legend
December 23, 2021
100 / activeDocument.resolution * 72
Chuck Uebele
Community Expert
Community Expert
December 23, 2021

Yes, I feel it's a bug also. You really just have to work with points, when dealing with text. I think it's the same, when creating paths with a script.