4 Replies Latest reply on Sep 17, 2015 12:52 AM by neepie21

    My fist script - Any serious errors?

    neepie21

      Hello,

      here is my first feeble attempt of scripting. I'm trying to change resolution to 300, then resize image and lastly resize again if print width/height is too large.

       

      docRef = app.activeDocument;
      
      var startDisplayDialogs = app.displayDialogs;
      var startRulerUnits = app.preferences.rulerUnits;
      var startTypeUnits = app.preferences.typeUnits;
      
      displayDialogs = DialogModes.NO;
      preferences.rulerUnits = Units.PIXELS;
      preferences.typeUnits = TypeUnits.PIXELS; 
      
      if (parseFloat(app.activeDocument.width) >= parseFloat(app.activeDocument.height)) 
      {
              //check if image is square (or close enough)
              if (parseFloat(app.activeDocument.height) >= parseFloat(app.activeDocument.width) * 0.9)
              {
                  var square = true;
              }
              else
              {
                  var square = false;
              }
      }
      else
      {      
              if (parseFloat(app.activeDocument.width) >= parseFloat(app.activeDocument.height) * 0.9)
              {
                  var square = true;
              }
              else
              {
                  var square = false;
              }    
      } 
      
      if (parseFloat(app.activeDocument.height) >= parseFloat(app.activeDocument.width))
      { 
          //change resolution
          docRef.resizeImage(undefined, undefined, 300, ResampleMethod.NONE);  
          
          //resize image
          docRef.resizeImage(undefined, 3543, undefined, ResampleMethod.BICUBIC);
          
          var printWidth = parseFloat(app.activeDocument.width) / app.activeDocument.resolution;
      
          //resize if print width is longer than 8.35 inch and image isn't square
          if (printWidth > 8.35 && square == false)
          {
              docRef.resizeImage(2505, undefined, undefined, ResampleMethod.BICUBIC);    
          }
      }    
      else
      {
          docRef.resizeImage(undefined, undefined, 300, ResampleMethod.NONE);  
      
          docRef.resizeImage(3543, undefined, undefined, ResampleMethod.BICUBIC);
      
          var printHeight = parseFloat(app.activeDocument.height) / app.activeDocument.resolution;
      
          if (printHeight > 8.35 && square == false)
          {            
              docRef.resizeImage(undefined, 2505, undefined, ResampleMethod.BICUBIC);
          }
       }
      
      //save changes and close image
      app.activeDocument.close(SaveOptions.SAVECHANGES); 
      
      app.preferences.rulerUnits = startRulerUnits;
      app.preferences.typeUnits = startTypeUnits;
      app.displayDialogs = startDisplayDialogs; 
      

       

      Any sense at all?

      Any serious errors?

       

      thanks

        • 1. Re: My fist script - Any serious errors?
          JJMack Most Valuable Participant

          I would think that if you had a max print size in mind all the would be needed would be a single resample resize like the"Fit Image" plugin script does.

           

          That  if you wanted a max print size toe be a 8"x10' 300 Image  That you would rotate the image to match the portrait orientation. Then fit the image to a pixel area 2400px by 3000px and change the resolution to 300 so the max the print size  would be is 8:x10 if the image has a 4:5 aspect ratio. The 300DPI setting may be done with a resize method but there is no resampling for me that just changing the pixel size the number of pixels do not change.  Fit Image also has a do not resize up option so you can do what you want with a a conditional action that would  rotate landscape to portrait. All image would be fitted for the 2400x3000px print area and the resolution changed to 300dpi. Small image would fit and not be resized at all.

           

          What I do not like about your script is that there is a possibility that an image is resampled twice.  Every time you resample an image you loose some image quality.  But then I may be nit picking.  Also if its a smart object layer it would not mattter it you resample it many times for you are alwasys resaping the original object rendered pixels.

          1 person found this helpful
          • 2. Re: My fist script - Any serious errors?
            Chuck Uebele Adobe Community Professional & MVP

            Just a few points. You forgot "var" in line one. You defined the active document, but then don't use the variable. instead you write out the long way. for comparing width & height of a doc, you don't need to parse the info. Plus files are always even pixels, so you do parse it, it should be parseInt. I agree with JJ about resizing. I'm not sure why you set the resolution in one step and don't resample then resample in the next. Just combine the two.

            • 3. Re: My fist script - Any serious errors?
              Chuck Uebele Adobe Community Professional & MVP

              See if this works. I didn't test it. I might have got the less than signs in lines 18 & 24 backwards if it doesn't work right.

               

              #target photoshop
              
              var docRef = activeDocument;//declare your variable and then use it
              
              var startDisplayDialogs = app.displayDialogs;
              var startRulerUnits = app.preferences.rulerUnits;
              var startTypeUnits = app.preferences.typeUnits;//don't really need this for this script.
              
              displayDialogs = DialogModes.NO;
              preferences.rulerUnits = Units.PIXELS;
              preferences.typeUnits = TypeUnits.PIXELS;//Dont need this either.
              
              //You want to see if the longest side is within .9 of the short side. Use Math.mas and Math.min. to cut the need
              //need for more lines and convoluted logic down to a min.
              if(Math.min(docRef.width,docRef.height)>Math.max(docRef.width,docRef.height) * .9){var square = true}}
              else{var square = false};
              
              //Rather than resize several times, you just want to see if once you resize one side, the other is too long.
              //Use a ratio to compare what you have with what you want. Then resize once and add the dpi resolution in at that point.
              if(square){docRef.resizeImage(undefined, 3543, 300, ResampleMethod.BICUBIC)}
              else if(docRef.height>docRef.width){
                  if(docRef.height/docRef.width < 3543/2505){
                      docRef.resizeImage(2505, undefined, 300, ResampleMethod.BICUBIC);
                  else{
                      docRef.resizeImage(undefined, 3543, 300, ResampleMethod.BICUBIC);
                      };
              else{
                  if(docRef.width/docRef.height < 3543/2505){
                      docRef.resizeImage(undefined, 2505, 300, ResampleMethod.BICUBIC);
                  else{
                      docRef.resizeImage(3543, undefined, 300, ResampleMethod.BICUBIC);
                      };  
                  };
              
              //save changes and close image
              docRef.close(SaveOptions.SAVECHANGES);//I don't like saving over files in scripts.
              //You can mess up a lot of files very quickly doing this.
              //Best to save the files to another directory or different name.
              
              app.preferences.rulerUnits = startRulerUnits;
              app.preferences.typeUnits = startTypeUnits;
              app.displayDialogs = startDisplayDialogs;
              
              
              • 4. Re: My fist script - Any serious errors?
                neepie21 Level 1

                Thank you all.