2 Replies Latest reply on Apr 11, 2016 11:26 AM by testr3456456546

    Save the file with the same name

    testr3456456546

      How to change the script to save the file with the same name ?


      
      
      
      
      //SaveforWebSP.jsx  
      
      
      
      //Save a fixed width image with a size constraint in its original folder  
      
      
       
       
      
      
      
      var docRef = activeDocument;  
      
      
      
      var outputFolder = docRef.path;  
      
      
       
       
      
      
      
      NamesaveRef = new File( outputFolder + "/reference.jpg" );  
      
      
      
      var NewfileRef = new File( NamesaveRef )  
      
      
       
       
      
      
      
      // quality/size constraints  
      
      
      
      var w = 1920; // new file width  
      
      
      
      var MaxSz =  361440; // max. 360Kb  
      
      
      
      var Qlt = 100; // initial quality 100  
      
      
      
      var x = 1; // decreasing step  
      
      
       
       
      
      
      
      // resize the image to the right width  
      
      
      
      docRef.resizeImage(UnitValue(w,"px"),null,100,ResampleMethod.BICUBICSHARPER);  
      
      
       
       
      
      
      
      // Perform the first SaveForWeb Operation  
      
      
      
      ExpWeb(NewfileRef, Qlt);  
      
      
       
       
      
      
      
      // Keep trying to save the file with max. Qlt, but under MaxSz 
       
      
      
      
      while (NewfileRef.length > MaxSz)  
      
      
      
      {  
      
      
            
      Qlt = Qlt - x;  
      
      
            
      NewfileRef = new File( NewfileRef );
       
      
      
            
      NewfileRef.remove();  
      
      
            
      ExpWeb(NewfileRef, Qlt);  // Perform a new SaveForWeb Operation, with slightly lower Qlt  
      
      
               
      if (Qlt <= 40) {  
      
      
                 
      alert("The file can't be saved with the desired size AND quality.");  
      
      
                 
      break  // break the loop whenever the quality is as low as 50 (this shouldn't need to happen)  
      
      
               
      }  
      
      
      
      }  
      
      
       
       
      
      
      
      var FileSz = NewfileRef.length/1024;  
      
      
      
      FileSz = Math.round(FileSz);  
      
      
       
       
      
      
      
      // close the original file without saving  
      
      
      
      activeDocument.close(SaveOptions.DONOTSAVECHANGES);  
      
      
       
       
      
      
      
      // SaveForWeb Export, with the desired constraints and parameters  
      
      
      
      function ExpWeb(FileNm, Qlt)  
      
      
      
      {  
      
      
            
      var options = new ExportOptionsSaveForWeb();  
      
      
            
      options.quality = Qlt;   // Start with highest quality (biggest file).  
      
      
            
      options.format = SaveDocumentType.JPEG;   // Save Format for the file  
      
      
            
      docRef.exportDocument(File(FileNm), ExportType.SAVEFORWEB, options);  
      
      
      
      }