3 Replies Latest reply on May 12, 2018 8:23 PM by JJMack

    Need help opening each file, crop canvas, save as copy then close file for multiple files.

    johnnysmith0825
      function saveJPEG( doc, saveFile, qty ) {  
           var saveOptions = new JPEGSaveOptions( );  
           saveOptions.embedColorProfile = true;  
           saveOptions.formatOptions = FormatOptions.STANDARDBASELINE;  
           saveOptions.matte = MatteType.NONE;  
           saveOptions.quality = qty;   
           doc.saveAs( saveFile, saveOptions, true );  
      }  
       
       
      var PSDs = new Folder('D:/lineartPSDs/PSDs/');
       
      for (var a = 0; a <= PSDs.length; a++) {
       var fileref = PSDs + a.toString() + '.psd';
       app.open( new File(fileref) );
       var docRef = app.activeDocument;
       app.activeDocument.resizeCanvas(w/2, h, AnchorPosition=AnchorPosition.MIDDLERIGHT);
       saveJPEG(app.activeDocument, new File(decodeURI('D:/lineartPSDs/')+ a.toString() +'.jpg'), 10 );
       app.docRef.close(SaveOptions.DONOTSAVECHANGES);
      }
      

       

      The above is my code when I run it nothing happens, no error. I have tried to wait a bit to see if there was a delay but no.

       

      The goal here is to open about 80 images and crop 50% of the canvas horizontally and only keep the left side of the canvas. Save the file in jpg format and then close the file, then repeat for the next 79 files. I haven't used javascript in a long time so I really need some help debugging this piece of code.

        • 1. Re: Need help opening each file, crop canvas, save as copy then close file for multiple files.
          JJMack Most Valuable Participant

          You need to get the file list of files in the PSDs Folder you wish to crop and process those files.

           

          // Loop Image Files

          var fileList = PSDs.getFiles(/\.(nef|cr2|crw|dcs|raf|arw|orf|dng|jpg|jpe|jpeg|tif|tiff|psd|eps|png|bmp)$ /i);

          // Loop through files

          for (var i = 0; i < fileList.length; i++) {

             open crop and save

             }

          • 2. Re: Need help opening each file, crop canvas, save as copy then close file for multiple files.
            johnnysmith0825 Level 1

            This did not work, there were no errors. If I manually set an upper bound for "i" eg: i <= 10 then it will open 10 of my photoshop files. I might just count how many files I have and directly set that to the upper bound. I also tried to debug with console.log() and I get console is not defined??

             

            Edited:

             

            New problem... I am trying to crop the left half of my image and save the file, here is my code:

             

            for(var i = 0; i <= 3; i++){
            app.open( new File('D:/lineartPSDs/PSDs/' + i.toString() + '.psd') );
            var docRef = app.activeDocument;
            var w = app.activeDocument.width;
            var h = app.activeDocument.height;
            app.activeDocument.resizeCanvas(w/2, h, AnchorPosition=AnchorPosition.MIDDLELEFT);
            saveJPEG(app.activeDocument, new File(decodeURI('D:/lineartPSDs/')+ i.toString() +'.jpg'), 10 );
            docRef.close(SaveOptions.DONOTSAVECHANGES);
             }
            

             

            This opens 4 PSDs from file 0 to 3. For each loop I would get the active document's width and height, I then call resizeCanvas to crop my image. I want half the width of the canvas cropping from the middle left anchorpoint. The first crop comes out great the second crops from the middle and the 3rd just fails with an error. See pic below to see what I am talking about. As you can see I have line art on the left side of the canvas and a reference on the right. It feels like "var w" or the anchorPosition is not updated correctly, maybe its because app.activeDocument is not grabbing the new opened file?

             

            Screenshot_38.png

            • 3. Re: Need help opening each file, crop canvas, save as copy then close file for multiple files.
              JJMack Most Valuable Participant

              From what you posted it is not possible to tell what is going on.  I do not know what your Photoshop ruler units is set to or what the visible contents of psd 0 1 2 and 3 are.   Your script should save jpg 0 1 2 and 3.  You show there is a file 0 and 1 in D:\lineartPSDs\ they may be jpg files if you let windows hide known extensions.  You do not state  that the script failed to open  PSD 2 and 3 or failed to save jpeg 2 and 3 there is  no try catch statements in your script to handle errors encountered by your script.

               

              You could easily do what you want you want by batching an action that crops the current document to its left half width.  Not rely on there being  files 0.psd 1.psd 2.psd and 3.psd in the source folder.  Process all files in the PSDs folder and save jpeg to the lineartpsds folder.