4 Replies Latest reply on Oct 17, 2015 10:49 PM by jameel flex

    Scaling text bound through photoshop scripting

    jameel flex

      Hi,

       

      I am trying to scale the text bounding box along with the text inside.

       

      I want to achieve the same effect as select the text box by double clicking on text->press cntrl->then resize the bounding box by mouse

       

      Here is my code to generate text.

       

      crs is an object with scaleX, scleY, width, height properties. I want to apply the scaling to the text.

       

      function addText(crs)

      {

        if ( documents.length > 0 )

        {

        var originalDialogMode = app.displayDialogs;

        app.displayDialogs = DialogModes.ERROR;

        var originalRulerUnits = preferences.rulerUnits;

        preferences.rulerUnits = Units.PIXELS;

       

        try

        {

        var docRef = activeDocument;

       

        // Now create a text layer at the front

        var myLayerRef = docRef.artLayers.add();

        myLayerRef.kind = LayerKind.TEXT;

        myLayerRef.name = "Text Description";

       

        var myTextRef = myLayerRef.textItem;

       

       

       

        myTextRef.contents = crs.lableName;

       

        // off set the text to be in the middle

        myTextRef.position = new Array( crs.xPos, crs.yPos);

        myTextRef.size = 20;

       

        var internalFontName = getInternalFontName(crs.fontFamily, crs.fontType);

        if(internalFontName != null)

        myTextRef.font = internalFontName;

       

       

        var R = hexToR("#"+crs.fontColor);

        var G = hexToG("#"+crs.fontColor);

        var B = hexToB("#"+crs.fontColor);

       

        //alert(crs.fontColor + " " + R + " " + G + " " + B);

        function hexToR(h) {return parseInt((cutHex(h)).substring(0,2),16)}

        function hexToG(h) {return parseInt((cutHex(h)).substring(2,4),16)}

        function hexToB(h) {return parseInt((cutHex(h)).substring(4,6),16)}

        function cutHex(h) {return (h.charAt(0)=="#") ? h.substring(1,7):h}

        if(isNaN(R) == false && isNaN(G)==false && isNaN(B)==false)

        {

        textColor = new SolidColor();

        textColor.rgb.red = R;

        textColor.rgb.green = G;

        textColor.rgb.blue = B;

        myTextRef.color = textColor;

        }

       

       

        if(crs.isFontUnderline == "true")

        myTextRef.underline = UnderlineType.UNDERLINELEFT;

       

        if(crs.isStrikeThru == "true")

        myTextRef.strikeThru = StrikeThruType.STRIKEHEIGHT;

       

        if(crs.alignLeft == "true")

        myTextRef.justification = Justification.LEFT;

        else if(crs.alignCenter == "true")

        myTextRef.justification = Justification.CENTER;

        else if(crs.alignRight == "true")

        myTextRef.justification = Justification.RIGHT;

       

        }

        catch( e )

        {

        // An error occurred. Restore ruler units, then propagate the error back

        // to the user

        alert("some error");

        preferences.rulerUnits = originalRulerUnits;

               app.displayDialogs = originalDialogMode;

        throw e;

        }

       

       

        // Everything went Ok. Restore ruler units

        preferences.rulerUnits = originalRulerUnits;

        app.displayDialogs = originalDialogMode;

        }

        else

        {

        alert( "You must have a document open to add the filename!" );

        }

       

        var idsetd = charIDToTypeID( "setd" );

           var desc153 = new ActionDescriptor();

           var idnull = charIDToTypeID( "null" );

               var ref62 = new ActionReference();

               var idPrpr = charIDToTypeID( "Prpr" );

               var idTxtS = charIDToTypeID( "TxtS" );

               ref62.putProperty( idPrpr, idTxtS );

               var idTxLr = charIDToTypeID( "TxLr" );

               var idOrdn = charIDToTypeID( "Ordn" );

               var idTrgt = charIDToTypeID( "Trgt" );

               ref62.putEnumerated( idTxLr, idOrdn, idTrgt );

           desc153.putReference( idnull, ref62 );

           var idT = charIDToTypeID( "T   " );

               var desc154 = new ActionDescriptor();

               var idSz = charIDToTypeID( "Sz  " );

               var idPnt = charIDToTypeID( "#Pnt" );

               desc154.putUnitDouble( idSz, idPnt, crs.fontSize );

           var idTxtS = charIDToTypeID( "TxtS" );

           desc153.putObject( idT, idTxtS, desc154 );

        executeAction( idsetd, desc153, DialogModes.NO );

       

       

      }

       

      please help.

      Thanks in advance.