Skip to content

Instantly share code, notes, and snippets.

@sambatlim
Created July 25, 2021 07:30
Show Gist options
  • Save sambatlim/3d145f4dc4b2f5d8ef641b18a39ac93c to your computer and use it in GitHub Desktop.
Save sambatlim/3d145f4dc4b2f5d8ef641b18a39ac93c to your computer and use it in GitHub Desktop.
Script to generate save image from each group in photoshop.
function Main() {
var num = 1;
for (var j = 0; j < app.activeDocument.layerSets[0].layers.length; j++) {
for (var k = 0; k < app.activeDocument.layerSets[1].layers.length; k++) {
for (var l = 0; l < app.activeDocument.layerSets[2].layers.length; l++) {
var groupChildArr1 = app.activeDocument.layerSets[0].layers;
var groupChildArr2 = app.activeDocument.layerSets[1].layers;
var groupChildArr3 = app.activeDocument.layerSets[2].layers;
app.activeDocument.layerSets[0].visible = true;
app.activeDocument.layerSets[1].visible = true;
app.activeDocument.layerSets[2].visible = true;
groupChildArr1[j].visible = true;
groupChildArr2[k].visible = true;
groupChildArr3[l].visible = true;
Save(num);
num++;
Revert();
}
}
}
}
function Save(num) {
var outFolder = app.activeDocument; // psd name
var outPath = outFolder.path;
var fName = "PNG"; // define folder name
var f = new Folder(outPath + "/" + fName);
if (!f.exists) {
f.create()
}
var saveFile = new File(outPath + "/" + fName + "/" + "Pattern_" + num + ".png");
pngSaveOptions = new PNGSaveOptions();
pngSaveOptions.interlaced = false;
app.activeDocument.saveAs(saveFile, pngSaveOptions, true, Extension.LOWERCASE);
}
function Revert() {
var idRvrt = charIDToTypeID("Rvrt");
executeAction(idRvrt, undefined, DialogModes.NO);
}
Main()
@bybyspalma
Copy link

me pasa que aparece el cuadro para guardar psd pero no guarda ningún png

@sambatlim
Copy link
Author

Please translate to English.

@bybyspalma
Copy link

when I run the script I get the dialog box to save a psd and from there I have to do the manual process to save png, I need your help please thanks

@bybyspalma
Copy link

function Main() {

var num = 1;


for (var j = 0; j < app.activeDocument.layerSets[0].layers.length; j++) {
    for (var k = 0; k < app.activeDocument.layerSets[1].layers.length; k++) {
        for (var l = 0; l < app.activeDocument.layerSets[2].layers.length; l++) {
	for (var m = 0; m < app.activeDocument.layerSets[3].layers.length; m++) {
  		  for (var n = 0; n < app.activeDocument.layerSets[4].layers.length; n++) {
  		      for (var o = 0; o < app.activeDocument.layerSets[5].layers.length; o++) {
		

			
            var groupChildArr1 = app.activeDocument.layerSets[0].layers;
            var groupChildArr2 = app.activeDocument.layerSets[1].layers;
            var groupChildArr3 = app.activeDocument.layerSets[2].layers;
	var groupChildArr4 = app.activeDocument.layerSets[3].layers;
            var groupChildArr5 = app.activeDocument.layerSets[4].layers;
            var groupChildArr6 = app.activeDocument.layerSets[5].layers;
	
			
            app.activeDocument.layerSets[0].visible = true;
            app.activeDocument.layerSets[1].visible = true;
            app.activeDocument.layerSets[2].visible = true;
	app.activeDocument.layerSets[3].visible = true;
            app.activeDocument.layerSets[4].visible = true;
            app.activeDocument.layerSets[5].visible = true;
	
			
            groupChildArr1[j].visible = true;
            groupChildArr2[k].visible = true;
            groupChildArr3[l].visible = true;
	groupChildArr4[m].visible = true;
            groupChildArr5[n].visible = true;
            groupChildArr6[o].visible = true;
	
		
            Save(num);
            num++;
            Revert();
        }
      }
  
}

}
}
}

}

