4 Replies Latest reply on May 30, 2012 9:49 PM by michael.shen

    how to create a marquee selection and  then write some texts inside

    michael.shen Level 1

      hi,guys.

       

      Does scripts can do the following things?

       

      1. create a marquee selection and  then write some texts inside

       

      2. align vertically center the text in the marquee selection

       

       

      Thanks,

      Michael

        • 1. Re: how to create a marquee selection and  then write some texts inside
          Paul Riggott Level 6

          He is an example...

           

          
          

          var startRulerUnits = app.preferences.rulerUnits;

          app.preferences.rulerUnits = Units.PIXELS;

          var FontName = "Georgia";

          var FontSize = 14;

          var black = new SolidColor();

          black.rgb.hexValue = '000000';

          //create a new document  @ 72 PPI

          app.documents.add(600,400,72,"Justification Example");

          var SB = new Array();

          SB[0] = 20; //Left

          SB[1] = 20; //Top

          SB[2] = 300; //Right

          SB[3] = 200; //Bottom

          //make a selection

          activeDocument.selection.select([[SB[0],SB[1]], [SB[2],SB[1]], [SB[2],SB[3]], [SB[0], SB[3]]], SelectionType.REPLACE, 0, false);

          //Stroke selection so you can see the results

          activeDocument.selection.stroke (black, 1, StrokeLocation.INSIDE, ColorBlendMode.NORMAL, 100, false);

          //new text layer

          var newTextLayer = activeDocument.artLayers.add();

          newTextLayer.kind = LayerKind.TEXT;

          newTextLayer.textItem.kind = TextType.POINTTEXT

          newTextLayer.textItem.color = black;

          newTextLayer.textItem.font = FontName;

          newTextLayer.textItem.size = FontSize;

          newTextLayer.textItem.contents = "Centered Marquee Text";

          var X = SB[0] + 5;  //Five pixels to the right of the marquee selection

          var Y = ((SB[3]-SB[1])/2) + SB[0]; //Find the middle of the marquee selection and add the pixels top of selection to top of document.

          //Position text

          newTextLayer.textItem.position = Array(X, Y);

          //Convert the document to 300 PPI

          activeDocument.resizeImage(undefined, undefined, 300, ResampleMethod.NONE);

          app.preferences.rulerUnits = startRulerUnits;

          • 2. Re: how to create a marquee selection and  then write some texts inside
            Paul Riggott Level 6

            This is another example to fit the text to the selection....

            It will create two documents...

             

            
            

            createExample("Example Text");
            createExample("Example Text and this fits too");

            function createExample(text){
            var startRulerUnits = app.preferences.rulerUnits;
            app.preferences.rulerUnits = Units.PIXELS;
            var FontName = "Georgia";
            var FontSize = 4;
            var black = new SolidColor();
            black.rgb.hexValue = '000000';
            //create a new document  @ 72 PPI
            app.documents.add(600,400,72,"Justification Example");
            var SB = new Array();
            SB[0] = 20; //Left
            SB[1] = 20; //Top
            SB[2] = 300; //Right
            SB[3] = 200; //Bottom
            //make a selection
            activeDocument.selection.select([[SB[0],SB[1]], [SB[2],SB[1]], [SB[2],SB[3]], [SB[0], SB[3]]], SelectionType.REPLACE, 0, false);
            //Stroke selection so you can see the results
            activeDocument.selection.stroke (black, 1, StrokeLocation.INSIDE, ColorBlendMode.NORMAL, 100, false);
            //new text layer
            var newTextLayer = activeDocument.artLayers.add();
            newTextLayer.kind = LayerKind.TEXT;
            newTextLayer.textItem.kind = TextType.POINTTEXT;
            newTextLayer.textItem.color = black;
            newTextLayer.textItem.font = FontName;
            newTextLayer.textItem.size = FontSize;
            newTextLayer.textItem.contents = text;
            var LB = activeDocument.activeLayer.bounds;
            //create text 6 pixels less than width of selection
            var selWidth = (SB[2]-SB[0]) - 6; //create text
            var LWidth = Math.abs(LB[2].value) - Math.abs(LB[0].value);   
            var percentageWidth = ((selWidth/LWidth)* 100);
            activeDocument.activeLayer.resize(percentageWidth,percentageWidth,AnchorPosition.MIDDLECENTER);
            var X = SB[0] + 3;  //Three pixels to the right of the marquee selection
            var Y = ((SB[3]-SB[1])/2) + (SB[0]+ (LB[3]-LB[1])/2); //Find the middle of the marquee selection and add the pixels top of selection to top of document.
            //Position text
            newTextLayer.textItem.position = Array(X, Y);
            //Convert the document to 300 PPI
            activeDocument.resizeImage(undefined, undefined, 300, ResampleMethod.NONE);
            app.preferences.rulerUnits = startRulerUnits;
            }

            1 person found this helpful
            • 3. Re: how to create a marquee selection and  then write some texts inside
              michael.shen Level 1

              Thank you.

               

              It works greate.

               

              But if want to align vertically center the text in the selection,  the Y coordinate is incorrect. It seems impossible to get the correct coordinate.  Because textItem.position is the  orign for the text , Is it right?

              • 4. Re: how to create a marquee selection and  then write some texts inside
                michael.shen Level 1

                Thanks i have solved .

                 

                I moved two times to align vertically center.

                 

                var LB = newTextLayer.bounds;

                 

                var detaY = (SB[3]-LB[3].value-LB[1].value+SB[1])/2;

                 

                 

                newTextLayer.textItem.position = Array(X, Y+detaY);