3 Replies Latest reply on Oct 10, 2012 11:41 PM by vinoth_mannu

    Folder name as to be file name

    vinoth_mannu Level 1

      Hi All,

       

      The below script saves the files but if we have multiple files the files are not saved.

       

      #target photoshop

       

      inputFolder = Folder.selectDialog("Select a folder contains '*.tif' files ");

       

      //var inputFolder = Folder ( 'D:/Work/In/');
      //var outputFolder = Folder( ['D:/Work/out/'] );

      if (inputFolder != null){
      fileType = ( '*.tif' );
      var fileList = inputFolder.getFiles(fileType);
      //$.writeln(fileList);fileList

                     
          if(fileList.length == 0 )
          {
                  alert ("No files found in the folder", "test", "errorIcon")
          }
          else
          {
              for (var i = 0; i < fileList.length; i++)
              {
                      if (fileList[i] instanceof File && fileList[i].hidden == false)
                      {            
              var TiffOptions = new TiffSaveOptions()
                          TiffOptions.imageCompression = TIFFEncoding.TIFFLZW
                          TiffOptions.embedColorProfile = false

                      var docRef = open( fileList[i] )
                      app.preferences.rulerUnits = Units.PICAS
                      docRef.flatten()
                      docRef.resizeImage (null, null, 600)
                      var Wid = docRef.width;
                      var Hig = docRef.height;
                      app.preferences.rulerUnits = Units.PICAS
                     
                      if (Number(Wid) > 30)
                          {
                               docRef.resizeImage(29,null);
                          }   
                      else if(Number(Hig) > 50)
                          {
                              docRef.resizeImage(null,50);
                           }
                      //$.writeln(Wid);Wid
                      //$.writeln(Hig);Hig
                    var newName = removeExtension(inputFolder.name);
                    $.writeln(newName);newName
                   
      for(a=1; a<12; a++){  

         
          docRef.saveAs(new File(inputFolder+"/"+newName+'_f00'+a+".tif"), TiffOptions, true, Extension.LOWERCASE)

       

          docRef.close(SaveOptions.DONOTSAVECHANGES)
              
      }
      //var newcmd = fileList[i].remove()
                      }
              }
              alert ("Process completed", "test")   
          }
      }

      /********************************************************************/
      function removeExtension(myDoc)
      {
          var str = myDoc.split(".tif");
          var ext = str[0]
          return ext;
      }
      /********************************************************************/

      /********************************************************************/
      function removeExt(myDoc)
      {
          var str = myDoc.split(".");
          var ext = str[1]
          return ext;
      }
      /********************************************************************/

      /********************************************************************/
      function removebit(myDoc)
      {
          var str = myDoc.split(".");
          var ext = str[1]
          return ext;
      }
      /********************************************************************/

       

      Please help me in solving this issue.

        • 1. Re: Folder name as to be file name
          c.pfaffenbichler Level 9

          I haven’t tested it but at a cursory glance I suspect you are wrong and the files are being saved, but as per

          docRef.saveAs(new File(inputFolder+"/"+newName+'_f00'+a+".tif"), TiffOptions, true, Extension.LOWERCASE)

          you are using the same name, so they get overwritten.

           

          Edit: Well, I stand corrected.

          • 2. Re: Folder name as to be file name
            JJMack Most Valuable Participant

            Look like you have a for loop to save 11 copies of the document but you close the document after the first save so the second save should fail for there is no open document to save.

            #target photoshop

             

            inputFolder = Folder.selectDialog("Select a folder contains '*.tif' files ");

             

            //var inputFolder = Folder ( 'D:/Work/In/');

            //var outputFolder = Folder( ['D:/Work/out/'] );

            if (inputFolder != null){

            fileType = ( '*.tif' );

            var fileList = inputFolder.getFiles(fileType);

            //$.writeln(fileList);fileList

                           

                if(fileList.length == 0 )

                {

                        alert ("No files found in the folder", "test", "errorIcon")

                }

                else

                {

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

                    {

                            if (fileList[i] instanceof File && fileList[i].hidden == false)

                            {            

                    var TiffOptions = new TiffSaveOptions()

                                TiffOptions.imageCompression = TIFFEncoding.TIFFLZW

                                TiffOptions.embedColorProfile = false

                            var docRef = open( fileList[i] )

                            app.preferences.rulerUnits = Units.PICAS

                            docRef.flatten()

                            docRef.resizeImage (null, null, 600)

                            var Wid = docRef.width;

                            var Hig = docRef.height;

                            app.preferences.rulerUnits = Units.PICAS

                            if (Number(Wid) > 30)

                                {

                                     docRef.resizeImage(29,null);

                                }   

                            else if(Number(Hig) > 50)

                                {

                                    docRef.resizeImage(null,50);

                                 }

                            //$.writeln(Wid);Wid

                            //$.writeln(Hig);Hig

                          var newName = removeExtension(inputFolder.name);

                          $.writeln(newName);newName

                         

            //for(a=1; a<12; a++){ 

                var a = i +1;

                docRef.saveAs(new File(inputFolder+"/"+newName+'_f00'+a+".tif"), TiffOptions, true, Extension.LOWERCASE)

             

                docRef.close(SaveOptions.DONOTSAVECHANGES)

                    

            //}

            //var newcmd = fileList[i].remove()

                            }

                    }

                    alert ("Process completed", "test")   

                }

            }

            /********************************************************************/

            function removeExtension(myDoc)

            {

                var str = myDoc.split(".tif");

                var ext = str[0]

                return ext;

            }

            /********************************************************************/

            /********************************************************************/

            function removeExt(myDoc)

            {

                var str = myDoc.split(".");

                var ext = str[1]

                return ext;

            }

            /********************************************************************/

            /********************************************************************/

            function removebit(myDoc)

            {

                var str = myDoc.split(".");

                var ext = str[1]

                return ext;

            }

            /********************************************************************/

             

            • 3. Re: Folder name as to be file name
              vinoth_mannu Level 1

              Excellent

               

              That worked perfect

               

              Thanks Mack