Skip to main content
Participant
December 15, 2015
Answered

Photoshop script to export groups as folders?

  • December 15, 2015
  • 1 reply
  • 974 views

Hello,

I am working with a .psb file that has around 100 groups with layers in them. I would like to export those groups into actual folders containing a .png file for each individual layer inside. If I had to create the folders manually and re-organise the png files inside them it would take me several hours. I can't seem to find a script that works. Any ideas? Thank you very much in advance.

This topic has been closed for replies.
Correct answer SuperMerlin

This script will create a folder of the filename and folders of the groupnames with pngs.

#target photoshop;

app.bringToFront();

main();

function main(){

if(!documents.length) return;

try{

var doc = app.activeDocument;

app.displayDialogs = DialogModes.NO;

pngSaveOptions = new PNGSaveOptions();

var LSets = getLayerSets();

if(LSets.length < 1) return;

try{

var Name = doc.name.replace(/\.[^\.]+$/, '');

var docPath = doc.path;

}catch(err){

    alert("This document needs to be saved before running this script!");

    return;

    }

var OutputPath = Folder(docPath + "/" + Name);

if(!OutputPath.exists) OutputPath.create();

for(var f in LSets){

    var LayerSetIndexs = getChildIndex(Number(LSets[0]),false);

    var groupFolder = Folder(OutputPath + "/" + LSets[1].toString());

    if(!groupFolder.exists) groupFolder.create();

    for(var x in LayerSetIndexs){

       selectLayerByIndex(Number(LayerSetIndexs));

       var Kind = activeDocument.activeLayer.kind;

        var Shape = isShapeLayer();

if(Kind ==LayerKind.NORMAL || Kind == LayerKind.SMARTOBJECT || Kind == LayerKind.TEXT || Shape == true){

    dupLayers();

    var saveFile = File(groupFolder + "/" +  activeDocument.activeLayer.name + ".png");

      activeDocument.saveAs(saveFile, pngSaveOptions, true, Extension.LOWERCASE);

    app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);

    }else{ continue; }

        }

    }

}catch(e){}

};   

function isShapeLayer(){

var ref = new ActionReference();

ref.putEnumerated( charIDToTypeID('Lyr '),charIDToTypeID('Ordn'),charIDToTypeID('Trgt') );

desc =  executeActionGet(ref);

if(!desc.hasKey(stringIDToTypeID('adjustment')) ) return false;

if(desc.getBoolean(stringIDToTypeID('hasVectorMask' )) == true)  return true;

return false;

};

function getLayerLayerSectionByIndex( index ) {  

   var ref = new ActionReference();

   ref.putIndex(charIDToTypeID('Lyr '), index);

   return typeIDToStringID(executeActionGet(ref).getEnumerationValue(stringIDToTypeID('layerSection')));

};

function getLayerNameByIndex( index ) {

    var ref = new ActionReference();

    ref.putIndex( charIDToTypeID( 'Lyr ' ), index );

    return executeActionGet(ref).getString(charIDToTypeID( 'Nm  ' ));

};

function skipNestedSets( layerIndex ){

   var isEnd = false;

   layerIndex = app.activeDocument.layers[app.activeDocument.layers.length-1].isBackgroundLayer ? layerIndex-2:layerIndex;

   while(!isEnd){

      layerIndex--;

      if( getLayerLayerSectionByIndex( layerIndex ) == 'layerSectionStart' ) layerIndex = skipNestedSets( layerIndex );

      isEnd = getLayerNameByIndex(layerIndex) == '</Layer group>' ? true:false;

   }

   return layerIndex-1;

};

function getChildIndex(idx, skipNested ){

   var layerSetIndex = idx;

   var isEndOfSet = false;

   var layerIndexArray = [];

   while(!isEndOfSet){

      layerSetIndex--;

      if( getLayerLayerSectionByIndex( layerSetIndex ) == 'layerSectionStart' && skipNested ){

         layerSetIndex = skipNestedSets( layerSetIndex );

      }

  if(getLayerLayerSectionByIndex( layerSetIndex ) == undefined) break;

      isEndOfSet = getLayerNameByIndex(layerSetIndex) == '</Layer group>' ? true:false;

     if(!isEndOfSet ) layerIndexArray.push( layerSetIndex );

   }

   return layerIndexArray;

};

