Advertisement
Guest User

cropAndStraightenBatch.jsx edited

a guest
Sep 10th, 2020
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // cropAndStraightenBatch.jsx
  2. // Copyright 2006-2008
  3. // Written by Jeffrey Tranberry
  4. // Photoshop for Geeks Version 2.0
  5.  
  6. /*
  7. Description:
  8. This script demonstates how to batch process
  9. a folder of images using the crop and straighten command
  10. */
  11.  
  12. // enable double clicking from the
  13. // Macintosh Finder or the Windows Explorer
  14. #target photoshop
  15.  
  16. // Make Photoshop the frontmost application
  17. // in case we double clicked the file
  18. app.bringToFront();
  19.  
  20. /////////////////////////
  21. // SETUP
  22. /////////////////////////
  23.  
  24. // A list of file extensions to skip, keep them lower case
  25. gFilesToSkip = Array( "db", "xmp", "thm", "txt", "doc", "md0", "tb0", "adobebridgedb", "adobebridgedbt", "bc", "bct" );
  26.  
  27. /////////////////////////
  28. // MAIN
  29. /////////////////////////
  30.  
  31. //Make sure there are no open documents
  32. if (app.documents.length > 0){
  33.     alert ("This script requires that there are no open documents to run.");
  34. }else{
  35.    
  36.     // Pops open a dialog for the user to choose the folder of documents to process
  37.     var inputFolder = Folder.selectDialog("Select a folder of documents to process");
  38.  
  39.     // Pops open a dialog for the user to set the output folder
  40.     var outputFolder = Folder.selectDialog("Select a folder for the output files");
  41.  
  42.     // Open and process a folder of Images
  43.     OpenFolder();
  44.    
  45. }
  46.  
  47. /////////////////////////
  48. // FUNCTIONS
  49. /////////////////////////
  50.  
  51. // Given the a Folder of files, open the files and process them
  52. function OpenFolder() {
  53.         var filesOpened = 0;
  54.         var fileList = inputFolder.getFiles();
  55.         for ( var i = 0; i < fileList.length; i++ ) {
  56.                    // Make sure all the files in the folder are compatible with PS
  57.                 if ( fileList[i] instanceof File && ! fileList[i].hidden && ! IsFileOneOfThese( fileList[i], gFilesToSkip )) {
  58.                         open( fileList[i] );
  59.                         filesOpened++;
  60.                
  61.                     /////////////////////////
  62.                     // Put all your processing functions...
  63.                     /////////////////////////
  64.                         app.doAction ('ScanPrep','ScanPrep')
  65.                             // Create a variable to store a reference to
  66.                             // the currently active document, which in this
  67.                             // case is the parent document we want to extract
  68.                             // multiple scanned images from
  69.                             var docRef = app.activeDocument;
  70.  
  71.                             // Run the cropAndStraighten function
  72.                             // which will rusult in more than one open document
  73.                             cropAndStraighten();
  74.  
  75.                             // Close the parent document we originally opened
  76.                             docRef.close(SaveOptions.DONOTSAVECHANGES);
  77.                    
  78.                             // Process all open documents until no documents
  79.                             // are left open.
  80.                             while (app.documents.length >=1){
  81.  
  82.                                 /////////////////////////
  83.                                 // Put all your processing functions...
  84.                                 /////////////////////////
  85.                                
  86.                                     // Flatten the document in case the file type we want to save to requires a flat doc
  87.                                     app.activeDocument.flatten();
  88.                                
  89.                                     //Save as a JPEG to the outputFolder
  90.                                     var jpegOptions = new JPEGSaveOptions();
  91.                                     jpegOptions.quality = 10;
  92.                                     jpegOptions.embedColorProfile = false;
  93.                                     app.activeDocument.saveAs( File( outputFolder  + "/" + activeDocument.name + ".jpg"), jpegOptions, false);
  94.                                    
  95.                                     // Close without saving
  96.                                      app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
  97.                                
  98.                                 /////////////////////////
  99.                                 // ...in the area between these two comments.
  100.                                 /////////////////////////
  101.                             }
  102.                    
  103.                     /////////////////////////
  104.                     // ...in the area between these two comments.
  105.                     /////////////////////////
  106.  
  107.                 }
  108.         }
  109.         return filesOpened;
  110. }
  111.  
  112. // given a file name and a list of extensions
  113. // determine if this file is in the list of extensions
  114. function IsFileOneOfThese( inFileName, inArrayOfFileExtensions ) {
  115.     var lastDot = inFileName.toString().lastIndexOf( "." );
  116.     if ( lastDot == -1 ) {
  117.         return false;
  118.     }
  119.     var strLength = inFileName.toString().length;
  120.     var extension = inFileName.toString().substr( lastDot + 1, strLength - lastDot );
  121.     extension = extension.toLowerCase();
  122.     for (var i = 0; i < inArrayOfFileExtensions.length; i++ ) {
  123.         if ( extension == inArrayOfFileExtensions[i] ) {
  124.             return true;
  125.         }
  126.     }
  127.     return false;
  128. }
  129.  
  130. // Crop and Straighten function created
  131. // using the ScriptingListener plug-in
  132. function cropAndStraighten(){
  133.     var id333 = stringIDToTypeID( "CropPhotosAuto0001" );
  134.     executeAction( id333, undefined, DialogModes.NO );
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement