Copy link to clipboard
Copied
I need a photoshop script that will take my file that has multiple group layers and turn them on one at a time and save the file as the group layer name then turn it off and turn on the next layer and save the file as that layers name and so forth for all the layers. There are about 10-15 group layers. is there a way already to do this or can someone make a script for me that can take care of this???
Copy link to clipboard
Copied
Did you do a search before posting?
https://github.com/golmakov/grexport#
Edit:
// saveTifCopiesWithOneGroupEach
// save tif copies with one group each;
// 2023, use it at your own risk;
if (app.documents.length > 0) {
var myDocument = activeDocument;
var docName = myDocument.name;
var basename = docName.match(/(.*)\.[^\.]+$/)[1];
var docPath = myDocument.path;
var theLayers = collectGroups();
for (var n = 0; n < theLayers.length; n++) {
selectLayerByID(theLayers[n][2], false);
showOrHideLayer(true, theLayers[n][1]);
hideOthers ();
saveCopyAsTif (myDocument, docPath+"/"+basename+"_"+theLayers[n][0]+".tif");
hideOthers ();
};
};
////// collect layers //////
function collectGroups () {
// get number of layers;
var ref = new ActionReference();
ref.putProperty(stringIDToTypeID('property'), stringIDToTypeID('numberOfLayers'));
ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var applicationDesc = executeActionGet(ref);
var theNumber = applicationDesc.getInteger(stringIDToTypeID("numberOfLayers"));
// process the layers;
var theLayers = new Array;
for (var m = 0; m <= theNumber; m++) {
try {
var ref = new ActionReference();
ref.putIndex( charIDToTypeID( "Lyr " ), m);
var layerDesc = executeActionGet(ref);
var layerSet = typeIDToStringID(layerDesc.getEnumerationValue(stringIDToTypeID("layerSection")));
var isBackground = layerDesc.getBoolean(stringIDToTypeID("background"));
// if group collect values;
if (layerSet == "layerSectionStart") {
var theName = layerDesc.getString(stringIDToTypeID('name'));
// collect if condition is met;
//if (theName.match(searchName) != undefined) {
var theID = layerDesc.getInteger(stringIDToTypeID('layerID'));
var theIndex = layerDesc.getInteger(stringIDToTypeID('itemIndex'));
theLayers.push([theName, theIndex, theID])
//}
};
}
catch (e) {};
};
return theLayers
};
////// based on code by mike hale, via paul riggott //////
function selectLayerByID(id,add){
add = undefined ? add = false:add
var ref = new ActionReference();
ref.putIdentifier(charIDToTypeID("Lyr "), id);
var desc = new ActionDescriptor();
desc.putReference(charIDToTypeID("null"), ref );
if(add) desc.putEnumerated( stringIDToTypeID( "selectionModifier" ), stringIDToTypeID( "selectionModifierType" ), stringIDToTypeID( "addToSelection" ) );
desc.putBoolean( charIDToTypeID( "MkVs" ), false );
try{
executeAction(charIDToTypeID("slct"), desc, DialogModes.NO );
}catch(e){
// alert(e.message);
}
};
////// collect layers //////
function collectLayersBounds () {
// the file;
var myDocument = app.activeDocument;
// get number of layers;
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var applicationDesc = executeActionGet(ref);
var theNumber = applicationDesc.getInteger(stringIDToTypeID("numberOfLayers"));
// process the layers;
var theLayers = new Array;
for (var m = 0; m <= theNumber; m++) {
try {
var ref = new ActionReference();
ref.putIndex( charIDToTypeID( "Lyr " ), m);
var layerDesc = executeActionGet(ref);
var layerSet = typeIDToStringID(layerDesc.getEnumerationValue(stringIDToTypeID("layerSection")));
var isBackground = layerDesc.getBoolean(stringIDToTypeID("background"));
// if group collect values;
if (layerSet != "layerSectionEnd" && layerSet != "layerSectionStart" /*&& isBackground != true*/) {
var theName = layerDesc.getString(stringIDToTypeID('name'));
var theID = layerDesc.getInteger(stringIDToTypeID('layerID'));
var theBounds = layerDesc.getObjectValue(stringIDToTypeID("bounds"));
var theseBounds = [theBounds.getUnitDoubleValue(stringIDToTypeID("left")), theBounds.getUnitDoubleValue(stringIDToTypeID("top")), theBounds.getUnitDoubleValue(stringIDToTypeID("right")), theBounds.getUnitDoubleValue(stringIDToTypeID("bottom"))];
theLayers.push([theName, theID, theseBounds])
};
}
catch (e) {};
};
return theLayers
};
////// buffer number with zeros //////
function bufferNumberWithZeros (number, places) {
var theNumberString = String(number);
for (var o = 0; o < (places - String(number).length); o++) {
theNumberString = String("0" + theNumberString)
};
return theNumberString
};
//////
function showOrHideLayer (theCheck, theID) {
if (theCheck == false) {var idhide = stringIDToTypeID( "hide" )}
else {var idhide = stringIDToTypeID( "show" )};
try {
var desc22 = new ActionDescriptor();
var list2 = new ActionList();
var ref8 = new ActionReference();
ref8.putIdentifier( stringIDToTypeID( "layer" ), theID );
//ref8.putEnumerated( stringIDToTypeID( "layer" ), stringIDToTypeID( "ordinal" ), stringIDToTypeID( "targetEnum" ) );
list2.putReference( ref8 );
desc22.putList( stringIDToTypeID( "null" ), list2 );
executeAction( idhide, desc22, DialogModes.NO );
} catch (e) {}
};
////// save tif //////
function saveCopyAsTif (myDocument, thePath) {
// tif options;
tifOpts = new TiffSaveOptions();
tifOpts.embedColorProfile = true;
tifOpts.imageCompression = TIFFEncoding.TIFFLZW;
tifOpts.alphaChannels = false;
tifOpts.byteOrder = ByteOrder.MACOS;
tifOpts.layers = true;
// save copy;
myDocument.saveAs((new File(thePath)), tifOpts, true);
};
////// hide others //////
function hideOthers () {
// =======================================================
var idShw = charIDToTypeID( "Shw " );
var desc2 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var list1 = new ActionList();
var ref1 = new ActionReference();
var idLyr = charIDToTypeID( "Lyr " );
var idOrdn = charIDToTypeID( "Ordn" );
var idTrgt = charIDToTypeID( "Trgt" );
ref1.putEnumerated( idLyr, idOrdn, idTrgt );
list1.putReference( ref1 );
desc2.putList( idnull, list1 );
var idTglO = charIDToTypeID( "TglO" );
desc2.putBoolean( idTglO, true );
executeAction( idShw, desc2, DialogModes.NO );
};
Copy link to clipboard
Copied