VI.ii Saving your layers as separate documents
Sample Code
_layersToFiles.js

 

Download .js code PS7 / CS
(right click and 'save as')


One of the most widespread use of the scripting tool is the script that allows you to turn your layers into individual files, which comes in quite handy when you want to dissect a PSD that has for example 70 layers. This script will save layers to individual PNG documents, in a new subfolder called 'documentName_separated' of the image's current folder. (you'll need to save the document first otherwise an alert screen will show up).



[ Click on the image to see how the script worked (pop up window)]

Note

The script will not save layers that are hidden, will not save adjustment layers etc (only text, normal and layer sets), and will not modify your initial image. Of course you can implement your own restrictions, or save hidden layers too, just modify the code !


displayDialogs = DialogModes.NO;
saveOptions = new PNGSaveOptions();

if ((documents.length != 0) && (activeDocument.saved)){

  var AD = activeDocument;
  var CurrentFolder = AD.path;
  var newFolder = AD.name+"_separated";
  var tempFolder = new Folder (CurrentFolder+"/"+newFolder);
  tempFolder.create();

  var tempLayer = AD.artLayers.add();
  var checkArray = new Array(AD.layers.length);

  for(a=1;a<=AD.layers.length;a++){
    var CL = AD.layers[a-1];
    if(!((CL.kind == LayerKind.TEXT)||(CL.kind == LayerKind.NORMAL)||(CL.kind == LayerKind.LayerSet))){
      checkArray[a-1] = 1;
    }
    if(CL.visible == 0){
      checkArray[a-1] = 2;
    }
  }

  for(a=1;a<=AD.layers.length;a++){
    AD.layers[a-1].visible = 0;
  }

  for(a=2;a<=AD.layers.length;a++){
    AD.layers[a-2].visible = 0;
    AD.layers[a-1].visible = 1;

    if((checkArray[a-1]!= 1)&&(checkArray[a-1]!= 2)){
      newFile = new File(tempFolder+"/("+(a-1)+")_"+AD.layers[a-1].name+".png");
      AD.saveAs (newFile,saveOptions, true, Extension.LOWERCASE);
    }
   }

  for(a=1;a<=AD.layers.length;a++){
    if(checkArray[a-1] == 2){
      AD.layers[a-1].visible = 0;
    }else{
      AD.layers[a-1].visible = 1;
    }
  }

   AD.layers[0].remove();

}else{

  alert("You either did not save the document or have no document opened !");

}

 

On to our third sample code: Saving your workspace to jpg images !


 page 7 / 11