2 Replies Latest reply on Apr 12, 2017 4:14 AM by Akash Sharma

    I have a great working 'reduce size' .jsx script. how to add a cropping aspect to it?

    dannyc_TP Level 1

      Hi there

       

      Very new here and hopefully some interesting answers will come about.

       

      I have a complex .jsx script that i have inherited. It works perfectly. It creates folders and reduces my tiff file to the required sizes, puts the reduced sizes into their respective folders then closes. Very brief explanation even though there is a lot going on behind the scenes.

       

      Everything works.

       

      My problem now is that i need to add to the script a cropping functionality.

       

      I need my script to do all the reduction it currently does and then to crop the image by a given amount and put the file into its designated are then close. (so it needs to do exactly the same thing that it does 8 times but just do it once for the cropping task)

       

      I would love some help if its available as i am over my head here

       

      here is the script

      __________________________________________________________________________________________ __________________________________

       

      // MODIFICATION NOTES:

      // - - - - - - - - - -

      // Modified on 25 February by K van Reenen - changed addtional image size from 170 x 170px to 230 x 230px

       

      // This script prompts the user for an input directory, which contains JPEG source images.

      // The images are loaded, their sizes transformed, and copies of the new images stored in a new directory structure.

       

      // Constants

      var FILE_TYPE = ".tif"; // The type of files that this script works on -- you can change

      var SEARCH_MASK = "*" + FILE_TYPE; // Image file filter to find only those files

       

      // Save current dialog preferences

      var startDisplayDialogs = app.displayDialogs;

      var startRulerUnits = app.preferences.rulerUnits;

       

      // Don't display dialogs, and set the units used to be pixels

      app.displayDialogs = DialogModes.NO;

      app.preferences.rulerUnits = Units.PIXELS;

       

      // Setup the array of folder names

      var folders = [

      { 'name' : 'zoom',

      'width' : 1500,

      'height' : 1500

      },

      { 'name' : 'thumb',

      'width' : 90,

      'height' : 90

      },

      { 'name' : 'product',

      'width' : 480,

      'height' : 480

      },

      { 'name' : 'optional',

      'width' : 60,

      'height' : 60

      },

      { 'name' : 'medium',

      'width' : 116,

      'height' : 116

      },

      { 'name' : 'checkout_thumb',

      'width' : 70,

      'height' : 70

      },

      { 'name' : 'additional',

      'width' : 230,

      'height' : 230

      },

      { 'name' : 'marketing',

      'width' : 2000,

      'height' : 2000

      },

      { 'name' : 'sliver',

      'width' : 20,

      'height' : 20

      }

      ];

       

       

      function compare(a,b) {

        if (a.height*a.width < b.height*b.width)

           return 1;

        if (a.height*a.width > b.height*b.width)

          return -1;

        return 0;

      }

       

      folders.sort(compare);

       

      try {

      // Ask user for input folder

      var inputFolder = Folder.selectDialog("Select a folder to process");

      if (inputFolder == null)

      throw X_NOINPUT;

       

      // Make the selected folder current

      Folder.current = inputFolder;

       

          // get all files in the input folder

      var fileList = inputFolder.getFiles(SEARCH_MASK);

       

      // Open each file in turn

      for( var i = 0; i < fileList.length; i++ ){

          // Only want to open non-hidden files (and no folders)

      if ((fileList[i] instanceof File) && (fileList[i].hidden == false)){

      // Get the file's name

      var name_xyz = fileList[i].name.toLowerCase( );

      // Check if the name is suffixed with "_v1" or "_v2" -> some images received early on were, but we want to remove it

      if (name_xyz.indexOf( "_v1" ) > -1){

      name_xyz = name_xyz.substring( 0, name_xyz.indexOf( "_v1" ) );

      }else if (name_xyz.indexOf( "_v2" ) > -1){

      name_xyz = name_xyz.substring( 0, name_xyz.indexOf( "_v2" ) );

      }else{

      name_xyz = name_xyz.substring( 0, name_xyz.indexOf( FILE_TYPE ) );

      }

       

      // All file names should specify a suffix, which we want to further strip away to generate the top-level folder name

      var folder_name = ""

      if(name_xyz.indexOf('_') >= 0) folder_name = name_xyz.toUpperCase().substring( 0, name_xyz.indexOf( "_" ) );

      else folder_name = name_xyz.toUpperCase();

       

      // Create a folder to contain the outputs

      var newFolder = new Folder( folder_name );

       

      // If the folder doesn't exist, create it

      if (!newFolder.exists){ newFolder.create( ); }

      if (!newFolder.exists){

      // Warn

      alert( "Could not create folder " + newFolder.toString( ) + "; cannot proceed without." );

      }else{

      // Loop through the sizes

      var index = 0, imgCount = folders.length - 1;

      if(name_xyz.substring(11,13) == "_9"){

      index = 8;

      imgCount = 9;

      name_xyz = name_xyz.replace('_9','')

      }

       

      for( var j = index; j < imgCount; j++ ){

      // Create the subfolder (if it doesn't already)

      var subFolder = new Folder( newFolder.toString( ) + "/" + folders[j].name );

       

      if (!subFolder.exists){ subFolder.create( ); }

      if (!subFolder.exists){

      // Warn

      alert( "Could not create folder " + subFolder.toString( ) + "; cannot proceed without." );

      }else{

      // Open the file in Photoshop

      var docRef = open(fileList[i]);

       

      if (docRef == null){

      // Warn

      alert( "Failed to (properly) open " + name + "; can't do much more with/about it." );

      }else{

      // Resize the image

      docRef.resizeImage( folders[j].width, folders[j].height );

      docRef.flatten();

      var dupe = docRef.activeLayer.duplicate();

      dupe.applyUnSharpMask(40,1,0); // UNSHARP MASK HERE

       

      // Export for web

      var outputFile = new File( subFolder.toString( ) + "/" + name_xyz.toUpperCase() + '.jpg' );

       

      if(folders[j].name == "marketing"){

      if(name_xyz.indexOf('_') == -1){

      if (docRef.bitsPerChannel != BitsPerChannelType.EIGHT) docRef.bitsPerChannel = BitsPerChannelType.EIGHT;

      var jpgSaveOptions = new JPEGSaveOptions();

      jpgSaveOptions.embedColorProfile = true;

      jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;

      jpgSaveOptions.matte = MatteType.NONE;

      jpgSaveOptions.quality = 12;

      docRef.saveAs(outputFile, jpgSaveOptions, true,Extension.UPPERCASE);

      }

      } else{

      var exportOptionsSaveForWeb = new ExportOptionsSaveForWeb( );

       

      exportOptionsSaveForWeb.format = SaveDocumentType.JPEG;

      exportOptionsSaveForWeb.optimized = false;

      exportOptionsSaveForWeb.quality = 60;

      exportOptionsSaveForWeb.interlaced = true;

      exportOptionsSaveForWeb.includeProfile = false;

      exportOptionsSaveForWeb.blur = 0;

      docRef.exportDocument( outputFile, ExportType.SAVEFORWEB, exportOptionsSaveForWeb );

      }

      dupe.remove();

      }

      }

      }

      // Close the Photoshop file

      docRef.close(SaveOptions.DONOTSAVECHANGES);

      }

      }

          }

      }

       

      catch (exception) {

       

      // Show degbug message and then bail out

      alert(exception);

      }

       

      finally {

       

      // Restore application preferences

      app.displayDialogs = startDisplayDialogs;

      app.preferences.rulerUnits = startRulerUnits;

      }

       

      __________________________________________________________________________________________ _____________________________________

       

      As i said. the script works and is complete for what it does. i just need to add the cropping aspect to it.

       

      If anyone can help please get in touch

       

      Many thanks

      Danny

      // MODIFICATION NOTES:// - - - - - - - - - -// Modified on 25 February by K van Reenen - changed addtional image size from 170 x 170px to 230 x 230px
      // This script prompts the user for an input directory, which contains JPEG source images.// The images are loaded, their sizes transformed, and copies of the new images stored in a new directory structure.
      // Constantsvar FILE_TYPE = ".tif"; // The type of files that this script works on -- you can changevar SEARCH_MASK = "*" + FILE_TYPE; // Image file filter to find only those files
      // Save current dialog preferencesvar startDisplayDialogs = app.displayDialogs;var startRulerUnits = app.preferences.rulerUnits;
      // Don't display dialogs, and set the units used to be pixelsapp.displayDialogs = DialogModes.NO;app.preferences.rulerUnits = Units.PIXELS;
      // Setup the array of folder namesvar folders = [ { 'name' : 'zoom', 'width' : 1500, 'height' : 1500 }, { 'name' : 'thumb', 'width' : 90, 'height' : 90 }, { 'name' : 'product', 'width' : 480, 'height' : 480 }, { 'name' : 'optional', 'width' : 60, 'height' : 60 }, { 'name' : 'medium', 'width' : 116, 'height' : 116 }, { 'name' : 'checkout_thumb', 'width' : 70, 'height' : 70 }, { 'name' : 'additional', 'width' : 230, 'height' : 230 }, { 'name' : 'marketing', 'width' : 2000, 'height' : 2000 }, { 'name' : 'sliver', 'width' : 20, 'height' : 20 }];

      function compare(a,b) {  if (a.height*a.width < b.height*b.width)     return 1;  if (a.height*a.width > b.height*b.width)    return -1;  return 0;}
      folders.sort(compare);




      try { // Ask user for input folder var inputFolder = Folder.selectDialog("Select a folder to process"); if (inputFolder == null) throw X_NOINPUT; // Make the selected folder current Folder.current = inputFolder;     // get all files in the input folder var fileList = inputFolder.getFiles(SEARCH_MASK);
      // Open each file in turn for( var i = 0; i < fileList.length; i++ ){     // Only want to open non-hidden files (and no folders) if ((fileList[i] instanceof File) && (fileList[i].hidden == false)){ // Get the file's name var name_xyz = fileList[i].name.toLowerCase( ); // Check if the name is suffixed with "_v1" or "_v2" -> some images received early on were, but we want to remove it if (name_xyz.indexOf( "_v1" ) > -1){ name_xyz = name_xyz.substring( 0, name_xyz.indexOf( "_v1" ) ); }else if (name_xyz.indexOf( "_v2" ) > -1){ name_xyz = name_xyz.substring( 0, name_xyz.indexOf( "_v2" ) ); }else{ name_xyz = name_xyz.substring( 0, name_xyz.indexOf( FILE_TYPE ) ); } // All file names should specify a suffix, which we want to further strip away to generate the top-level folder name var folder_name = "" if(name_xyz.indexOf('_') >= 0) folder_name = name_xyz.toUpperCase().substring( 0, name_xyz.indexOf( "_" ) ); else folder_name = name_xyz.toUpperCase(); // Create a folder to contain the outputs var newFolder = new Folder( folder_name ); // If the folder doesn't exist, create it if (!newFolder.exists){ newFolder.create( ); } if (!newFolder.exists){ // Warn alert( "Could not create folder " + newFolder.toString( ) + "; cannot proceed without." ); }else{ // Loop through the sizes var index = 0, imgCount = folders.length - 1; if(name_xyz.substring(11,13) == "_9"){ index = 8; imgCount = 9; name_xyz = name_xyz.replace('_9','') }
      for( var j = index; j < imgCount; j++ ){ // Create the subfolder (if it doesn't already) var subFolder = new Folder( newFolder.toString( ) + "/" + folders[j].name );
      if (!subFolder.exists){ subFolder.create( ); } if (!subFolder.exists){ // Warn alert( "Could not create folder " + subFolder.toString( ) + "; cannot proceed without." ); }else{ // Open the file in Photoshop var docRef = open(fileList[i]);
      if (docRef == null){ // Warn alert( "Failed to (properly) open " + name + "; can't do much more with/about it." ); }else{ // Resize the image docRef.resizeImage( folders[j].width, folders[j].height ); docRef.flatten(); var dupe = docRef.activeLayer.duplicate(); dupe.applyUnSharpMask(40,1,0); // UNSHARP MASK HERE // Export for web var outputFile = new File( subFolder.toString( ) + "/" + name_xyz.toUpperCase() + '.jpg' );
      if(folders[j].name == "marketing"){ if(name_xyz.indexOf('_') == -1){ if (docRef.bitsPerChannel != BitsPerChannelType.EIGHT) docRef.bitsPerChannel = BitsPerChannelType.EIGHT; var jpgSaveOptions = new JPEGSaveOptions(); jpgSaveOptions.embedColorProfile = true; jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE; jpgSaveOptions.matte = MatteType.NONE; jpgSaveOptions.quality = 12; docRef.saveAs(outputFile, jpgSaveOptions, true,Extension.UPPERCASE); } } else{ var exportOptionsSaveForWeb = new ExportOptionsSaveForWeb( );
      exportOptionsSaveForWeb.format = SaveDocumentType.JPEG; exportOptionsSaveForWeb.optimized = false; exportOptionsSaveForWeb.quality = 60; exportOptionsSaveForWeb.interlaced = true; exportOptionsSaveForWeb.includeProfile = false; exportOptionsSaveForWeb.blur = 0; docRef.exportDocument( outputFile, ExportType.SAVEFORWEB, exportOptionsSaveForWeb ); } dupe.remove(); } } } // Close the Photoshop file docRef.close(SaveOptions.DONOTSAVECHANGES); } }    }}
      catch (exception) {
      // Show degbug message and then bail out alert(exception);}
      finally {
      // Restore application preferences app.displayDialogs = startDisplayDialogs; app.preferences.rulerUnits = startRulerUnits;}