Copy link to clipboard
Copied
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
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
}
Copy link to clipboard
Copied
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..."); }
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Did you set doc ? like
var doc = app.activeDocument;
Copy link to clipboard
Copied
Hi JJ,
Thanks for that, seems to have done the trick.
One more question, how do I set the colour value?
Thanks,
Simon
Copy link to clipboard
Copied
I forgot to say, that I'm only really interested in adding a short message of a limited number of characters, so paragraphs are not an issue.
Simon
Copy link to clipboard
Copied
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
}
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Hi natrev,
I've worked out the array, the numbers relate to the ruler units. So if the ruler is set to inches the text appears 0.75" from the left, and 1.0" from the top.
So now my question is, how do I get the text to range to the left? That is, I would like the user to enter text so that it always ends at the bottom right hand corner of the image.
Thanks,
Simon
Copy link to clipboard
Copied
Hi natrev,
I've worked out the text alignment now. Took me a little bit of trial and error, but I got there.
So this is my code so far:
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.justification = Justification.RIGHT
newTextLayer.textItem.position = Array(99, 99)
newTextLayer.textItem.font = "Helvetica Neue UltraLight Italic"
newTextLayer.textItem.size = 18
newTextLayer.textItem.color = textColor
}
I've set the ruler units to percentage, so the text runs from 99%from top and 99% from the left. It works a treat. My only problem is that the line that selects the font does not work. Can you see where I'm going wrong?
JJ, can you either?
Thank you so much guys, you've been a great help so far.
Simon
Copy link to clipboard
Copied
2fingertyping wrote:
I've set the ruler units to percentage, so the text runs from 99%from top and 99% from the left. It works a treat. My only problem is that the line that selects the font does not work. Can you see where I'm going wrong?
As you have discovered, how Photoshop does some operation depends on how Ruler unites is set. You can mess around with that setting.
When scripting Photoshop I usually do everything with Ruler units set to Pixels. and when dealing with text I work with the document resolution set to 72DPI. Which is like Photoshop point units that text seems to be built on pt.
So my scripts will start with saving the users current Unit setting and saving the document current Resolution. I will the set Photoshop units to pixel and the document resolution to 72DPI. Before the script ends and returns to Photoshop I will restore the users units setting and return the document to its original resolution.
If I want something to be locate somewhere I'll calculate that location using the documents resolution if I need to. The document top left pixel offset is 0X,0Y and the bottom right is PixelsWidthX,PixelsHieghtY. Both + and - offset values are valid. Values can be larger than width and height. Layer can be larger than canvas size and pixels need not be over the canvas area.
Knowing the document Resolution, The Canvas width and height in pixels, Its easy to calculate units like inches in pixels or percentage in in pixels.
.
Copy link to clipboard
Copied
Hi JJ,
Thank you for that.
I've chosen to go with percent, as it then doesn't matter what size the document is, the text will alway appear in the correct location, relative to the document dimensions.
So I now have this:
var docRef = app.activeDocument
//set preference units
var originalRulerPref = app.preferences.rulerUnits;
var originalTypePref = app.preferences.typeUnits;
app.preferences.rulerUnits = Units.PERCENT;
app.preferences.typeUnits = TypeUnits.POINTS;
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.font = "Helvetica Neue UltraLight Italic"
newTextLayer.textItem.justification = Justification.RIGHT
newTextLayer.textItem.position = Array(99, 99)
newTextLayer.textItem.size = 18
newTextLayer.textItem.color = textColor
}
//set preference units back to normal
app.preferences.rulerUnits = originalRulerPref;
app.preferences.typeUnits = originalTypePref;
Which is great. Thank you again for your help and advice. I really appreciate it. I still can't get the font to change from the default though.
Does the code look wrong to you?
Simon
Copy link to clipboard
Copied
I guess the line:
newTextLayer.textItem.font = "Helvetica Neue UltraLight Italic"
should be:
newTextLayer.textItem.font = "HelveticaNeue-UltraLightItalic"
i.e. the *PostScript* name of the font is required.
HTH,
--Mikaeru
Copy link to clipboard
Copied
Hi Mikaeru,
Thank you for that. That worked. Nifty little script too for getting the postscript name.
Simon
Copy link to clipboard
Copied
You need to use the Font internal name.
/* Internal Photoshop Text name */
var fontName = "ArialMT";
var fontName = "TimesNewRomanPSMT";
var fontName = "Tahoma";
You can retrive the Fornt Names from your system. I do that and create pulldown selection menues for user to pick from. And use the index value to address the internal name in the array of the selected font.
for (var i=0,len=app.fonts.length;i<len;i++) {
fontlist = app.fonts.name;
}
var ChosenFontPostScriptName = app.fonts[Math.round(fontNumber)].postScriptName;
Copy link to clipboard
Copied
Wow JJ, that looks amazing!
Simon