Skip to main content
Participant
March 2, 2009
Question

Batch Export Separate Layer with the same Background Layer , Please HELP

  • March 2, 2009
  • 8 replies
  • 10471 views
I have the code as below, it worked in order saving separate layer to JPG, but it didn't include a Background layer to the JPGs which were exported.

Please help: I'd like all images exported has separate layer AND the same background.

Thanks in advanced.

============================================
// define your variables
var doc = app.activeDocument;
var artlayers = new Array();

// define your jpeg options
var options = new ExportOptionsSaveForWeb();
options.quality = 80; // Start with highest quality (biggest file).
options.format = SaveDocumentType.JPEG; // Or it'll default to GIF.

// loop through all layers
var x = 0;
var a = 0;
while(x < doc.layers.length)
{
// check if the layer is a item
if (doc.layers.kind == LayerKind.NORMAL)
{
// add to array
artlayers = x;
a++;
}
x++;
}

// loop through our array of layers
// each loop will export a jpeg with 1 text layer visible and hiding the others
var x = 0;
var a = 0;
while (a < artlayers.length)
{
while (x < artlayers.length)
{
if (a == artlayers)
{
doc.layers[artlayers].visible = true;
} else {
doc.layers[artlayers].visible = false;
}

x++;
}

// export as jpeg
doc.exportDocument(new File("/c/variation_"+ a +".jpg"), ExportType.SAVEFORWEB, options)

x = 0;
a++
}
=============================================
This topic has been closed for replies.

8 replies

Participant
November 22, 2010

Hello, just found this post and trying to do the same thing. Does anyone have the final script with the merge background and save file with layer file name added. Cannot seem to get it to work by piecing it together?

Thanks in advance.

Participant
May 27, 2011

// define your variables

var destination = "C:/path/here/"

var doc = app.activeDocument;

var artlayers = new Array();

// define your jpeg options

var options = new ExportOptionsSaveForWeb();

options.quality = 65; // Start with highest quality (biggest file).

options.format = SaveDocumentType.JPEG; // Or it'll default to GIF.

// loop through all layers

var x = 0;

var a = 0;

while(x < doc.layers.length)

{

// check if the layer is a item

if (doc.layers.kind == LayerKind.NORMAL)

{

// add to array

artlayers = x;

a++;

}

x++;

}

// loop through our array of layers

// each loop will export a jpeg with 1 text layer visible and hiding the others

var x = 0;

var a = 0;

while (a < artlayers.length)

{

while (x < artlayers.length)

{

if (a == artlayers)

{

doc.layers[artlayers].visible = true;

} else {

doc.layers[artlayers].visible = false;

}

x++;

}

// export as jpeg

try { doc.backgroundLayer.visible = true; } catch (e) {}

file = new File(destination + doc.layers[artlayers].name +".jpg");

doc.exportDocument(file, ExportType.SAVEFORWEB, options);

x = 0;

a++

}

============================

// loop through our array of layers

// each loop will export a jpeg with 1 text layer visible and hiding the others

with the following loop:

=======================

for  (x=0; x < artlayers.length; x++)

{

doc.layers[artlayers].visible = true;

// export as jpeg

try { doc.backgroundLayer.visible = true; } catch (e) {}

file = new File(destination + doc.layers[artlayers].name +".jpg");

doc.exportDocument(file, ExportType.SAVEFORWEB, options);

doc.layers[artlayers].visible = false;

}

==========================

