Skip to main content
Participant
April 24, 2012
Answered

Export layers and Save as

  • April 24, 2012
  • 4 replies
  • 3462 views

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!)

This topic has been closed for replies.
Correct answer c.pfaffenbichler

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.visible = true;

          for (var n = 0; n < variation.length; n++) {

                    variation.visible = true;

                    myDocument.saveAs((new File(docPath+"/image"+ base.name + variation.name +".jpg")), jpegOptions, true);

                    variation.visible = false;

                    }

          base.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;

// 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

          };

4 replies

Babymac08
Known Participant
April 7, 2017

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

          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.visible = true;

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

          theLayers.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;

// 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

          };

c.pfaffenbichler
Community Expert
Community Expert
April 7, 2017

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];

?

Babymac08
Known Participant
April 7, 2017

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

var docPath = myDocument.path;

Line... Any ideas?

Babymac08
Known Participant
March 20, 2017

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

c.pfaffenbichler
Community Expert
Community Expert
March 20, 2017

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;

Babymac08
Known Participant
April 7, 2017

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.visible = true;

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

          theLayers.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;

// 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

          };

WikVertoAuthor
Participant
April 24, 2012

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?

c.pfaffenbichler
Community Expert
c.pfaffenbichlerCommunity ExpertCorrect answer
Community Expert
April 25, 2012

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.visible = true;

          for (var n = 0; n < variation.length; n++) {

                    variation.visible = true;

                    myDocument.saveAs((new File(docPath+"/image"+ base.name + variation.name +".jpg")), jpegOptions, true);

                    variation.visible = false;

                    }

          base.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;

// 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

          };

WikVertoAuthor
Participant
April 25, 2012

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!!!!

c.pfaffenbichler
Community Expert
Community Expert
April 24, 2012

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

WikVertoAuthor
Participant
April 24, 2012

I can ungroup them, it does not matter.

c.pfaffenbichler
Community Expert
Community Expert
April 24, 2012

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.