function Save(num) {
var outFolder = app.activeDocument; // psd name
var outPath = outFolder.path;
var fName = "PNG"; // define folder name
var f = new Folder(outPath + "/" + fName);
if (!f.exists) {
f.create()
}
var saveFile = new File(outPath + "/" + fName + "/" + "Pattern_" + num + ".png");
pngSaveOptions = new PNGSaveOptions();
pngSaveOptions.interlaced = false;
app.activeDocument.saveAs(saveFile, pngSaveOptions, true, Extension.LOWERCASE);
}

function Revert() {
var idRvrt = charIDToTypeID("Rvrt");
executeAction(idRvrt, undefined, DialogModes.NO);
}

Main()

@bybyspalma
Copy link

Captura de pantalla 2022-01-21 a la(s) 22 39 11

@sambatlim
Copy link
Author

Before run script you need to save your PS file first.

@bybyspalma
Copy link

where?

@bybyspalma
Copy link

not working :(

@sambatlim
Copy link
Author

Save your PSD file where you want the output to be saved.

@bybyspalma
Copy link

i found this

main();

function main()
{

var Name = app.activeDocument.name.replace(/\.[^\.]+$/, ''); 
var Ext = decodeURI(app.activeDocument.name).replace(/^.*\./,''); 
if(Ext.toLowerCase() != 'psd') return;

// assumes the file has already been saved
var Path = app.activeDocument.path; 

// Save as PSD
var SaveFile = File(Path + "/" + Name +".psd"); 
if(SaveFile.exists) SaveFile.remove(); 
SavePSD(SaveFile);

// Save as PNG
SaveFile = File(Path + "/" + Name +".png"); 
if(SaveFile.exists) SaveFile.remove(); 
SavePNG(SaveFile);

alert("File saved as .png and .psd");

}

function SavePNG(saveFile)
{
var pngSaveOptions = new PNGSaveOptions();
activeDocument.saveAs(saveFile, pngSaveOptions, true, Extension.LOWERCASE);
}

function SavePSD(saveFile)
{
var psdFile = new File(saveFile);
psdSaveOptions = new PhotoshopSaveOptions();
psdSaveOptions.embedColorProfile = true;
psdSaveOptions.alphaChannels = true;
activeDocument.saveAs(psdFile, psdSaveOptions, false, Extension.LOWERCASE);
}

@bybyspalma
Copy link

not working :( i´m gonna die

@mtfonseca83
Copy link

Hi Sambat,
I only created 3 groups in photoshop so I won't have to edit your script. When I launch the script from photoshop it only creates the PNG folder but no images have been exported and the folder is empty. Im using photoshop 2014.
image

@know-sol
Copy link

Im having the same issue. I am also using 2014.

@sambatlim
Copy link
Author

Never try on PS 2014.

@artMayakosha
Copy link

I am using CS6. When I browse for your .JS file I cant see it. Does it not work?
Really great tutorial. Which Photoshop version are you using?

@artMayakosha
Copy link

Works with PS2020. Great job mate!! Long Live King Lim!!

@screwfaceworld
Copy link

bybyspalma : i use ur code above and its working on myside i using PS 2020

@ArtisNegoti
Copy link

Hi
I have used this to create static NFTs. I have a character and then need to change background color and icon. I also have animated the characters. How do I change this code to work on an animated character in photoshop or aftereffects? i.e the character is animated and the background color and icon change

@ArtisNegoti
Copy link

I have managed to get the animation into a video group in Photoshop (as a .mov) and then converted my other groups of layers into groups of video groups. As the file goes through the various layers in the group, it needs to be added to a render queue rather than saved as a png.

@sambatlim
Copy link
Author

It would be great if you share your code here.

@ArtisNegoti
Copy link

ArtisNegoti commented Mar 16, 2022 via email

@ArtisNegoti
Copy link

Hi
Checking to see if you received the email I sent yesterday. Let me know if you are interested.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment