Skip to main content
2fingertyping
Known Participant
June 9, 2016
Answered

Automating the creation of a text layer for a user to manually add text.

  • June 9, 2016
  • 2 replies
  • 1977 views

Hi,

I wonder if anyone can help me here?

What I would like to do is to write a script, which creates a text layer, pauses and invites a user to add their text, and then save that to the file.

So far I've got this:

docRef = app.activeDocument

textColor = new SolidColor

textColor.rgb.red = 255

textColor.rgb.green = 255

textColor.rgb.blue = 255

helloWorldText = "Hello, World!"

newTextLayer = docRef.artLayers.add()

newTextLayer.kind = LayerKind.TEXT

newTextLayer.textItem.contents = helloWorldText

newTextLayer.textItem.position = Array(0.75, 1)

newTextLayer.textItem.size = 36

newTextLayer.textItem.color = textColor

Obviously this script just adds whatever text is in the line helloWorldText.

As I say I would like the script to stop, prompt the user to add their text, be ok'd, then complete the script.

Any suggestions?

Thank you,

Simon

This topic has been closed for replies.
Correct answer natrev

Hi,

Try this..

var docRef = app.activeDocument

textColor = new SolidColor

textColor.rgb.red = 255

textColor.rgb.green = 255

textColor.rgb.blue = 255

var userText = prompt ("Please enter your message here.","");

if(userText!=""){ 

    newTextLayer = docRef.artLayers.add()

    newTextLayer.kind = LayerKind.TEXT

    newTextLayer.textItem.contents = userText

    newTextLayer.textItem.position = Array(0.75, 1)

    newTextLayer.textItem.size = 36

    newTextLayer.textItem.color = textColor

}

2 replies

natrev
natrevCorrect answer
Legend
June 10, 2016

Hi,

Try this..

var docRef = app.activeDocument

textColor = new SolidColor

textColor.rgb.red = 255

textColor.rgb.green = 255

textColor.rgb.blue = 255

var userText = prompt ("Please enter your message here.","");

if(userText!=""){ 

    newTextLayer = docRef.artLayers.add()

    newTextLayer.kind = LayerKind.TEXT

    newTextLayer.textItem.contents = userText

    newTextLayer.textItem.position = Array(0.75, 1)

    newTextLayer.textItem.size = 36

    newTextLayer.textItem.color = textColor

}

2fingertyping
Known Participant
June 10, 2016

Thank you natrev,

That works a treat.

How do I move the location of the text. I can see it's line 11, but don't understand how the array works.

Also, is it possible to define the font in the script? If so, how?

Thanks,

Simon

JJMack
Community Expert
Community Expert
June 9, 2016

I think you would be better off prompting the user for the text then add the text layer you could then choose an approbate font size once you know the number of characters the user want in the text.  You need to calculate the font size and position the layer for the text size.

var userText = prompt ("Enter Text you want to add for xxxxx ");

Text goes something like this it old code I did not understand how text worked when I wrote it. Some vars use here their sets are not are not shown like font name color etc. Some setting are commented our for I wanted to later position the layer via an action. Paragraphs can not be position by my action.

// save Document resolutiom and set it to 72 DPI restore resolutio before returing to user working units pixels //

// I player with ressolution using a var testres and a size factor this code is cut from a scrio I wrote not knowing how text worked //

text_layer = doc.artLayers.add(); // Add a Layer

text_layer.name = "Layer Name"; // Name Layer

text_layer.kind = LayerKind.TEXT; // Make Layer a Text Layer

text_layer.textItem.color = textColor; // set text layer color

/* Do not set TextType to Pargarph Text for StampEXIF so action can position text layer

text_layer.textItem.kind = TextType.PARAGRAPHTEXT; // Set text layers text type

*/

text_layer.textItem.font = fontName; // set text font

text_layer.blendMode = BlendMode.NORMAL // blend mode

text_layer.textItem.fauxBold = false; // Bold

text_layer.textItem.fauxItalic = false; // Italic

text_layer.textItem.underline = UnderlineType.UNDERLINEOFF; // Underlibn

text_layer.textItem.capitalization = TextCase.NORMAL; // Case

text_layer.textItem.antiAliasMethod = AntiAlias.SHARP; // antiAlias

/* Calulate font size to use for Text keep size same for landscape and portrait base on document size here I use 30 as max character length */

if (doc.width >= doc.height) {var fontSize = Math.round(doc.height / (30 * sizeFactor));}

else {var fontSize = Math.round(doc.width / (30 * sizeFactor));}

if (fontSize<10){fontSize=10}; // don't use Font size smaller then 10

text_layer.textItem.size = fontSize; // set text font Size

//text_layer.textItem.position = Array(textX, textY ); // set text layers position in and down textx and testy need a setting

text_layer.textItem.position = Array(textX, (textY + fontSize )); // set text layers position in and down for Stamp add in fontsize

textWidth = ((doc.width - textX) * 72/testres ); // Text width document width - offset

textHeight = ((doc.height - textY) * 72/testres ); // Text height document height - offset

/* Do not set Text Area  so action can position text layer

text_layer.textItem.width = textWidth; // set text area width

text_layer.textItem.height = textHeight; // set text area height

*/

try{text_layer.textItem.contents = userText;}

catch (er) {alert("Error Setting Contents..."); }

JJMack
2fingertyping
Known Participant
June 9, 2016

Hi JJ,

Thank you for that. Good idea asking for the text input first. I hadn't thought of that.

OK, so here's where I am with your suggestion. I really like the code you've written and understand all the parameters you've set, but I'm getting an error message at line 5, which is:

Error 2: docRef is undefined.

Being relatively new to scripting I don't really know what this means.

What I have done is add this bit of code:

  var userText = prompt ("Please enter your message here."); 

to the beginning of the code you sent me.

So, to recap, what I would like to happen next is for the message the user adds to be made in to a new text layer, which I can script to appear in whatever colour, font, size, style, and most importantly, location in the open document.

Where am I going wrong?

Thank you for your help, it is much appreciated.

Simon

JJMack
Community Expert
Community Expert
June 9, 2016

Did you set doc ?  like

var doc = app.activeDocument;

JJMack