Skip to main content
Participant
November 28, 2018
Question

Editing existing code but change saveDocumentAsJPEG to SaveForWeb

  • November 28, 2018
  • 2 replies
  • 824 views

Hi everybody,

Firstly apologies, I am a Designer and not a Developer.

I have found the following script for saving out multiple layer compositions in Photoshop, which is great, but it save out as standard JPegs when I would like it to save out SaveForWeb jpegs at 60%. Any help would be greatly appreciated.

Thanks,

Alex

var userDisplayDialogsPref = app.displayDialogs;

app.displayDialogs = DialogModes.ALL;

var savePath;

app.displayDialogs = DialogModes.NO;

function getType(thing){

    if(thing === null) return "[object Null]"; // special case

    return Object.prototype.toString.call(thing);

}

function getCombinations(arr, n) {

  if (n == 1) {

      var ret = [];

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

          for (var j = 0; j < arr.length; j++) {

              ret.push([arr]);

          }

      }

      return ret;

  } else {

      var ret = [];

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

          var elem = arr.shift();

          for (var j = 0; j < elem.length; j++) {

              var childperm = getCombinations(arr.slice(), n - 1);

              for (var k = 0; k < childperm.length; k++) {

                  ret.push([elem].concat(childperm));

              }

          }

      }

      return ret;

  }

}

function showAllArtLayers() {

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

    for(var z = 0; z < layerSets.artLayers.length; z++) {

      layerSets.artLayers.visible = true;

    }

  }

}

function hideAllArtLayers() {

  var layerSets = app.activeDocument.layerSets;

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

    if(layerSets.artLayers.length) {

      for(var z = 0; z < layerSets.artLayers.length; z++) {

        layerSets.artLayers.visible = false;

      }

    } else {

      for(var z = 0; z < layerSets.layerSets.length; z++) {

        layerSets.layerSets.visible = false;

      }

    }

  }

}

function getArtLayerCollectionCollection() {

  var layerSets = app.activeDocument.layerSets,

      artLayerCollectionCollection = [];

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

    var artlayerCollection = [];

    if(layerSets.artLayers.length) {

      for(var z = 0; z < layerSets.artLayers.length; z++) {

        if(layerSets.name.indexOf('__') !== 0)

          artlayerCollection.push(layerSets.artLayers);

      }

    } else {

      for(var z = 0; z < layerSets.layerSets.length; z++) {

        if(layerSets.name.indexOf('__') !== 0)

          artlayerCollection.push(layerSets.layerSets);

      }

    }

    artLayerCollectionCollection.push(artlayerCollection);

  }

  return artLayerCollectionCollection;

}

function combine() {

  var artLayerCollectionCollection = getArtLayerCollectionCollection(),

      artLayerCollectionCollectionCombinations = getCombinations(artLayerCollectionCollection, getLayerSetsCount()),

      continueConfirmation;

      if(! artLayerCollectionCollectionCombinations.length) return alert('Script has aborted. No combinations found. Please make sure no empty groups are present.');

      continueConfirmation = confirm(artLayerCollectionCollectionCombinations.length + " combinations found. Would you like to continue?");

      if(! continueConfirmation ) return alert('Script has been aborted.');

      savePath = Folder.selectDialog("Select an output folder");

      var includePSDFiles = confirm('Would you like to include corresponding PSD documents?')

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

        hideAllArtLayers();

        var artLayerNames = [];

        for(var z = 0; z < artLayerCollectionCollectionCombinations.length; z++) {

          var artLayer = artLayerCollectionCollectionCombinations;

          artLayer.visible = true;

          artLayerNames.push(artLayer.parent.name);

          artLayerNames.push(artLayer.name);

        }

        saveDocumentAsJPEG(savePath + '/' + normalizeSaveFileName(artLayerNames.join('')).substr(0, 254));

        if(includePSDFiles) saveDocumentAsPSD(savePath + '/' + normalizeSaveFileName(artLayer.parent.name + artLayerNames.join('')).substr(0, 254));

      }

}

function getSmallestLayerSetCount() {

  var count = null,

  layerSets = app.activeDocument.layerSets;

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

    var artLayers = layerSets.artLayers;

    if(count === null) count =  artLayers.length;

    if(artLayers.length < count) count = artLayers.length;

  }

  return 1;

}

function getLayerSetsCount() {

  var layerSets = app.activeDocument.layerSets,

      count = 0;

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

    if(layerSets.name.indexOf('__') !== 0) count++;

  }

  return count;

}

function normalizeSaveFileName(name) {

  return name;

}

function saveDocumentAsJPEG(path) {

  app.activeDocument.saveAs(new File(path), new JPEGSaveOptions());

}

function saveDocumentAsPSD(path) {

  app.activeDocument.saveAs(new File(path), new PhotoshopSaveOptions());

}

combine();

app.displayDialogs = userDisplayDialogsPref;

This topic has been closed for replies.

2 replies

JJMack
Community Expert
Community Expert
November 29, 2018

I only hack at scripting and you only show parts of scripts to me it look like only the file can be passed to the function and

"new JPEGSaveOptions()" would only generate a  JPEGSaveOptions object perhaps with default settings not the low quality setting you seem to want.

JJMack
JJMack
Community Expert
Community Expert
November 28, 2018

Is the 60% quality or 60% size what about metadata and color space conversion to SRGB.  Why do you want to use Save for Web to save Jpeg rather than save as. I also do not see a resize option in Adobe DOM for for exportdocument save for web options just see a quality setting  So if you want to reduce the image size you may need to add that to your script. I having problems with this web site today....

]]

JJMack
Participant
November 28, 2018

Thanks JJMack for your speedy reply.

I wish to Save For Web and at 60% quality for reduced filesize.

This is where I found the original code:

https://github.com/mechanicious/photoshopCompositionComposer

I have found this code for below, for SaveForWeb, but after many failed attempts I have been unable to implement it in the original code above.

function main(){ 

if(!documents.length) return; 

var Name = app.activeDocument.name.replace(/\.[^\.]+$/, '');  

var saveFile = File(Folder.desktop + "/" + Name + ".jpg"); 

if(saveFile.exists){ 

   if(!confirm("Overwrite existing document?")) return; 

    saveFile.remove(); 

    } 

SaveForWeb(saveFile,100); //change to 60 for 60% 

main(); 

function SaveForWeb(saveFile,jpegQuality) { 

var sfwOptions = new ExportOptionsSaveForWeb();  

   sfwOptions.format = SaveDocumentType.JPEG;  

   sfwOptions.includeProfile = false;  

   sfwOptions.interlaced = 0;  

   sfwOptions.optimized = true;  

   sfwOptions.quality = jpegQuality; //0-100  

activeDocument.exportDocument(saveFile, ExportType.SAVEFORWEB, sfwOptions); 

JJMack
Community Expert
Community Expert
November 28, 2018

Try Changing:
SaveForWeb(saveFile,100); //change to 60 for 60%

to

SaveForWeb(saveFile,60); //change to 60 for 60%

like the comment shows

JJMack