Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Issues creating multiline text layers from multiline edittext fields

New Here ,
Sep 03, 2016 Sep 03, 2016

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:

Screen Shot 2016-09-04 at 01.42.38.png

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.

TOPICS
Actions and scripting
1.2K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Sep 03, 2016 Sep 03, 2016

Change line 13 for your regex to:

  ref.contents = input.text.replace(/\n/g,'\r'); 

Translate
Adobe
Community Expert ,
Sep 03, 2016 Sep 03, 2016

Change line 13 for your regex to:

  ref.contents = input.text.replace(/\n/g,'\r'); 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 04, 2016 Sep 04, 2016

Work perfectly! Been going over this for days and never once thought to change the regex. Still got so much to learn! Thanks

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 04, 2016 Sep 04, 2016
LATEST

Yea, I do that too. I'm not too good with regex, and tend to use methods to work around it.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines