4 Replies Latest reply on Jul 2, 2012 5:29 PM by Janice_Terron

    Scripting Fit Image Command


      Does anyone know if there is a way to add the "Fit Image" command to a script?

      This is what I currently am working with below. I just want to be able to make it so that images are no longer than 1000 pixels on either side, and not have to call an action.







      var idPlc = charIDToTypeID( "Plc " );

          var desc973 = new ActionDescriptor();

          var idnull = charIDToTypeID( "null" );

          desc973.putPath( idnull, new File( "C:/watermark.gif\/" ) );

          var idFTcs = charIDToTypeID( "FTcs" );

          var idQCSt = charIDToTypeID( "QCSt" );

          var idQcsa = charIDToTypeID( "Qcsa" );

          desc973.putEnumerated( idFTcs, idQCSt, idQcsa );

          var idOfst = charIDToTypeID( "Ofst" );

              var desc974 = new ActionDescriptor();

              var idHrzn = charIDToTypeID( "Hrzn" );

              var idPxl = charIDToTypeID( "#Pxl" );

              desc974.putUnitDouble( idHrzn, idPxl, 0.000000 );

              var idVrtc = charIDToTypeID( "Vrtc" );

              var idPxl = charIDToTypeID( "#Pxl" );

              desc974.putUnitDouble( idVrtc, idPxl, 0.000000 );

          var idOfst = charIDToTypeID( "Ofst" );

          desc973.putObject( idOfst, idOfst, desc974 );

      executeAction( idPlc, desc973, DialogModes.NO );




        • 1. Re: Scripting Fit Image Command
          c.pfaffenbichler Level 9

          You could try this:


          // make document square with the document’s longer side’s length;

          // 2012, use it at your own risk;

          #target photoshop

          if (app.documents.length > 0) {

          var myDocument = app.activeDocument;

          var originalRulerUnits = preferences.rulerUnits;

          preferences.rulerUnits = Units.PIXELS;

          // get longer side;

          var theFactor = 1000 / getTheLonger(Number(myDocument.width), Number(myDocument.height));

          // resize;

          myDocument.resizeImage (myDocument.width * theFactor, myDocument.height * theFactor, myDocument.resolution, ResampleMethod.BICUBIC);

          // reset;

          preferences.rulerUnits = originalRulerUnits;


          else {};

          function getTheLonger(a,b) {

                    if (a >= b) {

                              var x = a}

                    else {

                              var x = b};

                    return x;


          • 2. Re: Scripting Fit Image Command
            JJMack Most Valuable Participant

            You can also look at the Image Processor script to see how it actually uses the Plug-in script "Fit Image.jsx"..   It woul look somethimg like this


            FitImage( 1000, 1000 );



            //Code below from "Image Processor.jsx"


            // use the fit image automation plug-in to do this work for me

            function FitImage( inWidth, inHeight ) {

                      if ( inWidth == undefined || inHeight == undefined ) {

                                alert( strWidthAndHeight );   //This statement woul need to changed



                      var desc = new ActionDescriptor();

                      var unitPixels = charIDToTypeID( '#Pxl' );

                      desc.putUnitDouble( charIDToTypeID( 'Wdth' ), unitPixels, inWidth );

                      desc.putUnitDouble( charIDToTypeID( 'Hght' ), unitPixels, inHeight );

                      var runtimeEventID = stringIDToTypeID( "3caa3434-cb67-11d1-bc43-0060b0a13dc4" );

                      executeAction( runtimeEventID, desc, DialogModes.NO );



            The image process set vars for localization


                      // the string that need localized

                      strWidthAndHeight = localize( "$$$/JavaScripts/ImageProcessor/WidthAndHeight=Width and Height must be defined to use FitImage function!" );1

            • 3. Re: Scripting Fit Image Command
              Michael L Hale Level 5

              FIt Image itself is a script. You could have a look at it and copy the parts you need into your script. Or because that script is set up to work with actons you could use it as the Image Processor script does as JJMack suggested. Or as it looks like you are already using the scriptlistener plugin you could just do a FIt Image in the GUI and copy out the last entry in the log.

              • 4. Re: Scripting Fit Image Command
                Janice_Terron Level 1

                Thanks everyone!

                The script from c.pfaffenbichler works.. and thanks again for all of the other advice.