Help with editing a script
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;
}