2 Replies Latest reply on May 4, 2017 3:15 PM by JJMack

    ReferenceError: open is not a function

    cwodwrth Level 1

      I've written the following script to iterate through a hierarchy of directories (using some code on this forum), then it opens any PDF files and resaves them without any downsampling. It works great, except when it's run for a while it has two issues:

       

      1) It always seems to crap out after about 1400 files processed, it simply errors out in the code somewhere which has worked fine in the previous 1399 files. If I restart, it works fine again - this seems to happen with every photoshop script I've worked on (I usually have to process over 50,000 files in a batch, so I just have to keep rerunning things over and over), I don't know if there is a memory leak or something, but at some point during a run of a lot of files (usually after 1400 openings or so), something simply fails.

       

      2) My main problem right now is that after a running this on about 8100 files (in 4 different sessions), the script has simply stopped working, giving me the error message:

       

      ReferenceError: open is not a function

       

      Now, as I said, this script has worked perfectly for two days (other than issue #1), and it seems to do this no matter what the input file is. This happened once yesterday and it just seems like after a day it started working again today until it failed again. Has anyone experienced this before and have a solution? I have no idea why this would suddenly change when the code hasn't:

       

      var topFolder = new Folder('//192.168.0.25/Assets');

      var fileandfolderAr = scanSubFolders(topFolder,/\.(pdf)$/i);

       

      function getPDFSaveOptions()

      {

          var pdfOpts = new PDFSaveOptions();

          pdfOpts.downSample = PDFResample.NONE;

          pdfOpts.generateThumbnails = true;

          pdfOpts.preserveEditability = true;  

          return pdfOpts;

      }

       

      function scanSubFolders(tFolder, mask) { // folder object, RegExp or string

          var sFolders = new Array();

          var allFiles = new Array();

          sFolders[0] = tFolder;

          for (var j = 0; j < sFolders.length; j++){ // loop through folders        

              var procFiles = sFolders[j].getFiles();

              for (var i=0;i<procFiles.length;i++){ // loop through this folder contents

                  if (procFiles[i] instanceof File ){

                      //if(mask==undefined) allFiles.push(procFiles[i]);// if no search mask collect all files

                      if (procFiles[i].fullName.search(mask) != -1)

                      {

                         $.writeln(fileIndex + " " + procFiles[i]);

                         try

                         {

                             var docRef = open(procFiles[i]); <----- FAILS HERE

                             docRef.saveAs(new File(docRef.fullName), getPDFSaveOptions(), false, Extension.LOWERCASE);       

                             docRef.close();

                         }

                         catch(e){ alert(e); }                         

                         fileIndex++;

                      }

              }else if (procFiles[i] instanceof Folder){

                  sFolders.push(procFiles[i]);// store the subfolder

                  scanSubFolders(procFiles[i], mask);// search the subfolder

               }

            }

         }

         return [allFiles,sFolders];

      };