Copy link to clipboard
Copied
Hi all,
I've written a script that takes a user input and puts it into the layerName.textItem.contents part of a text layer, sample code below.
var docRef = app.activeDocument;
var win = new Window("dialog", "Hello world");
var input = win.add("edittext", [0,0,400,40], "", {multiline:true, wantReturn:true});
input.active = true;
var go = win.add("button", undefined, "Go");
function makeText() {
var myText = docRef.artLayers.add();
myText.kind = LayerKind.TEXT;
var ref = myText.textItem;
ref.kind = TextType.POINTTEXT;
ref.size = 50;
ref.contents = input.text.replace(/\\n/g,'\r');
win.close();
}
go.onClick = function() {
docRef.suspendHistory("text", "makeText()");
}
win.show();
However, simply using Enter for a new line in the input box produces unexpected results:

Oddly, copying the symbol and pasting it elsewhere reveals it to be a new line character, it's just not recognised by Photoshop.
By setting wantReturn to false and adding in a keypress eventListener:
win.addEventListener("keydown", function(kd) {pressed(kd)});
function pressed(k) {
if (k.keyName == "Enter") {
input.text += "\\n";
}
}
I'm able to get a proper new line, though each press of Enter brings the cursor to the beginning of the input field, requiring the user to manually move the cursor to the end before they can continue to type. This also (obviously I guess) puts a visible "\n" in the input field.
Is there a solution that at the very least can prevent the cursor jumping back to the start whenever Enter is pressed? Ideally the end user wouldn't see "\n" in the input field at all, but rather a visible new line in the input field that results in a visible new line in the text layer.
I'm running CS6 on OSX but will primarily be using the script on Windows, also CS6.
Thanks to anyone that can help.
Change line 13 for your regex to:
ref.contents = input.text.replace(/\n/g,'\r');
Copy link to clipboard
Copied
Change line 13 for your regex to:
ref.contents = input.text.replace(/\n/g,'\r');
Copy link to clipboard
Copied
Work perfectly! Been going over this for days and never once thought to change the regex. Still got so much to learn! Thanks
Copy link to clipboard
Copied
Yea, I do that too. I'm not too good with regex, and tend to use methods to work around it.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now