-
-
Save discostu105/98c945eafffd8a51bf3ce9c84f6efd81 to your computer and use it in GitHub Desktop.
// cropAndStraightenBatch.jsx | |
// Copyright 2006-2008 | |
// Written by Jeffrey Tranberry | |
// Photoshop for Geeks Version 2.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(); | |
///////////////////////// | |
// SETUP | |
///////////////////////// | |
// 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(); | |
function main() { | |
//Make sure there are no open documents | |
if (app.documents.length > 0){ | |
alert ("This script requires that there are no open documents to run."); | |
}else{ | |
// 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 | |
OpenFolderRecursively(inputFolder, outputFolder); | |
} | |
} | |
///////////////////////// | |
// FUNCTIONS | |
///////////////////////// | |
// Given the a Folder of files, open the files and process them | |
function OpenFolderRecursively(inputFolder, outputFolder) { | |
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++; | |
///////////////////////// | |
// Put all your processing functions... | |
///////////////////////// | |
// Create a variable to store a reference to | |
// the currently active document, which in this | |
// case is the parent document we want to extract | |
// multiple scanned images from | |
var docRef = app.activeDocument; | |
// Run the cropAndStraighten function | |
// which will rusult in more than one open document | |
cropAndStraighten(); | |
// Close the parent document we originally opened | |
docRef.close(SaveOptions.DONOTSAVECHANGES); | |
// Process all open documents until no documents | |
// are left open. | |
while (app.documents.length >=1){ | |
///////////////////////// | |
// Put all your processing functions... | |
///////////////////////// | |
// Flatten the document in case the file type we want to save to requires a flat doc | |
app.activeDocument.flatten(); | |
//Save as a JPEG to the outputFolder | |
var jpegOptions = new JPEGSaveOptions(); | |
jpegOptions.quality = 10; | |
jpegOptions.embedColorProfile = false; | |
app.activeDocument.saveAs( File( outputFolder + "/" + activeDocument.name + ".jpg"), jpegOptions, false); | |
// Close without saving | |
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES); | |
///////////////////////// | |
// ...in the area between these two comments. | |
///////////////////////// | |
} | |
///////////////////////// | |
// ...in the area between these two comments. | |
///////////////////////// | |
} | |
// go recursive | |
if (!(fileList[i] instanceof File)) { | |
var newOuputFolder = new Folder(outputFolder + "/" + new Folder(fileList[i]).name); | |
newOuputFolder.create(); | |
OpenFolderRecursively(fileList[i], newOuputFolder); | |
} | |
} | |
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; | |
} | |
// Crop and Straighten function created | |
// using the ScriptingListener plug-in | |
function cropAndStraighten(){ | |
var id333 = stringIDToTypeID( "CropPhotosAuto0001" ); | |
executeAction( id333, undefined, DialogModes.NO ); | |
} |
Thanks to discostu105 & Jeffrey Tranberry for this very useful script. I know next to nothing of Java and I'm hoping for some help modifying it.
I am working with layered, 16 bit/channel tiff files. The background layer is not selected when the file is opened so the cropandstraighten command fails. I need to add a step before line 77 to select the background layer. I want to retain the layers and so a 'flatten' step doesn't help. Can anyone give me an example of the line needed to select the background layer? The total number of layers will vary but it'll always be named 'background' and be at the bottom of the layer stack.
I may also want to save as flattened jpg and to do that I'll need a step to convert 16bpc files to 8bpc. How would I write that?
Thanks in advance for any help!
Yeah, should probably include a check to make sure the output folder isn't inside the input folder. Otherwise infinite loops 😆
Thanks atav32. That's not a problem. I just need to find how to do write the steps above.
Thanks @discostu105.
I've enhanced your script;
- Made the script continue when it has an image it can't work with (resulted in a crash before)
- Added a check to make sure the output folder is not the same or within the input folder (@atav32 's suggestion)
- Set jpg quality to 12 (max)
Link: https://gist.github.com/jaap/f097d07ebbdd51fe5f6b4e743a110b46
HUGE help - thank you for providing this to the public!
Thanks for awesome script. I am trying to use this script. However whenever the script encounters an error (if crop function is not available) then script fails and aborts. I am wondering if you can modify the script to copy the original file to destination if there is any error. That way destination folder will not have any missing photos.