function getLayerSets(){

   var ref = new ActionReference();

   ref.putProperty( charIDToTypeID( "Prpr" ), charIDToTypeID( 'NmbL' ));

   ref.putEnumerated( charIDToTypeID('Dcmn'), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );

   var count = executeActionGet(ref).getInteger(charIDToTypeID('NmbL')) +1;

   var Names=[];

try{

    activeDocument.backgroundLayer;

var i = 0; }catch(e){ var i = 1; };

   for(i;i<count;i++){

       if(i == 0) continue;

        ref = new ActionReference();

        ref.putIndex( charIDToTypeID( 'Lyr ' ), i );

        var desc = executeActionGet(ref);

        var layerName = desc.getString(charIDToTypeID( 'Nm  ' ));

        if(layerName.match(/^<\/Layer group/) ) continue;

        var layerType = typeIDToStringID(desc.getEnumerationValue( stringIDToTypeID( 'layerSection' )));

        var isLayerSet =( layerType == 'layerSectionContent') ? false:true;

        if(isLayerSet) Names.push([,[layerName]]);

   };

return Names;

};

function selectLayerByIndex(index){

var ref = new ActionReference();

ref.putIndex(charIDToTypeID("Lyr "), index);

var desc = new ActionDescriptor();

desc.putReference(charIDToTypeID("null"), ref );

desc.putBoolean( charIDToTypeID( "MkVs" ), true );

try{

executeAction(charIDToTypeID("slct"), desc, DialogModes.NO );

}catch(e){}

};

function dupLayers() {

var desc = new ActionDescriptor();

var ref = new ActionReference();

ref.putClass( charIDToTypeID('Dcmn') );

desc.putReference( charIDToTypeID('null'), ref );

desc.putString( charIDToTypeID('Nm  '), 'Temp' );

var ref25 = new ActionReference();

ref25.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );

desc.putReference( charIDToTypeID('Usng'), ref25 );

desc.putInteger( charIDToTypeID('Vrsn'), 5 );

executeAction( charIDToTypeID('Mk  '), desc, DialogModes.NO );

};

1 reply

c.pfaffenbichler
Community Expert
Community Expert
December 16, 2015

How frequent is the task for you?

agumanAuthor
Participant
December 16, 2015

Thank you for your answer. I work a lot on animations with scanned drawings, so very frequent.

SuperMerlin
SuperMerlinCorrect answer
Inspiring
December 16, 2015

This script will create a folder of the filename and folders of the groupnames with pngs.

#target photoshop;

app.bringToFront();

main();

function main(){

if(!documents.length) return;

try{

var doc = app.activeDocument;

app.displayDialogs = DialogModes.NO;

pngSaveOptions = new PNGSaveOptions();

var LSets = getLayerSets();

if(LSets.length < 1) return;

try{

var Name = doc.name.replace(/\.[^\.]+$/, '');

var docPath = doc.path;

}catch(err){

    alert("This document needs to be saved before running this script!");

    return;

    }

var OutputPath = Folder(docPath + "/" + Name);

if(!OutputPath.exists) OutputPath.create();

for(var f in LSets){

    var LayerSetIndexs = getChildIndex(Number(LSets[0]),false);

    var groupFolder = Folder(OutputPath + "/" + LSets[1].toString());

    if(!groupFolder.exists) groupFolder.create();

    for(var x in LayerSetIndexs){

       selectLayerByIndex(Number(LayerSetIndexs));

       var Kind = activeDocument.activeLayer.kind;

        var Shape = isShapeLayer();

if(Kind ==LayerKind.NORMAL || Kind == LayerKind.SMARTOBJECT || Kind == LayerKind.TEXT || Shape == true){

    dupLayers();

    var saveFile = File(groupFolder + "/" +  activeDocument.activeLayer.name + ".png");

      activeDocument.saveAs(saveFile, pngSaveOptions, true, Extension.LOWERCASE);

    app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);

    }else{ continue; }

        }

    }

}catch(e){}

};   

function isShapeLayer(){

var ref = new ActionReference();

ref.putEnumerated( charIDToTypeID('Lyr '),charIDToTypeID('Ordn'),charIDToTypeID('Trgt') );

desc =  executeActionGet(ref);

if(!desc.hasKey(stringIDToTypeID('adjustment')) ) return false;

if(desc.getBoolean(stringIDToTypeID('hasVectorMask' )) == true)  return true;

return false;

};

function getLayerLayerSectionByIndex( index ) {  

   var ref = new ActionReference();

   ref.putIndex(charIDToTypeID('Lyr '), index);

   return typeIDToStringID(executeActionGet(ref).getEnumerationValue(stringIDToTypeID('layerSection')));

};

function getLayerNameByIndex( index ) {

    var ref = new ActionReference();

    ref.putIndex( charIDToTypeID( 'Lyr ' ), index );

    return executeActionGet(ref).getString(charIDToTypeID( 'Nm  ' ));

};

function skipNestedSets( layerIndex ){

   var isEnd = false;

   layerIndex = app.activeDocument.layers[app.activeDocument.layers.length-1].isBackgroundLayer ? layerIndex-2:layerIndex;

   while(!isEnd){

      layerIndex--;

      if( getLayerLayerSectionByIndex( layerIndex ) == 'layerSectionStart' ) layerIndex = skipNestedSets( layerIndex );

      isEnd = getLayerNameByIndex(layerIndex) == '</Layer group>' ? true:false;

   }

   return layerIndex-1;

};

function getChildIndex(idx, skipNested ){

   var layerSetIndex = idx;

   var isEndOfSet = false;

   var layerIndexArray = [];

   while(!isEndOfSet){

      layerSetIndex--;

      if( getLayerLayerSectionByIndex( layerSetIndex ) == 'layerSectionStart' && skipNested ){

         layerSetIndex = skipNestedSets( layerSetIndex );

      }

  if(getLayerLayerSectionByIndex( layerSetIndex ) == undefined) break;

      isEndOfSet = getLayerNameByIndex(layerSetIndex) == '</Layer group>' ? true:false;

     if(!isEndOfSet ) layerIndexArray.push( layerSetIndex );

   }

   return layerIndexArray;

};

function getLayerSets(){

   var ref = new ActionReference();

   ref.putProperty( charIDToTypeID( "Prpr" ), charIDToTypeID( 'NmbL' ));

   ref.putEnumerated( charIDToTypeID('Dcmn'), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );

   var count = executeActionGet(ref).getInteger(charIDToTypeID('NmbL')) +1;

   var Names=[];

try{

    activeDocument.backgroundLayer;

var i = 0; }catch(e){ var i = 1; };

   for(i;i<count;i++){

       if(i == 0) continue;

        ref = new ActionReference();

        ref.putIndex( charIDToTypeID( 'Lyr ' ), i );

        var desc = executeActionGet(ref);

        var layerName = desc.getString(charIDToTypeID( 'Nm  ' ));

        if(layerName.match(/^<\/Layer group/) ) continue;

        var layerType = typeIDToStringID(desc.getEnumerationValue( stringIDToTypeID( 'layerSection' )));

        var isLayerSet =( layerType == 'layerSectionContent') ? false:true;

        if(isLayerSet) Names.push([,[layerName]]);

   };

return Names;

};

function selectLayerByIndex(index){

var ref = new ActionReference();

ref.putIndex(charIDToTypeID("Lyr "), index);

var desc = new ActionDescriptor();

desc.putReference(charIDToTypeID("null"), ref );

desc.putBoolean( charIDToTypeID( "MkVs" ), true );

try{

executeAction(charIDToTypeID("slct"), desc, DialogModes.NO );

}catch(e){}

};

function dupLayers() {

var desc = new ActionDescriptor();

var ref = new ActionReference();

ref.putClass( charIDToTypeID('Dcmn') );

desc.putReference( charIDToTypeID('null'), ref );

desc.putString( charIDToTypeID('Nm  '), 'Temp' );

var ref25 = new ActionReference();

ref25.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );

desc.putReference( charIDToTypeID('Usng'), ref25 );

desc.putInteger( charIDToTypeID('Vrsn'), 5 );

executeAction( charIDToTypeID('Mk  '), desc, DialogModes.NO );

};