23 Replies Latest reply on Apr 10, 2017 8:08 AM by c.pfaffenbichler

    Export layers and Save as

    WikVerto

      I have image.psd file with base1, base2, base3, base4, base5 layers and variation1, variation2, ..., variation80 layers. I need to export images and save them as imagebase1variation1.jpg, ..., imagebase1variation80.jpg and the same for each base layer + each variation layer. What is the most efficient way to do that in Photoshop CS5? (And I need to repeat this task for the rest of my 200 .psd images!)

        • 1. Re: Export layers and Save as
          c.pfaffenbichler Level 9

          Are Layer Groups involved or are all the Layers at the top level?

          • 2. Re: Export layers and Save as
            WikVerto Level 1

            I can ungroup them, it does not matter.

            • 3. Re: Export layers and Save as
              c.pfaffenbichler Level 9

              It matters when it concerns 200 files, I guess.

              But it can be taken care of in the Script, so don’t bother about it yet.

              1 person found this helpful
              • 4. Re: Export layers and Save as
                WikVerto Level 1

                And I cannot find out how to create a script that exports jpg-image in format original image+layer name+layer name.jpg. Is it even possible?

                • 5. Re: Export layers and Save as
                  c.pfaffenbichler Level 9

                  I have not included checks for all potential problems (for example if you were to have two Layers names »base1« the resulting files would be overwritten without warning and numbers will only be considered to a length of 5 spaces).

                  But you could give it a try with some duplicate files.

                  If you want to give it a try, paste the following text into a new file in ExtendScript Toolkit (part of Photoshop’s installation, Applications/Utilities/Adobe Utilities/ExtendScript Toolkit CS4 or /Applications/Utilities/Adobe Utilities-CS5/ExtendScript Toolkit CS5) and save it as a jsx-file into Photoshop’s Presets/Scripts-folder.

                  After restarting Photoshop the Script should be available under File > Scripts and can be assigned a Keyboard Shortcut directly, recorded into an Action, (in CS4 and CS5) be used in a Configurator-Panel or started from ExtendScript Toolkit directly.

                   

                  Edited:

                  // combine base with variation layers and save jpgs;
                  // 2012, use at your own risk;
                  #target photoshop
                  if (app.documents.length > 0) {
                  var myDocument = app.activeDocument;
                  var docName = myDocument.name;
                  var basename = docName.match(/(.*)\.[^\.]+$/)[1];
                  var docPath = myDocument.path;
                  // jpg options;
                  var jpegOptions = new JPEGSaveOptions();
                  jpegOptions.quality = 9;
                  jpegOptions.embedColorProfile = true;
                  jpegOptions.matte = MatteType.NONE;
                  // get layers;
                  var theLayers = collectLayers(app.activeDocument, [[], []]);
                  var base = theLayers[0];
                  var variation = theLayers[1];
                  // create the combinations;
                  for (var m = 0; m < base.length; m++) {
                            base[m].visible = true;
                            for (var n = 0; n < variation.length; n++) {
                                      variation[n].visible = true;
                                      myDocument.saveAs((new File(docPath+"/image"+ base[m].name + variation[n].name +".jpg")), jpegOptions, true);
                                      variation[n].visible = false;
                                      }
                            base[m].visible = false;
                            };
                  };
                  ////// function collect all layers //////
                  function collectLayers (theParent, allLayers) {
                            if (!allLayers) {var allLayers = new Array} 
                            else {};
                            for (var m = theParent.layers.length - 1; m >= 0;m--) {
                                      var theLayer = theParent.layers[m];
                  // apply the function to layersets;
                                      if (theLayer.typename == "ArtLayer") {
                                                if (theLayer.name.match(/^base\d{1,5}$/)) {allLayers[0].push(theLayer)};
                                                if (theLayer.name.match(/^variation\d{1,5}$/)) {allLayers[1].push(theLayer)};
                                                theLayer.visible = false
                                                }
                                      else {
                                                allLayers = (collectLayers(theLayer, allLayers))
                                                }
                                      };
                            return allLayers
                            };
                  
                  • 6. Re: Export layers and Save as
                    WikVerto Level 1

                    I cannot thank you enough! I tried your script and it works just fine! I need to do some adjustments and it`s all set to get this project done!!!!

                    • 7. Re: Export layers and Save as
                      c.pfaffenbichler Level 9

                      Please note that I edited the lines

                       

                      if (theLayer.name.match(/^base\d{1,5}$/)) {allLayers[0].push(theLayer)};
                      if (theLayer.name.match(/^variation\d{1,5}$/)) {allLayers[1].push(theLayer)};

                       

                      Previously they would also have accepted for example »base_2«, now they should only accept Layers with names that have the number directly after the name element.

                      • 8. Re: Export layers and Save as
                        c.pfaffenbichler Level 9

                        Another warning:

                        As the jpgs are currently being named only after the Layers and not the Files’ names they would overwrite one another if more than one file with identically named layers in one Folder were processed.

                        • 9. Re: Export layers and Save as
                          Babymac08 Level 1

                          Any chance you can simplify this even more for me... I need to simply save each individual layer as a file... The stock script from Adobe to Export to Layers... is way to complicated and requires interaction... I need a slick simple script and your's seems to be a good base to work from... Any help?

                           

                          Thanks

                          • 10. Re: Export layers and Save as
                            c.pfaffenbichler Level 9

                            There is no check for identical names or existing files of the same name, so those would be overwritten.

                            // save jpgs of layers;

                            // 2017, use at your own risk; 

                            #target photoshop 

                            if (app.documents.length > 0) { 

                            var myDocument = app.activeDocument; 

                            var docName = myDocument.name; 

                            var basename = docName.match(/(.*)\.[^\.]+$/)[1]; 

                            var docPath = myDocument.path; 

                            // jpg options; 

                            var jpegOptions = new JPEGSaveOptions(); 

                            jpegOptions.quality = 9; 

                            jpegOptions.embedColorProfile = true; 

                            jpegOptions.matte = MatteType.NONE; 

                            // get layers;

                            var theLayers = collectLayers(app.activeDocument, []); 

                            // create the combinations; 

                            for (var m = 0; m < theLayers.length; m++) { 

                                      theLayers[m].visible = true;

                                      myDocument.saveAs((new File(docPath+"/"+ docName + "_" + theLayers[m].name.replace("/","_") + ".jpg")), jpegOptions, true); 

                                      theLayers[m].visible = false; 

                                      };

                            };

                            ////// function collect all layers //////

                            function collectLayers (theParent, allLayers) {

                                      if (!allLayers) {var allLayers = new Array}

                                      else {};

                                      for (var m = theParent.layers.length - 1; m >= 0;m--) {

                                                var theLayer = theParent.layers[m];

                            // apply the function to layersets;

                                                if (theLayer.typename == "ArtLayer") {

                                                          allLayers.push(theLayer);

                                                          theLayer.visible = false

                                                          }

                                                else {

                                                          theLayer.visible = true

                                                          allLayers = (collectLayers(theLayer, allLayers))

                                                          }

                                                };

                                      return allLayers

                                      };

                            • 11. Re: Export layers and Save as
                              Babymac08 Level 1

                              @c.pfaffenbichler  is it possible to adjust the above script to save as a .tif file instead...???  Appreciate any help you can offer... Thanks

                              • 12. Re: Export layers and Save as
                                c.pfaffenbichler Level 9

                                You can create tif-options instead of jpg-options and then change the saving-line accordingly (the suffix and the reference to the options).

                                tifOpts = new TiffSaveOptions();

                                tifOpts.embedColorProfile = true;

                                tifOpts.imageCompression = TIFFEncoding.TIFFLZW;

                                tifOpts.alphaChannels = false;

                                tifOpts.byteOrder = ByteOrder.MACOS;

                                tifOpts.layers = false;

                                • 13. Re: Export layers and Save as
                                  Babymac08 Level 1

                                  c.pfaffenbichler  Sorry it's been a bit since I've had the opportunity to tackle this... I applied the settings you provided for saving as tifs, but I keep getting an error... It's stopping on the following line Marked with ****** below, any idea of why? I'm assuming this will work no matter if the document has 1 layer or multiple...???

                                   

                                   

                                  // save tif of layers;

                                   

                                   

                                  #target photoshop

                                  if (app.documents.length > 0) {

                                  var myDocument = app.activeDocument;

                                  ****** This one *************  var docName = myDocument.name;

                                  ****** This one ************* var basename = docName.match(/(.*)\.[^\.]+$/)[1];

                                  var docPath = myDocument.path;

                                   

                                   

                                  // tif options;

                                  var tifOpts = new TiffSaveOptions();

                                  tifOpts.embedColorProfile = true;

                                  tifOpts.imageCompression = TIFFEncoding.TIFFLZW;

                                  tifOpts.alphaChannels = false;

                                  tifOpts.byteOrder = ByteOrder.IBM;

                                  tifOpts.layers = false;

                                   

                                   

                                  // get layers;

                                  var theLayers = collectLayers(app.activeDocument, []);

                                   

                                   

                                  // create the combinations;

                                  for (var m = 0; m < theLayers.length; m++) {

                                            theLayers[m].visible = true;

                                            myDocument.saveAs((new File(docName + "_" + theLayers[m].name.replace("/","_") + ".tif")), tifOpts, true);

                                            theLayers[m].visible = false;

                                            };

                                  };

                                   

                                   

                                  ////// function collect all layers //////

                                  function collectLayers (theParent, allLayers) {

                                            if (!allLayers) {var allLayers = new Array}

                                            else {};

                                            for (var m = theParent.layers.length - 1; m >= 0;m--) {

                                                      var theLayer = theParent.layers[m];

                                  // apply the function to layersets;

                                                      if (theLayer.typename == "ArtLayer") {

                                                                allLayers.push(theLayer);

                                                                theLayer.visible = false

                                                                }

                                                      else {

                                                                theLayer.visible = true

                                                                allLayers = (collectLayers(theLayer, allLayers))

                                                                }

                                                      };

                                            return allLayers

                                            };

                                  • 14. Re: Export layers and Save as
                                    c.pfaffenbichler Level 9

                                    What is the document’s name?

                                    Has the document been saved at some point?

                                    What exact error occurs?

                                    • 15. Re: Export layers and Save as
                                      Babymac08 Level 1

                                      What my overall plan is will be adding this to the bottom of another script that will watch a hot folder and open a .pdf (Single or Multi Page) then jump to this script and save each layer as a flattened .tif... When I'm running the script by itself from the script editor it stops and those lines turn orange.  I'll attach both scripts below so you can see what the ultimate plan is...

                                       

                                      // opens all pages of pdfs cropped to trimbox with set settings; 

                                      // 2011, use it at your own risk; 

                                      #target photoshop 

                                      var pdfOpenOpts = new PDFOpenOptions; 

                                      pdfOpenOpts.antiAlias = true; 

                                      pdfOpenOpts.bitsPerChannel = BitsPerChannelType.EIGHT; 

                                      pdfOpenOpts.cropPage = CropToType.MEDIABOX; 

                                      pdfOpenOpts.mode = OpenDocumentMode.CMYK; 

                                      pdfOpenOpts.resolution = 300; 

                                      pdfOpenOpts.suppressWarnings = true; 

                                      pdfOpenOpts.usePageNumber  = true; 

                                      // dialog for pdf-selection; 

                                      var theFiles = app.openDialog(); 

                                      if (theFiles) { 

                                      // change pref; 

                                           var originalRulerUnits = app.preferences.rulerUnits; 

                                      // change pref; 

                                           var originalRulerUnits = app.preferences.rulerUnits; 

                                           app.preferences.rulerUnits = Units.INCHES; 

                                           for (var m = 0; m < theFiles.length; m++) { 

                                                var theFile = theFiles[m]; 

                                                if (theFile.name.slice(-4) == ".pdf") { 

                                                     var thePdf = openMultipagePDF(theFile); 

                                                          }; 

                                                     } 

                                                } 

                                      // reset pref; 

                                           app.preferences.rulerUnits = originalRulerUnits; 

                                       

                                      function openMultipagePDF(myPDFFile) { 

                                      // suppress dialogs; 

                                           var theDialogSettings = app.displayDialogs; 

                                           app.displayDialogs = DialogModes.NO; 

                                           var myCounter = 1; 

                                           var myBreak = false; 

                                           while(myBreak == false){ 

                                                pdfOpenOpts.page = myCounter; 

                                                try { 

                                                     var thePdf = app.open(myPDFFile, pdfOpenOpts); 

                                                     thePdf.flatten(); 

                                                     thePdf.layers[0].isBackgroundLayer = false; 

                                                     //thePdf.layers[0].name = myPDFFile.name+"_"+myCounter; 

                                                     if (myCounter == 1) { 

                                                          var theFile = thePdf 

                                                          } 

                                                     else { 

                                                          thePdf.layers[0].duplicate(theFile, ElementPlacement.PLACEATBEGINNING); 

                                                          thePdf.close(SaveOptions.DONOTSAVECHANGES) 

                                                          } 

                                                     } 

                                                catch (e) {myBreak = true}; 

                                                myCounter = myCounter + 1; 

                                                }; 

                                      // reset dialogmodes; 

                                           app.displayDialogs = DialogModes.ERROR; 

                                           return app.activeDocument 

                                           }; 

                                      *******Your script starts below **************

                                      // save tif of layers;

                                      #target photoshop

                                      if (app.documents.length > 0) {

                                      var myDocument = app.activeDocument;

                                      var docName = myDocument.name;

                                      var basename = docName.match(/(.*)\.[^\.]+$/)[1];

                                      var docPath = myDocument.path;

                                       

                                       

                                      // tif options;

                                      var tifOpts = new TiffSaveOptions();

                                      tifOpts.embedColorProfile = true;

                                      tifOpts.imageCompression = TIFFEncoding.TIFFLZW;

                                      tifOpts.alphaChannels = false;

                                      tifOpts.byteOrder = ByteOrder.IBM;

                                      tifOpts.layers = false;

                                       

                                       

                                      // get layers;

                                      var theLayers = collectLayers(app.activeDocument, []);

                                       

                                       

                                      // create the combinations;

                                      for (var m = 0; m < theLayers.length; m++) {

                                                theLayers[m].visible = true;

                                                myDocument.saveAs((new File(docPath+"/"+ docName + "_" + theLayers[m].name.replace("/","_") + ".tif")), tifOpts, true);

                                                theLayers[m].visible = false;

                                                };

                                      };

                                      ////// function collect all layers //////

                                      function collectLayers (theParent, allLayers) {

                                                if (!allLayers) {var allLayers = new Array}

                                                else {};

                                                for (var m = theParent.layers.length - 1; m >= 0;m--) {

                                                          var theLayer = theParent.layers[m];

                                      // apply the function to layersets;

                                                          if (theLayer.typename == "ArtLayer") {

                                                                    allLayers.push(theLayer);

                                                                    theLayer.visible = false

                                                                    }

                                                          else {

                                                                    theLayer.visible = true

                                                                    allLayers = (collectLayers(theLayer, allLayers))

                                                                    }

                                                          };

                                                return allLayers

                                                };

                                      • 16. Re: Export layers and Save as
                                        c.pfaffenbichler Level 9

                                        There is your problem, I guess – a file that has been created by converting a pdf is new, it has no suffix yet.

                                        What happens when you remove the line

                                        var basename = docName.match(/(.*)\.[^\.]+$/)[1];

                                        ?

                                        • 17. Re: Export layers and Save as
                                          Babymac08 Level 1

                                          Well interesting... I had never combined the two elements yet to run as one.. I was just trying to get the tiff save portion working... However, I removed the line you suggested and went ahead and ran the combined script and it seemed to work... Thanks for your help...

                                          • 18. Re: Export layers and Save as
                                            Babymac08 Level 1

                                            OK... I must have gotten lucky the first time, because now it's stopping at the

                                            var docPath = myDocument.path;

                                             

                                            Line... Any ideas?

                                            • 19. Re: Export layers and Save as
                                              c.pfaffenbichler Level 9

                                              If the file is newly created and has never been saved it has no path.

                                              • 20. Re: Export layers and Save as
                                                Babymac08 Level 1

                                                I think I gather what your saying, but I'm not sure how to go about fixing... any suggestions...??

                                                 

                                                Thanks

                                                • 21. Re: Export layers and Save as
                                                  c.pfaffenbichler Level 9

                                                  You could use the pdf-file’s original path.

                                                  • 22. Re: Export layers and Save as
                                                    Babymac08 Level 1

                                                    Where would I insert this into the script to satisfy the function

                                                    (~/Desktop/Test)

                                                     

                                                    Thanks

                                                    • 23. Re: Export layers and Save as
                                                      c.pfaffenbichler Level 9

                                                      I recommend that you stop riding old threads and start a new one.

                                                       

                                                      (~/Desktop/Test)

                                                      That is not a String.

                                                      In any case

                                                      docPath

                                                      is used in the line

                                                                myDocument.saveAs((new File(docPath+"/"+ docName + "_" + theLayers[m].name.replace("/","_") + ".tif")), tifOpts, true);