the first loop runs in O(n^2), while the second loop runs in O(n) [http://en.wikipedia.org/wiki/Big_O_notation]

also, be sure that what you're calling the background layer is what photoshop is calling the background (you can tell because the word Background will be in italics).  if it's not an official background layer, you can make it so by selecting the layer, then going to Layer -> New -> Background From Layer

Participant
March 29, 2013

I used your script to save layers all with the same background. The problem I have is when I call out the command it tells me the destination folder does not exist.

Being new to the scripting world I tried the original script posted before yours and at the bottom of this post in bold and it said the same thing.

I set the destination folders as below and not sure what is going on?

// define your variables


var destination = "C:/Desktop/Layerfolder/"


var doc = app.activeDocument;

var artlayers = new Array();


// define your jpeg options

var options = new ExportOptionsSaveForWeb();

options.quality = 65; // Start with highest quality (biggest file).

options.format = SaveDocumentType.JPEG; // Or it'll default to GIF.


// loop through all layers

var x = 0;

var a = 0;

while(x < doc.layers.length)

{

// check if the layer is a item

if (doc.layers.kind == LayerKind.NORMAL)

{

// add to array

artlayers = x;

a++;

}

x++;

}


// loop through our array of layers

// each loop will export a jpeg with 1 text layer visible and hiding the others

var x = 0;

var a = 0;

while (a < artlayers.length)

{

while (x < artlayers.length)

{

if (a == artlayers)

{

doc.layers[artlayers].visible = true;

} else {

doc.layers[artlayers].visible = false;

}


x++;

}


// export as jpeg

try { doc.backgroundLayer.visible = true; } catch (e) {}

file = new File(destination + doc.layers[artlayers].name +".jpg");

doc.exportDocument(file, ExportType.SAVEFORWEB, options);


x = 0;

a++

}

// define your variables
var doc = app.activeDocument;
var artlayers = new Array();

// define your jpeg options
var options = new ExportOptionsSaveForWeb();
options.quality = 80; // Start with highest quality (biggest file).
options.format = SaveDocumentType.JPEG; // Or it'll default to GIF.

// loop through all layers
var x = 0;
var a = 0;
while(x < doc.layers.length)
{
// check if the layer is a item
if (doc.layers.kind == LayerKind.NORMAL)
{
// add to array
artlayers
= x;
a++;
}
x++;
}

// loop through our array of layers
// each loop will export a jpeg with 1 text layer visible and hiding the others
var x = 0;
var a = 0;
while (a < artlayers.length)
{
while (x < artlayers.length)
{
if (a == artlayers)
{
doc.layers[artlayers].visible = true;
} else {
doc.layers[artlayers].visible = false;
}

x++;
}
try { doc.backgroundLayer.visible = true; } catch (e) {}
// export as jpeg
doc.exportDocument(new File("/c/variation_"+ a +".jpg"), ExportType.SAVEFORWEB, options)

x = 0;
a++
}v
// JavaScript Document

Participant
April 2, 2009
Would I be able to use this script to export a layer then combined it with the background layer without having to do it manually for each layer, then save each layer into a PDF?
Participant
April 2, 2009
I would like to use this script, but instead of separate layers, I'd like to save separate groups as files (jpg) and keep the background.
Participant
March 21, 2009
Hey Guys,
Where do I save this and how do I run it?
I tried saving it as a jsx file and running it by going to File / Scripts / Browse
Didn't work.

Bascially trying to export all my layers to files, and keep my top layer applied when the jpegs are saved out-- which will be a border on all the layers below it

Any help is appreciated.
Thanks!
Ken
Participant
August 21, 2013

Hi Ken, did you find an answer to your question about keeping the top layer while exporting others to jpegs? Bit confused about all this jsx stuff?

Many thanks,

Dan

Participant
August 21, 2013

Hi did you put the jsx in the appropriate Photoshop folder ?

Sent from my iPhone

c.pfaffenbichler
Community Expert
Community Expert
March 6, 2009
Maybe:
doc.exportDocument(new File("/c/"+ doc.layers[artlayers].name +".jpg"), ExportType.SAVEFORWEB, options)
?
Participant
March 3, 2009
Any idea if I 'd like to save them as "layer name.jpg"
Participant
March 3, 2009
WOW, It really works. Thank you very much, you are pro. :D
Known Participant
March 2, 2009
> Please help: I'd like all images exported has separate layer AND the same background.

Add this just before the exportDocument line:

try { doc.backgroundLayer.visible = true; } catch (e) {}

-X