Skip to main content
Participating Frequently
December 19, 2009
Answered

Can anyone help me out w/ a simple question?

  • December 19, 2009
  • 2 replies
  • 869 views

I need a little help getting this to work. This is my first script so go easy on me! 😎

I looked at the forums but being that this is my first script it's still too difficult to decipher everything. In my document that's already open I want the script to make a text box of 50 pixels (for example) and insert my text inside it. Here's what I have...

// Create new text layer

var artLayerRef = activeDocument.artLayers.add()

artLayerRef.kind = LayerKind.TEXT

// Make text BOX on text layer

TextType.PARAGRAPHTEXT.width = 50

// Add text to text layer

var textItemRef = artLayerRef.textItem

textItemRef.contents = "My Text Here. My Text Here. My Text Here"

// End

artLayerRef = null

textItemRef = null

It places the text but doesn't put it in a text box. It's just a line of text that extends off of the page. Anybody know what I can do?


Thanks you!
This topic has been closed for replies.
Correct answer Michael_L_Hale

You need to set kind, width, and height

var artLayerRef = activeDocument.artLayers.add()
artLayerRef.kind = LayerKind.TEXT
// Add text to text layer*
var textItemRef = artLayerRef.textItem;
textItemRef.kind = TextType.PARAGRAPHTEXT;
textItemRef.width = 50;
textItemRef.height = 100;
textItemRef.contents = "My Text Here. My Text Here. My Text Here"

Also unles you set the ruler somewhere else you might want to use UnitValues to make sure the units are what you want them to be.

2 replies

Participating Frequently
December 19, 2009

Thanks Michael and Paul. Both seemed to work fine! You guys rock! Since the text in photoshop is not raster I don't know that the res matters. Of course I'm not at all claiming to know more about js than either of you, i'm just saying that I'll be able to scale the text to my desired size despite the res.

Inspiring
December 19, 2009

It's not the text quality that is the issue. If say in you example you want the text box to be 50 px wide, the line that sets the width will only work correctly if the res is 72. Yes, you can go back in the GUI and resize the textbox. For that matter your first post created a pointtype text layer. You could also convert that to paragraph text in the GUI.

Participating Frequently
December 19, 2009

Oh I see what you're saying. This is even better. Do I need to credit either of you guys for your help in this script?

Michael_L_HaleCorrect answer
Inspiring
December 19, 2009

You need to set kind, width, and height

var artLayerRef = activeDocument.artLayers.add()
artLayerRef.kind = LayerKind.TEXT
// Add text to text layer*
var textItemRef = artLayerRef.textItem;
textItemRef.kind = TextType.PARAGRAPHTEXT;
textItemRef.width = 50;
textItemRef.height = 100;
textItemRef.contents = "My Text Here. My Text Here. My Text Here"

Also unles you set the ruler somewhere else you might want to use UnitValues to make sure the units are what you want them to be.

Paul Riggott
Inspiring
December 19, 2009

Hi Mike, even setting unit values don't seem to work so have to resort back to the 72ppi


var res=activeDocument.resolution;
var artLayerRef = activeDocument.artLayers.add()
artLayerRef.kind = LayerKind.TEXT
var textItemRef = artLayerRef.textItem
// Make text BOX on text layer
textItemRef.kind = TextType.PARAGRAPHTEXT;
textItemRef.width = 50/(res/72); //width in pixels
textItemRef.height = 100/(res/72);//height in pixels
// Add text to text layer
textItemRef.contents = "My Text Here. My Text Here. My Text Here."

Inspiring
December 19, 2009

I don't use paragraphtext much so I didn't notice the bug. If you use a distance UnitValue like in or cm it does work. It Works incorrectly with px and throws an error with %.

Width and height also return the wrong value. It also needs to be corrected for res.