5 Replies Latest reply on Mar 6, 2013 3:15 PM by .jake.

    resize a layer to a percentage of the document size and position in corner

    .jake.

      I need to create a script that places a logo in the corner of my main document but i need the logo to be constrained to a percentage of the document size. my logos needs to live in the bottom right corner of my images and be no larger than 28 percent height and no larger than 60 percent of the width of the document. i need it to constrain proportions of the original file so if the logo is square it will remain square, or if it is more rectangular it will maintain its rectangular shape.  Heres what i've come up with so far, i am pretty inept at scripting so if disregarding my current script is more helpful than building upon it please ignore mine:

       

      if ( documents.length > 0 )

      {

          var originalDialogMode = app.displayDialogs;

          app.displayDialogs = DialogModes.ERROR;

          var originalRulerUnits = preferences.rulerUnits;

          preferences.rulerUnits = Units.PIXELS;

         

          try

          {

             

              app.activeDocument = app.documents[0];    //set active document to 2nd document   

              var docRefOne = app.documents[0];

              var refWidth = docRefOne.width;

              var refHeight = docRefOne.height;

             

              app.activeDocument = app.documents[1];    //set active document to 2nd document   

               var docRef = activeDocument;

              //current image dimensions

              var w = docRef.width;

              var h = docRef.height;

            

        

              activeDocument.selection.selectAll();

             

           

              docRef.selection.copy();   //Copy selection

             

              app.activeDocument = app.documents[0]; //set first document as active document

              var docRef = activeDocument;

                    

              activeDocument.selection.selectAll();

             docRef.paste(true); //paste into selection

             

          }

          catch( e )

          {

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

              // to the user

              preferences.rulerUnits = originalRulerUnits;

             app.displayDialogs = originalDialogMode;

              throw e;

          }

       

       

          // Everything went Ok. Restore ruler units

          preferences.rulerUnits = originalRulerUnits;

          app.displayDialogs = originalDialogMode;

      }

        • 1. Re: resize a layer to a percentage of the document size and position in corner
          Paul Riggott Level 6

          You could try this...

           

           

          #target photoshop
          activeDocument.suspendHistory('Logo Resize', 'main()');
          function main(){
          if(!documents.length) return;
          var startRulerUnits = app.preferences.rulerUnits;
          app.preferences.rulerUnits = Units.PIXELS;
          var myDoc = activeDocument;
          /////////////////////////////// Amend to suit /////////////////////////////////
          var Percent = 10; /* Resize logo to percentage of smallest side of doc */
          var Opacity = 100; /* Opacity of logo */
          //Position logo   X,Y
          var X = myDoc.width - 10; //Right side of logo
          var Y = myDoc.height - 10; //Bottom of logo
          var Logo = File(Folder.desktop + "/logo.png"); //logo
          if(!Logo.exists){
              alert("Your logo '" + decodeURI(Logo.name) + "' does not exist!");
              app.preferences.rulerUnits = startRulerUnits;
              return;
              }
          /////////////////////////////////////////////////////////////////////////////////////////////
          placeFile(Logo);
          var LB = myDoc.activeLayer.bounds; 
          var docHeight = myDoc.height;
          var docWidth = myDoc.width;
          var LHeight = Math.abs(LB[3].value) - Math.abs(LB[1].value);
          var LWidth = Math.abs(LB[2].value) - Math.abs(LB[0].value);    
          var percentageHeight = ((docHeight/LWidth)*Percent); 
          var percentageWidth = ((docWidth/LWidth)*Percent);
          if(docWidth < docHeight){
          myDoc.activeLayer.resize(percentageWidth,percentageWidth,AnchorPosition.MIDDLECENTER);
          }else{   
            myDoc.activeLayer.resize(percentageHeight,percentageHeight,AnchorPosition.MIDDLECENTER);
            }
          var LB = myDoc.activeLayer.bounds;
          X = X - LB[2].value;
          Y = Y - LB[3].value;
          activeDocument.activeLayer.translate(X,Y);
          activeDocument.activeLayer.opacity=Opacity;
          app.preferences.rulerUnits = startRulerUnits;
          };
          function placeFile(placeFile) {
          var desc = new ActionDescriptor();
          desc.putPath( charIDToTypeID('null'), new File(placeFile) );
          desc.putEnumerated( charIDToTypeID('FTcs'), charIDToTypeID('QCSt'), charIDToTypeID('Qcsa') );
          var desc2 = new ActionDescriptor();
          desc2.putUnitDouble( charIDToTypeID('Hrzn'), charIDToTypeID('#Pxl'), 0.000000 );
          desc2.putUnitDouble( charIDToTypeID('Vrtc'), charIDToTypeID('#Pxl'), 0.000000 );
          desc.putObject( charIDToTypeID('Ofst'), charIDToTypeID('Ofst'), desc2 );
          try{
          executeAction( charIDToTypeID('Plc '), desc, DialogModes.NO );
          }catch(e){}
          };
          
          

           

          Also Picture Processor supports this functionallity.

          http://www.scriptsrus.talktalk.net/PP.htm

          • 2. Re: resize a layer to a percentage of the document size and position in corner
            .jake. Level 1

            that's definitely getting me in the ballpark of what i was aiming for, i will play around with it for sure and see if it will fulfill my needs.

            • 3. Re: resize a layer to a percentage of the document size and position in corner
              .jake. Level 1

              do you know of a way to incorporate some kind of line in there that does something like

              if logo is wider than tall set the percentage in this line "var Percent = 10; /* Resize logo to percentage of smallest side of doc */" to 60 instead of ten

              but if logo is taller than wide set the percentage of that line to 28

               

              the script looks like it more or less does what i want, the problem is that i have to actually use a variety of logos,  i tested a few of the logos i deal with and found that i could achieve what i want by having two versions of your script one for 60 and one for 28 but it would definitely streamline things if the script was able to decide which to do vs me having to choose which version of the script. i hope that makes sense.

              • 4. Re: resize a layer to a percentage of the document size and position in corner
                Paul Riggott Level 6

                You could do something like this...

                 

                 

                #target photoshop
                activeDocument.suspendHistory('Logo Resize', 'main()');
                function main(){
                if(!documents.length) return;
                var startRulerUnits = app.preferences.rulerUnits;
                app.preferences.rulerUnits = Units.PIXELS;
                var myDoc = activeDocument;
                /////////////////////////////// Amend to suit /////////////////////////////////
                var Opacity = 100; /* Opacity of logo */
                //Position logo   X,Y
                var X = myDoc.width - 10; //Right side of logo
                var Y = myDoc.height - 10; //Bottom of logo
                var Logo = File(Folder.desktop + "/logo.png"); //logo
                if(!Logo.exists){
                    alert("Your logo '" + decodeURI(Logo.name) + "' does not exist!");
                    app.preferences.rulerUnits = startRulerUnits;
                    return;
                    }
                /////////////////////////////////////////////////////////////////////////////////////////////
                placeFile(Logo);
                var LB = myDoc.activeLayer.bounds; 
                var docHeight = myDoc.height;
                var docWidth = myDoc.width;
                var LHeight = Math.abs(LB[3].value) - Math.abs(LB[1].value);
                var LWidth = Math.abs(LB[2].value) - Math.abs(LB[0].value);   
                if(LWidth > LHeight) {//Landscape logo
                var Percent = 60; /* Resize logo to percentage of smallest side of doc */
                }
                if(LWidth < LHeight) {//Portrait logo
                var Percent = 28; /* Resize logo to percentage of smallest side of doc */
                }
                if(LWidth == LHeight) {//Square logo
                var Percent = 15; /* Resize logo to percentage of smallest side of doc */
                }
                var percentageHeight = ((docHeight/LWidth)*Percent); 
                var percentageWidth = ((docWidth/LWidth)*Percent);
                if(docWidth < docHeight){
                myDoc.activeLayer.resize(percentageWidth,percentageWidth,AnchorPosition.MIDDLECENTER);
                }else{   
                  myDoc.activeLayer.resize(percentageHeight,percentageHeight,AnchorPosition.MIDDLECENTER);
                  }
                var LB = myDoc.activeLayer.bounds;
                X = X - LB[2].value;
                Y = Y - LB[3].value;
                activeDocument.activeLayer.translate(X,Y);
                activeDocument.activeLayer.opacity=Opacity;
                app.preferences.rulerUnits = startRulerUnits;
                };
                function placeFile(placeFile) {
                var desc = new ActionDescriptor();
                desc.putPath( charIDToTypeID('null'), new File(placeFile) );
                desc.putEnumerated( charIDToTypeID('FTcs'), charIDToTypeID('QCSt'), charIDToTypeID('Qcsa') );
                var desc2 = new ActionDescriptor();
                desc2.putUnitDouble( charIDToTypeID('Hrzn'), charIDToTypeID('#Pxl'), 0.000000 );
                desc2.putUnitDouble( charIDToTypeID('Vrtc'), charIDToTypeID('#Pxl'), 0.000000 );
                desc.putObject( charIDToTypeID('Ofst'), charIDToTypeID('Ofst'), desc2 );
                try{
                executeAction( charIDToTypeID('Plc '), desc, DialogModes.NO );
                }catch(e){}
                };
                
                
                • 5. Re: resize a layer to a percentage of the document size and position in corner
                  .jake. Level 1

                  That was precisely the ticket, thank you very much.