4 Replies Latest reply on Mar 18, 2010 1:14 AM by afrobeater

    Scripting challenge - help please

    afrobeater

      Hi All, I hope someone out there can help me with this. I want to run a batch of photographs through PS(CS4) using the process described below. If anyone knows how I might automate this, or if there's such a  script already knocking about, please let me know - doing it manually  would take an age!


      1. Open file (which would be less than 300dpi).
      2. Convert to 300dpi.
      3. Capture the image dimensions for print at  300dpi (wd x ht in cm)
      4. Save off the file and include the image dimensions in the filename.

       

      By way of example:

       

      The original file is 72dpi and named:

      photofile01.jpg

       

      The output file is converted up to 300dpi and is saved with the dimensions in the filename, something like:

      photofile01_40x30cm.jpg

       

      (wash, rinse, repeat...)


      I am not familiar with scripting in PS and cannot see how to get around the bit about capturing the dimensions and  renaming the file.

      Any solutions greatly appreciated

       

      Paul

        • 1. Re: Scripting challenge - help please
          Paul Riggott Level 6

          Please give this a try...

          main();
          function main(){
          var inputFolder = Folder.selectDialog ("Please select folder to process");
          if(inputFolder == null) return;
          var outputFolder = Folder.selectDialog ("Select ouput folder");
          if(outputFolder == null) return;
          var fileList = inputFolder.getFiles("*.jpg");
          for (var a in fileList){
              open(fileList[a]);
          var doc = activeDocument;
          if (activeDocument.resolution != 300){
              doc.resizeImage(undefined, undefined, 300, ResampleMethod.NONE);
              }
          var Width = doc.width.as('cm').toFixed(2).toString().replace(/\./g,'-');
          var Height = doc.height.as('cm').toFixed(2).toString().replace(/\./g,'-');
          var suffix = Width +"x"+Height+"cm";
          var Name = app.activeDocument.name.replace(/\.[^\.]+$/, '');
          var saveFile = File(outputFolder +"/"+Name+"_"+suffix+".jpg");
          SaveJPEG(saveFile,8);
          app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
          }
          }
          function SaveJPEG(saveFile, jpegQuality){
          jpgSaveOptions = new JPEGSaveOptions();
          jpgSaveOptions.embedColorProfile = true;
          jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
          jpgSaveOptions.matte = MatteType.NONE;
          jpgSaveOptions.quality = jpegQuality;
          activeDocument.saveAs(saveFile, jpgSaveOptions, true,Extension.LOWERCASE);
          }
          
          • 2. Re: Scripting challenge - help please
            afrobeater Level 1

            Paul - this is amazing! You seem to have captured everything. I've never seen a PS script before, but this is an excellent introduction to how powerful they can be. FIrst of all, thank you so much. Secondly, I wonder if you could help me tweak the output a little? Currently, using your script, the file goes in as say:

             

            photofile01.jpg

             

            and (brilliantly converted as per spec) comes out as:

            photofile01_10-84x7-51cm.jpg

             

            what seems to be happening is that the dimensions are captured to 2 decimal places, separated by a -

             

            Is there a way to  round off the dimension to the whole number, so the above would come out without decimals as:

            photofile01_11x8cm.jpg

             

            That would be awesome and many thanks in advance if you can help me with my learning on this.

             

            Paul

            • 3. Re: Scripting challenge - help please
              Paul Riggott Level 6

              No problem Paul, here you are...

              main();
              function main(){
              var inputFolder = Folder.selectDialog ("Please select folder to process");
              if(inputFolder == null) return;
              var outputFolder = Folder.selectDialog ("Select ouput folder");
              if(outputFolder == null) return;
              var fileList = inputFolder.getFiles("*.jpg");
              for (var a in fileList){
                  open(fileList[a]);
              var doc = activeDocument;
              if (activeDocument.resolution != 300){
                  doc.resizeImage(undefined, undefined, 300, ResampleMethod.NONE);
                  }
              var Width = Math.round(doc.width.as('cm')).toString();
              var Height = Math.round(doc.height.as('cm')).toString();
              var suffix = Width +"x"+Height+"cm";
              var Name = app.activeDocument.name.replace(/\.[^\.]+$/, '');
              var saveFile = File(outputFolder +"/"+Name+"_"+suffix+".jpg");
              SaveJPEG(saveFile,8);
              app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
              }
              }
              function SaveJPEG(saveFile, jpegQuality){
              jpgSaveOptions = new JPEGSaveOptions();
              jpgSaveOptions.embedColorProfile = true;
              jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
              jpgSaveOptions.matte = MatteType.NONE;
              jpgSaveOptions.quality = jpegQuality;
              activeDocument.saveAs(saveFile, jpgSaveOptions, true,Extension.LOWERCASE);
              }
              
              1 person found this helpful
              • 4. Re: Scripting challenge - help please
                afrobeater Level 1

                Terrific - in fact I looked at your original code and made a clumsy workaround by changing:

                 

                var Width = doc.width.as('cm').toFixed(2).toString().replace(/\./g,'-');
                var Height = doc.height.as('cm').toFixed(2).toString().replace(/\./g,'-');

                 

                to

                 

                var Width = doc.width.as('cm').toFixed(0).toString().replace(/\./g,'-');
                var Height = doc.height.as('cm').toFixed(0).toString().replace(/\./g,'-');

                 

                and that worked, but your solution is far more elegant.

                 

                Thanks Paul, you have me thinking about PS in a whole new way now!

                 

                P