// Copyright 2007 // Written by Jeffrey Tranberry // Photoshop for Geeks Version 1.0 /* Description: This script demonstates how to batch process a folder of images using the crop and straighten command */ // enable double clicking from the // Macintosh Finder or the Windows Explorer #target photoshop // Make Photoshop the frontmost application // in case we double clicked the file app.bringToFront(); /////////////////////////// // SET-UP // /////////////////////////// // A list of file extensions to skip, keep them lower case gFilesToSkip = Array( "db", "xmp", "thm", "txt", "doc", "md0", "tb0", "adobebridgedb", "adobebridgedbt", "bc", "bct" ); /////////////////////////// // MAIN // /////////////////////////// main(); /////////////////////////// // FUNCTIONS // /////////////////////////// function main(){ // Because we are doing a for loop later in the script that processes all open documents // We don't want to have any documents open that we don't want accidentally processed // Warn the user if they have an open document and exit the script with return if (documents.length > 0){ alert ("This script requires that there are no open documents to run."); return; } // Pops open a dialog for the user to choose the folder of documents to process var inputFolder = Folder.selectDialog("Select a folder of documents to process"); // Pops open a dialog for the user to set the output folder var outputFolder = Folder.selectDialog("Select a folder for the output files"); // Open and process a folder of Images OpenFolder(outputFolder, inputFolder); } // Given the a Folder of files, open the files and process them function OpenFolder(outputFolder, inputFolder) { var filesOpened = 0; var fileList = inputFolder.getFiles(); for ( var i = 0; i < fileList.length; i++ ) { // Make sure all the files in the folder are compatible with PS if ( fileList[i] instanceof File && ! fileList[i].hidden && ! IsFileOneOfThese( fileList[i], gFilesToSkip )) { open( fileList[i] ); filesOpened++; // Store the name of the parent document we're going to crop and straighten from var docRef = activeDocument; // Process the document, which will rusult in more than one open document cropAndStraighten(); // Close the parent document without saving docRef.close(SaveOptions.DONOTSAVECHANGES); // Assume the rest of the open documents are the images extracted from the parent document // Process each one in turn until there are no more docmuents. while (app.documents.length >=1){ // Flatten the document in case the file type we want to save to requires a flat doc app.activeDocument.flatten(); // Save the document as a JPEG to the folder the user specified saveJPG(outputFolder); // Close the document because it was saved in the step before fileClose(SaveOptions.DONOTSAVECHANGES); } } } // There are no more documents to open so exit the script return filesOpened; } // given a file name and a list of extensions // determine if this file is in the list of extensions function IsFileOneOfThese( inFileName, inArrayOfFileExtensions ) { var lastDot = inFileName.toString().lastIndexOf( "." ); if ( lastDot == -1 ) { return false; } var strLength = inFileName.toString().length; var extension = inFileName.toString().substr( lastDot + 1, strLength - lastDot ); extension = extension.toLowerCase(); for (var i = 0; i < inArrayOfFileExtensions.length; i++ ) { if ( extension == inArrayOfFileExtensions[i] ) { return true; } } return false; } // fileClose function for closing current document function fileClose(options) { app.activeDocument.close(options); } // Crop and Straighten function cropAndStraighten(){ var id333 = stringIDToTypeID( "CropPhotosAuto0001" ); executeAction( id333, undefined, DialogModes.NO ); } // Save JPG function saveJPG(filePath){ var id43 = charIDToTypeID( "save" ); var desc11 = new ActionDescriptor(); var id44 = charIDToTypeID( "As " ); var desc12 = new ActionDescriptor(); var id45 = charIDToTypeID( "EQlt" ); desc12.putInteger( id45, 10 ); var id46 = charIDToTypeID( "MttC" ); var id47 = charIDToTypeID( "MttC" ); var id48 = charIDToTypeID( "None" ); desc12.putEnumerated( id46, id47, id48 ); var id49 = charIDToTypeID( "JPEG" ); desc11.putObject( id44, id49, desc12 ); var id50 = charIDToTypeID( "In " ); desc11.putPath( id50, new File( filePath ) ); var id51 = charIDToTypeID( "LwCs" ); desc11.putBoolean( id51, true ); executeAction( id43, desc11, DialogModes.NO ); }