3 Replies Latest reply on Sep 4, 2016 9:58 AM by Chuck Uebele

    Issues creating multiline text layers from multiline edittext fields

    jonnyr84832999

      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.