title: Extendscript Photoshop: Is there a way to save out jpeg with specific KB file sizes for web?
body: <p>I'm trying to utilize the following. what this does is saves out Jpeg accordingly to less then a a certain size set. I was curious if anyone knew a way to specifically direct a filename to get a filesize that is equal to the following or as close as possible.</p>
  
  <p>Filename_160x600.png = Filename_160x600.jpg   39kb</p>
  
  <p>Filename_300x600.png = Filename_300x600.jpg   59kb</p>
  
  <p>Filename_1500x513.png = Filename_1500x513.jpg 150kb</p>
  
  <pre><code>         saveJPG(
           {
           path: activeDocument.path,
           maxSize: 50 //size in kbs
           })
  
          function saveJPG(_data)
      {
      if (_data.path == undefined) return false;
      _data.name = _data.name == undefined ? activeDocument.name : _data.name;
      _data.quality == undefined &amp;&amp; _data.quality = 75;
  
      if (!new Folder(_data.path).exists)
  
  
  {
          alert("Output path doesn't exist!"); //you can add a function to create a path if needed
          return false
      }
  
      var options = new ExportOptionsSaveForWeb(),
          jpgFile = new File(_data.path + '/' + getName(_data.name) + '.jpg');
  
      options.format = SaveDocumentType.JPEG;
      options.quality = _data.quality;
      activeDocument.exportDocument(jpgFile, ExportType.SAVEFORWEB, options);
      if (_data.maxSize != undefined)
      {
          var ms = _data.maxSize * 1000;
          if (jpgFile.length &gt; ms)
          {
              if (!jpgFile.remove())
              {
                  alert('Save file is locked, please make sure it\'s not opened anywhere');
                  return
              };
              saveJPG(
              {
                  path: _data.path,
                  name: _data.name,
                  maxSize: _data.maxSize,
                  quality: _data.quality - 2
              });
          }
      };
  </code></pre>