3 Replies Latest reply: Mar 31, 2012 4:36 PM by dserodio RSS

    need help to create a text layer using scripting

    michael.shen Community Member


      i'm new in scripting. now i want to use scripting to create a text layer,but i don't know how to set some properties, does anyone can help me.

      please see my code below:


      var myLayer1 = docRef.artLayers.add();

      myLayer1.kind = LayerKind.TEXT;



      myLayer1.textItem.size='26.3203234863281 pt';

      var textColor1 = new SolidColor;

      textColor1.rgb.hexValue = 'FAF9EF';

      myLayer1.textItem.color = textColor1;

      myLayer1.textItem.contents = 'madison';


      docRef.activeLayer.translate(1120.0000000, 649.0000000);



      now i want to set justification , bounds.


      another question is if my text has "\n" how to do?



        • 1. Re: need help to create a text layer using scripting
          JJMack CommunityMVP

          I have played a bit with addinf text layers.  Had a few problems sizeing text Photoshop seem to want to use points and a 72 DPI resolution. I also find it hard to move or align a text layer the has been added as a paragraph in some boundry, Here is some comments and code I have in one of my scripts where i playe around tryinf to do text, note several settinf lines have been commented out


          /* Trying to figure out font size for the number of lines to cover the document height*/
          /* and getting setting text area to cover the document was a trip. Adobe Postscript trip*/
          /* I believe that 72 or 72.27 Point/Pica Size Photoshop Preference maybe I should see if*/
          /* I could retrieve it. Anyway mine is set to 72 Setting the document resolution taking*/
          /* the document width and dividing by 72 would probably yield number of characters that*/
          /* would fit in the document width. Setting the documents resolution comes into play*/
          /* with Photoshop text support. Using the documents height and dividing the by the number*/
          /* of lines of text I needed I hoped would yield the font size I needed. However that
          /* did not work the text area was correct the number of text lines did not fit. I needed*/
          /* to use a smaller font.  When the document resolution is set to 72 DPI and I set a text*/
          /* layer font size to 72 and the text area the number of pixels I want and observing
          /* Photoshop's text options bar there I see a one 1 to one relationship. 72 px = 72 px.*/
          /* If I set the documents resolution lower and set a Photoshop text layer font size to*/
          /* 72 px I see Photoshop scale the number to a lower number of pixels in the option bar.*/
          /* Just what I needed. Setting the Documents resolution to 60 DPI let the number of line*/
          /* I needed fit on the document. However Photoshop also scaled the text area I set down*/
          /* in size and that number of lines did not fit within that area. I needed to scale the
          /* text area up. Scaling the Text area up using 72/resolution did the trick...


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

                              text_layer.name = "EXIF Stamp";                                                                      // 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


          //                    var fontSize = Math.round((doc.height- textY) / ((numExifItems +1) * sizeFactor)); // Calulate font size to use Item nomber + last \r

                              /* Calulate font size to use for StampExit keep size same for landscape and portrait base on document size */

                              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

                              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 for StampEXIF so action can position text layer

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

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



          text_layer.textItem.contents =  "Picture  " + docName + "  Copyright \u00A9 " + Biz + " " + cpyrt

          + "\r" + Artist + phoTime

          + "\r" + Model  + lens + maxF + focLength

          + "\r" + expPgm + expCmp + mtrMode + ev

          + "\r" + wbMode + ISO  + expTime + Fstop + flshMode;




          • 2. Re: need help to create a text layer using scripting
            c.pfaffenbichler Community Member


            myLayer1.textItem.justification = Justification.LEFT;

            or the appropriate property not work?


            And for PointText you could set the coordinates with an Array of two values

            myLayer1.textItem.position = [100, 100];

            • 3. Re: need help to create a text layer using scripting
              dserodio Community Member

              If your text has newlines, you must substitute them for "\r":

              var text = "line 1\nline2";

              text = text.replace("\n", "\r");