OK, the following script hack will add .png to all layer set names, which will automatically create PNG image assets via Generator.
// https://forums.adobe.com/message/8519528
// https://forums.adobe.com/message/11092165#11092165
// Script hacked to add .png extension to all layer sets for use with Generator to auto create assets
// Does not work with nested sets/groups inside of sets/groups
#target photoshop;
setLayerSetNames();
function setLayerSetNames(){
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;
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 ' ));
var ID = desc.getInteger(stringIDToTypeID( 'layerID' ));
if(layerName.match(/^<\/Layer group/) ) continue;
var layerType = typeIDToStringID(desc.getEnumerationValue( stringIDToTypeID( 'layerSection' )));
var isLayerSet =( layerType == 'layerSectionContent') ? false:true;
if(isLayerSet) setLayerName(ID,layerName.replace(/$/, '\.png'));
};
};
function setLayerName(ID,Name) {
var desc4 = new ActionDescriptor();
var ref3 = new ActionReference();
ref3.putIdentifier(charIDToTypeID('Lyr '), ID);
desc4.putReference( charIDToTypeID('null'), ref3 );
var desc5 = new ActionDescriptor();
desc5.putString( charIDToTypeID('Nm '), Name);
desc4.putObject( charIDToTypeID('T '), charIDToTypeID('Lyr '), desc5 );
executeAction(charIDToTypeID('slct'), desc4, DialogModes.NO);
executeAction( charIDToTypeID('setd'), desc4, DialogModes.NO );
};
The Generate > Image Assets is actually an excellent solution for automating export, provided you can find quick/easy ways to name your layers and groups accordingly! (See below)
Heh, wow, Stephan, I can't for the life of me figure out why, but if I remove the comments at the top of your script it works fine... So, your solution effectively works!
The complete solution is as follows.
In a file with multiple groups (layersets) that you wish to export as PNG.
1. First, run this script (to append .png to all layers sets) - Credit Stephen_A_Marsh and SuperMerlin (Is it possible to convert layerset names to uppercase? )
#target photoshop;
setLayerSetNames();
function setLayerSetNames(){
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;
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 ' ));
var ID = desc.getInteger(stringIDToTypeID( 'layerID' ));
if(layerName.match(/^<\/Layer group/) ) continue;
var layerType = typeIDToStringID(desc.getEnumerationValue( stringIDToTypeID( 'layerSection' )));
var isLayerSet =( layerType == 'layerSectionContent') ? false:true;
if(isLayerSet) setLayerName(ID,layerName.replace(/$/, '\.png'));
};
};
function setLayerName(ID,Name) {
var desc4 = new ActionDescriptor();
var ref3 = new ActionReference();
ref3.putIdentifier(charIDToTypeID('Lyr '), ID);
desc4.putReference( charIDToTypeID('null'), ref3 );
var desc5 = new ActionDescriptor();
desc5.putString( charIDToTypeID('Nm '), Name);
desc4.putObject( charIDToTypeID('T '), charIDToTypeID('Lyr '), desc5 );
executeAction(charIDToTypeID('slct'), desc4, DialogModes.NO);
executeAction( charIDToTypeID('setd'), desc4, DialogModes.NO );
};
Which renames the groups with a .png extension.
2. Make sure Generate Assets > Image Assets is checked. If you want to do this via an action, there are two ways:
- Insert the menu item into an action
var idAdobeScriptAutomationScripts = stringIDToTypeID( "AdobeScriptAutomation Scripts" );
var desc885 = new ActionDescriptor();
var idjsNm = charIDToTypeID( "jsNm" );
desc885.putString( idjsNm, """Image Assets""" );
executeAction( idAdobeScriptAutomationScripts, desc885, DialogModes.NO );
3. Now save. This will save the document as well as an adjacent folder called [documentname]-assets which will include a PNG for every layer and/or group with the .png extension
- note - in my case all layers also had a .png extension, which was it's own, separate hassle
- note2 - this may be in some ways connected to your export settings. If you are not getting a separate folder, try checking those settings
All of this can be recorded as actions, and therefore completely automate-able.