4 Replies Latest reply on Apr 27, 2014 10:46 AM by ojodegato

    syntax error var retVal = false;

    ojodegato Level 1

      Hello All,

      I am working through the scripting exercises in the Power Speed and Automatization text book and would like to ask for help in debugging

      the script batchSaveJPEG. Not sure what is causing the syntax error with the variable var retVal = false; in the second function isImageFIle().

       

       

       

      ///////////////////////////////////////////////////////////////

      // SET UP

      ///////////////////////////////////////////////////////////////

       

       

      //Enable double clicking from hte Macintosh Finder or Windows  Explorer

      #target photoshop

       

       

      //Make Photoshop the formost Appplication

      app.bringToFront();

       

       

      //Adobe Photoshop/Presets/Scripts/Event Scripts Only

      var begDesc = "$$$/JavaScripts/batchSaveJPEG/Description=Batch procees jpg files." // endDesc

      var begName = "$$$/JavaScripts/batchSaveJPEG/MenuName= Batch Save Jpeg" // endName

       

       

      //A list of most common file extensions for image file formats

      var gImageFileExtensions = new Array("jpg", "jpeg", "gif", "png", "tif", "tiff", "psd");

       

       

      ///////////////////////////////////////////////////////////////

      // MAIN

      ///////////////////////////////////////////////////////////////

             

      //Ask a user for a folder to process

      var inputFolder = Folder.selectDialog ("Choose a folder to process");

        

      if(inputFolder != null){

          //Get teh fils inhte folder

          var processList = filterFilesForImages(inputFolder);

         

              if(processList.length > 0){           

                  //Ask the user for a folder to palce the new images

                  var outputFolder = Folder.selectDialog ("Choose a folder for the new files");

                 

                          if(outputFolder != null){

                             

                              try{

                                      //Process all the iamge files and save them

                                      processAndSaveFiles(processList, outputFolder);

                                  }

                             

                              catch(e){

                                      //Something went wrong

                                      alert ("Program error. The script did not complete.");

                                  }

                                                     

                          }

                 

                  }

             

              else{

                      //Alert no image files were found

                      alert ("No image files were found inhte folder.");

                  }

       

       

          }  

        

       

       

      ///////////////////////////////////////////////////////////////

      // Function filterFilesForImages

      ///////////////////////////////////////////////////////////////

      //Function to filter for image file formats

      function filterFilesForImages(myFolder){

             

              var returnFileList = new Array();

       

       

              var fileList = myFolder.getFiles();

             

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

                     

                      //Check if the file is an image file

                     

                      if(isImageFile(fileList[i])){

                             

                              //Add the file to the return list

                              returnFileList.push(fileList[i]);

                          }

                  }

              

              return returnFileList;

             

           }//end function

       

       

      ///////////////////////////////////////////////////////////////

      // Function isImageFile

      ///////////////////////////////////////////////////////////////

      //checks if a file is a known image file format

      function isImageFile(myFile){

         

              var retVal = false;      

             

              //First test if myFile is a file and not a hidden file

              if(myFile instanceof File && !myFile.hidden){

                     

                      //Get the file extension from the file

                      var fileExt = getFileExtension(myFile);

                     

                      //Check if file extension is an image extension

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

                             

                              if(fileExt == gImageFileExtensions[i]){

                                     

                                      //Found it, breake out of the for loop

                                      retVal = true;

                                     

                                      break;

                                  }

                             

                          }

                  }

             

              return retVal;

             

          }

       

       

      ///////////////////////////////////////////////////////////////

      // Function getFileExtension

      ///////////////////////////////////////////////////////////////

      //returns the file extension of a file

      function getFileExtension(myFile){

             

              var fileExt="";

             

              //Dissect the file name to see if it has a file extension

              var lastDot = myFile.name.lastIndexOf(".");

             

              if(lastDot > 0){

                     

                      //Find the file extension

                      var strLength = myFile.name.length;

                     

                      fileExt = myFile.name.substr(lastDot + 1, strLength - lastDot);

                     

                      fileExt = fileExt.toLowerCase();

                 

                  }

              return fileExt;

             

          }

       

       

      ///////////////////////////////////////////////////////////////

      // Function processAndSaveFiles

      ///////////////////////////////////////////////////////////////

      //wrapper to processe and save a list of files

      function processAndSaveFiles(fileList, outputFolder){

             

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

                     

                      processFile(fileList[i], outputFolder);

                 

                  }

          }

       

       

      ///////////////////////////////////////////////////////////////

      // Function processFile

      ///////////////////////////////////////////////////////////////

      //process the file and save it in the output folder

      function processFile(myFile, outputFolder){

         

              try{

                      var saveDialogMode = app.displayDialogs;

                      app.displayDialogs = DialogModes.NO;

                     

                      //Open the file in Photoshop without dialogs shown

                      app.open(myFile);

                     

                      //Process the document

                      //** in this case nothing **

                     

                      //Conver the document to save it as jpg in sRGB

                      app.activeDocument.flatten();

                      app.activeDocument.channels.removeAll();

                      app.activeDocument.pathItems.removeAll();

                      app.activeDocument.changeMode(ChangeMode.RGB);

                      app.activeDocument.bitsPerChannel = BitsPerChannelType.EIGHT;

                      app.activeDocument.convertProfile("sRGBIEC61966-2.1", Intent.RELATIVECOLORMETRIC);

                     

                      //Use the default JPEG options for saving

                      var myJPEGOPtions = new JPEGSaveOptions();

                     

                      //Save the file in the output folder

                      var newFileName = getFileNameWithoutExtension(app.activeDocument) + ".jpg";

                     

                      var newFileName = new File(outputFolder +"/"+ newFileName);

                     

                      app.activeDocument.saveAs(newFile, myJPEGOptions, true);

                     

                      //Close the document

                      app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);

                     

                      app.displayDialogs = saveDialogMode;

                  }

             

              catch(e)®{

                      //Something went wrong

                      alert ("An unexpected error occurred processing the file" +myFile.name);

                      alert(e);

                     

                     

                  }

             

          }

       

       

      ///////////////////////////////////////////////////////////////

      // Function getFileNameWithoutExtension

      ///////////////////////////////////////////////////////////////

      //return the file name without the extension

      function getFileNameWithoutExtension(myFile){

         

          var fileExt = "";

         

          //Disect the file name to see if it has a file extension

          var lastDot = myFile.toString().lastIndexOf(".");

         

          if(lastDot > 0){

                 

                  //Find the file extension

                  var strLength = myFile.name.length;

                 

                  fileExt = myFile.name.slice(0, lastDot - strLength - 1);

                 

                  fileExt = fileExt.toLowerCase();

              }

          return fileExt;

         

          }

        • 1. Re: syntax error var retVal = false;
          Jeffrey Tranberry Adobe Employee

          try this:

           

          // saveJPEG.jsx

          // Copyright 2011

          // Written by Geoff Scott

           

           

          /*

          Description:

          Example of saving documents as JPEG files

          */

           

           

          // 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 for image file formats

          var gImageFileExtensions = new Array("jpg", "jpeg", "gif", "png", "tif", "tiff", "psd");

           

           

          /////////////////////////

          // MAIN

          /////////////////////////

           

           

          // ask the user for a folder to process

          var inputFolder = Folder.selectDialog("Choose a folder to process");

          if (inputFolder != null)

          {

              // get the files in the folder

              var processList = filterFilesForImages(inputFolder);

              if (processList.length > 0)

              {

                  // since there are image files to process,

                  // ask the user for a folder to place the new files

                  var outputFolder = Folder.selectDialog("Choose a folder for the new files")

                  if (outputFolder != null)

                  {

                      try

                      {

                          // process all the image files and save them

                          processAndSaveFiles(processList, outputFolder);

                      }

                      catch(e)

                      {

                          // something went wrong

                          alert("An unexpected error occurred and the script did not complete.");

                          alert(e);

                      }

                  }

              }

              else

              {

                  // no image files were found, so let the user know

                  alert("No images files found in that folder.");

              }

          }

           

           

          /////////////////////////

          // FUNCTIONS

          /////////////////////////

           

           

          // function to filter for image file formats

          function filterFilesForImages(myFolder)

          {

              var returnFileList = new Array();

              var fileList = myFolder.getFiles();

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

              {

                  // check if the file is an image file

                  if (isImageFile(fileList[i]))

                  {

                      // add the file to the return list

                      returnFileList.push(fileList[i]);

                  }

              }

           

           

              return returnFileList;

          }

           

           

          // check if a file is a known image file format

          function isImageFile(myFile)

          {

              var retVal = false;

             

              // first test if myFile is a file and not a hidden file

              if (myFile instanceof File && !myFile.hidden)

              {

                  // get the file extension from the file

                  var fileExt = getFileExtension(myFile);

                 

                  // check if the file extension is an image extension

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

                  {

                      if (fileExt == gImageFileExtensions[i])

                      {

                          // found it, so break out of the loop

                          retVal = true;

                          break;

                      }

                  }

              }

             

              return retVal;

          }

           

           

          // return the file extension of a file

          function getFileExtension(myFile)

          {

              var fileExt = "";

           

           

              // dissect the file name to see if it has a file extesion

              var lastDot = myFile.name.lastIndexOf(".");

              if (lastDot > 0)

              {

                  // find the file extension

                  var strLength = myFile.name.length;

                  fileExt = myFile.name.substr(lastDot + 1, strLength - lastDot);

                  fileExt = fileExt.toLowerCase();

             }

           

           

              return fileExt;

          }

           

           

          // wrapper function to process and save a list of files

          function processAndSaveFiles(fileList, outputFolder)

          {

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

              {

                  processFile(fileList[i], outputFolder);

              }

          }

           

           

          // process the file and save it in the outputFolder

          function processFile(myFile, outputFolder)

          {

              try

              {

                  var saveDialogMode = app.displayDialogs;

                  app.displayDialogs = DialogModes.NO;

                 

                  // open the file in Photoshop without dialogs shown

                  app.open(myFile);

                 

                  // process the document

                  // ** in this case don't do anything to the document **

                 

                  // convert the document so it can be saved as a JPEG in sRGB

                  app.activeDocument.flatten();

                  app.activeDocument.channels.removeAll();

                  app.activeDocument.pathItems.removeAll();

                  app.activeDocument.changeMode(ChangeMode.RGB);

                  app.activeDocument.bitsPerChannel = BitsPerChannelType.EIGHT;

                  app.activeDocument.convertProfile("sRGB IEC61966-2.1", Intent.RELATIVECOLORIMETRIC);

           

           

                  // use the default JPEG options for saving

                  var myJPEGOptions = new JPEGSaveOptions();

           

           

                  // save the file in the outputFolder

                  var newFileName = getFileNameWithoutExtension(app.activeDocument) + ".jpg";

                  var newFile = new File(outputFolder + "/" + newFileName);

                  app.activeDocument.saveAs(newFile, myJPEGOptions, true);

           

           

                  // close the document

                  app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);

                 

                  app.displayDialogs = saveDialogMode;

              }

              catch(e)

              {

                  // something went wrong

                  alert("An unexpected error occurred processing the file " + myFile.name);

                  alert(e);

              }

          }

           

           

          // return the file name without the file extension

          function getFileNameWithoutExtension(myFile)

          {

              var fileExt = "";

           

           

              // dissect the file name to see if it has a file extesion

              var lastDot = myFile.toString().lastIndexOf(".");

              if (lastDot > 0)

              {

                  // find the file extension

                  var strLength = myFile.name.length;

                  fileExt = myFile.name.slice(0, lastDot - strLength - 1);

                  fileExt = fileExt.toLowerCase();

              }

           

           

              return fileExt;

          }

          • 2. Re: syntax error var retVal = false;
            ojodegato Level 1

            Thanks for looking at the script and responding my query. I am learning Photoshop scripting and there are areas that I don't understand well. For instance, how do you check for syntax and logic errors when working with a long script?

             

            Does Javascript allow for sequential debugging when writing a script or does the script need to be completed before the checking for errors.

             

            While working on the batchSaveJPEG script I keept compiling the script regularly to find erros.

            The var retVal = false;   syntax error appeared with the second function isImageFIle(myFile).

            I was not sure if the error was there because the script was incomplete and would cancel out once all the functions were in place completing the script.

            • 3. Re: syntax error var retVal = false;
              Chuck Uebele Adobe Community Professional & MVP

              There are several debugging features in ESTK.  I typically just create small amounts of code and check to make sure they work.  The use of: alert to generate a dialog box with a suspected problem is extremely useful, as is $.writeln(), which writes the info to the ESTK console, so it doesn't stop the script - very useful for loops and lots of data checking.

              • 4. Re: syntax error var retVal = false;
                ojodegato Level 1

                I will review the debugging section in the JavaScript Tools Guide to learn the debuging features.

                Thanks for the back!