1 Reply Latest reply on Mar 29, 2017 9:52 PM by Chuck Uebele

    Multiple Save Loops

    clayd85047281

      Hello. I need some help. I'm using Photoshop CC 2015.

       

      Here is a look at my layer groups. Contact Information and Pricing are my two Top Level Layer Groups. Below those you will see sub layers of locations and pricing tiers.

       

      I am trying to automate to process of exporting every permutation of Contact Information and Pricing as a pdf.

       

       

      I am half way to my ideal solution by using the below piece of code that I found elsewhere in the forums.

       

      Problem #1:  Using this code I can keep one Layer Group constant and cycle through permutations of second Layer Group. So if I select Platinum from the Pricing group, it will return all of the Contact Information layers for Platinum. But then it stops. I want to modify the code so that when it's done with Platinum, it moves on to the next layer and returns all permutations for Gold, then Silver then Standard. So basically I need a loop within a loop.

       

      Problem #2:  The saved pdf file size is way too big, 30MB. I created a Save Preference in Adobe and called it Binder. I tried to plug that preference into the script, but I cant get it to pick up.

       

       

      Thanks all

       

      __________________________________________________________________________________________ ________________________________________

       

       

      // Name: Export Layers Inside Selected Group.jsx

      // Description: Photoshop script that separately saves top level layers inside the selected group.

      // https://gist.github.com/joonaspaakko/013a223e94ba0fb9a2a0

       

      #target photoshop

       

      try {

          var doc = app.activeDocument;

          var docName = doc.name.split('.')[0];

      }

      catch (e) {

          alert( 'Open a document first...' );

      }

       

      function init() {

       

          var savefiles;

       

          dlg.g.saveAs.minimumSize.width = 463;

          dlg.btns.minimumSize.height = 142;

       

       

          dlg.btns.save.onClick = function(){

             savefiles = true;

             dlg.close();

             return savefiles;

          };

       

          dlg.show();

       

          if ( savefiles ){

       

              var getDestination = Folder.selectDialog( 'Select destination folder...', doc.saved ? doc.path : '' );

              var group = doc.activeLayer;

              var groupLength = group.layers.length;

       

              for( var i = 0 ; i < groupLength; i++ ){

                  group.layers[i].visible = false;

              }

       

              for( var i = 0 ; i < groupLength; i++ ){

       

                  var layer = group.layers[ i ];

                  var layerIndex = i+1;

       

                  layer.visible = true;

       

                  save.file( dlg, doc, getDestination, layerIndex );

       

                  layer.visible = false;

       

              }

       

              alert('Files Saved!');

       

          }

       

      }

       

      var save = {

          file: function( dlg, doc, getDestination, layerIndex ) {

       

              var saveOptions = {};

              var formats = ["psd", "pdf", "png", "jpg", "tiff"];

       

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

                  if ( dlg.g.saveAs[ formats[i] ].value ) {

       

                      var fileformat = formats[i];

       

                      var path = getDestination + "/" + fileformat;

       

                      makeFolder( path );

       

                      doc.saveAs( File( path + "/" + dlg.g.filename.filename.text + activeDocument.activeLayer.layers[layerIndex-1].name ), save[fileformat](), true );

       

                  }

              }

       

          },

          psd: function() {

       

              var psd_saveOpts = new PhotoshopSaveOptions();

       

              psd_saveOpts.layers = true;

              psd_saveOpts.embedColorProfile = true;

              psd_saveOpts.annotations = true;

              psd_saveOpts.alphaChannels = true;

       

              return psd_saveOpts;

       

          },

          pdf: function() {

       

              var presetName = '[Binder]';

       

              var pdf_SaveOpts = new PDFSaveOptions();

       

              pdf_SaveOpts.pDFPreset = presetName;

       

              return pdf_SaveOpts;

       

          },

          jpg: function() {

       

              var jpg_SaveOpts = new JPEGSaveOptions();

       

              jpg_SaveOpts.matte = MatteType.WHITE;

              jpg_SaveOpts.quality = 10;

              jpg_SaveOpts.formatOptions.STANDARDBASELINE;

       

              return jpg_SaveOpts;

       

          },

          png: function() {

       

              var png_SaveOpts = new PNGSaveOptions();

       

              png_SaveOpts.compression = 9;

              png_SaveOpts.interlaced = false;

       

              return png_SaveOpts;

       

          },

          tiff: function() {

       

              var tiff_SaveOpts = new TiffSaveOptions();

       

              tiff_SaveOpts.alphaChannels = true;

              tiff_SaveOpts.annotations = true;

              tiff_SaveOpts.imageCompression = TIFFEncoding.JPEG;

              tiff_SaveOpts.interleaveChannels = true;

              tiff_SaveOpts.jpegQuality = 10;

              tiff_SaveOpts.layers = true;

              tiff_SaveOpts.layerCompression = LayerCompression.ZIP;

              tiff_SaveOpts.transparency = true;

       

              return tiff_SaveOpts;

       

          }

      };

       

      // Prepare dialog...

      var dlg = new Window("dialog {  \

          text: 'Export layers inside the selected group', \

          alignChildren:['left','center'], \

          orientation: 'row', \

          g: Group { \

              orientation:'column', \

              alignChildren: ['left','center'], \

              filename: Panel { \

                  orientation:'column', \

                  alignChildren: ['left','top'], \

                  filename_text: StaticText { alignment:'left', text: 'Filename ( Incremental numbers added automatically ): '}, \

                  filename: EditText { alignment:'left', preferredSize: [430,20], text: '"+ docName +"', active: true },  \

              }, \

              saveAs: Panel { \

                  margins: 20, \

                  spacing: 20, \

                  orientation: 'row', \

                  alignChildren: ['left','top'], \

                  saveAs_txt: StaticText { text: 'Save as: '}, \

                  jpg: Checkbox { text: 'jpg', value: true }, \

                  psd: Checkbox { text: 'psd', value: false }, \

                  pdf: Checkbox { text: 'pdf', value: false }, \

                  png: Checkbox { text: 'png', value: false }, \

                  tiff: Checkbox { text: 'tiff', value: false } \

              } \

          }, \

          btns: Panel { \

              margins: 20, \

              spacing: 20, \

              orientation: 'column',  \

              alignment: ['right','top'], \

              save: Button { text: 'Save', properties:{ name: 'ok' }, preferredSize:[88, 24] }, \

              cancel: Button { text: 'Cancel', properties:{ name: 'cancel' }, preferredSize:[88, 24] }, \

          } \

      }");

       

      function makeFolder( path ) {

       

          var newFolder = Folder( path );

          if( !newFolder.exists ) newFolder.create();

       

      }

       

      if ( app.documents.length > 0 ) {

          if ( app.activeDocument.activeLayer.layers ) {

       

                  init();

       

          }

          else {

              alert( "Error: \nSelect a parent group of the layers you want to export.")

          }

      }

        • 1. Re: Multiple Save Loops
          Chuck Uebele Adobe Community Professional & MVP

          You need to make your for loop recursive. So you put in an if statement to check if the any of the groups layers are layersets. If so, you call the same function.

           

          var doc = activeDocument;
          processLayers (doc)
          function processLayers(gp){
              for(var i=0;i<gp.layers.length;i++){
                  if(gp.layers[i].typename=='LayerSet'){
                      processLayers (gp.layers[i])
                      }//end if
                  else{
                      //put your code here to process regular layers
                      }//end else
                  }//end for loop
              }//end function
          
          1 person found this helpful