Skip to main content
Participating Frequently
April 11, 2017
Question

Help with editing a script

  • April 11, 2017
  • 5 replies
  • 493 views

Hi there

I have this script that works fine. this script reduces the size of an image by various sizes and places them in their designated folder. what i need is for this script to also crop an image to a given size and place that in its specified folder. I have inherited this script and therefore i am not usually working with .jsx files as i mainly handle my actions within Photoshop itself

any help would be greatly appreciated

// 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 instanceof File) && (fileList.hidden == false)){

  // Get the file's name

  var name_xyz = fileList.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.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);

  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.width, folders.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.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;

}

This topic has been closed for replies.

5 replies

dannyc_TPAuthor
Participating Frequently
April 18, 2017

does the plug in export out a script? can i then inject the script into the script i have already? bottom line is i will need to use a script and nothing else

thanks

dannyc_TPAuthor
Participating Frequently
April 18, 2017

The Script i have generates the folders. we want to use this script on hundreds of images so a script that is there fully working is what is needed. We have the 1st one that opens a file and saves to multiple folders depending on the size. This final script i need to do everything it does already but instead just crop them. I am not able to use the plug in as it has to be used within a script. do you know how to edit the script at all? i am not at this time able to use the plugin since a plugin was never used and the company will not go from a method they have worked with to one they are not familiar working with at all.

We wont be using the plugin just the jsx file

thanks

JJMack
Community Expert
Community Expert
April 18, 2017

That is why I suggest you look at the Image Processor Pro plug-in Script theat you can download and install.  That Script is designed to process your image files to create the Image files you need when your need the to be.  It has options to resize and include simple actions you write the enable you to add you customization to the files being save. All you would need to do is record the Action to set the selection and then image crop. You would use the Image Processor Pro  dialog to select Tarher you input files. Create Tab fir the files you want to save. In the tab you would add you simple Action to do the set selection and crop.  The Script does all the hard work. For you and give you the option for naming the output files and locations.

JJMack
dannyc_TPAuthor
Participating Frequently
April 18, 2017

Hi there

thanks for your reply.

When it comes to coding i am not that guy. i can adapt code and search for code and adapt it to suit. thats as far as my coding skills go.

so basically for my 2250*3000 image i need to get a script that does something like this:

for (var i = 0; i < files.length; i++) {
  var doc = app.open(files);
  var bounds = [375, 0, 2625, 3000];
  doc.crop(bounds);

    ....

}

If this is the sort if script i need then u will need to add this to the main script as i need all the folder actions to happen. I understand i may need to get rid of a chunk that deals with square resizing processes.

it will only be 1 folder that needs to be created instead of the many that are there now.

Would it be ok for me to post up code i have adapted and you can see if i am on the right track?

(i don't think i need the FOR loop there are there are loops already in the existing script)

Thanks for your help so far. Very much appreciated

JJMack
Community Expert
Community Expert
April 12, 2017

To me it look like that script just resize image to a square 1:1 aspect ratio that are different in size.  If the source image is not square forcing them to square would distort them.  I see no crop in the script.

IMO you would be better off looking at the download Plug-in Image Processor Pro.  It is a very good and can easily be set to do what you need to do.  You could include different action for each output file.   You may want to use different unsharp setting for the different image sizes  instead of using 40 1 0 for all images like this script does. The plug-in will resize and preserve the image aspect ratio son none square image will not be distorted.

Image Processor pro will process all image file types not just Tiff like the script you posted.

JJMack
dannyc_TPAuthor
Participating Frequently
April 12, 2017

Hi there

Yes you are right. this is a script that resizes a 3000 px square tiff image to various square'd sizes of jpgs. I am after a script that instead crops.

The link below asks the question and has the crop snippet included. this is my first time scripting so i do not know fully what needs to be done. I need to have this script in a working order so i can transfer it back to the designer who will use the script for all project images for the new site design that he is involved in.

thanks once again

How can i get this script to work?