基本上,想要你想要的前提是遍历所有层,在你去的时候打开和关闭可行性。或者在开始之前将它们全部关闭,然后让代码循环遍历所有层。同样,当您开始时,摆脱任何组,这会更容易。如果需要,请在没有它们的情况下制作 psd 的副本。
在 Photoshop 脚本中,最顶层是 0。下面的下一层是 1,依此类推。循环倒数更容易
for (var i = numOfLayers -1; i >= 0; i--)
{
// do stuff here
}
每一层的可见性通过以下方式控制:
theLayer.visible = true; // visible
thatLayer.visible = false; // not visible
所以简而言之,你会有这样的东西:
// WITH ALL THE LAYERS VISIBILITY SWITCHED OFF TO START WITH!
// Switch off any dialog boxes
displayDialogs = DialogModes.NO; // OFF
// call the source document
var srcDoc = app.activeDocument;
// get the number of layers in the PSD
var numOfLayers = srcDoc.layers.length;
var myFolder = "D:\\temp\\";
// main loop
// ignore background
for (var i = numOfLayers -2; i >= 0 ; i--)
{
var docName = "image_" + i + ".jpg";
// get a reference to the layers as you go up
var thisLayer = srcDoc.layers[i];
thisLayer.visible = true;
// duplicate image into new document
duplicate_it(docName);
// active document is now the NEW one!
// flatten it
activeDocument.flatten();
jpeg_it(myFolder + docName, 12);
// close the image WITHOUT saving as we've just saved it
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
// make the source document the active document
app.activeDocument = srcDoc;
// switch OFF the layer
thisLayer.visible = false;
}
// function DUPLICATE IT (str)
// --------------------------------------------------------
function duplicate_it(str)
{
// duplicate image into new document
if (arguments.length == 0) str = "temp";
var id428 = charIDToTypeID( "Dplc" );
var desc92 = new ActionDescriptor();
var id429 = charIDToTypeID( "null" );
var ref27 = new ActionReference();
var id430 = charIDToTypeID( "Dcmn" );
var id431 = charIDToTypeID( "Ordn" );
var id432 = charIDToTypeID( "Frst" );
ref27.putEnumerated( id430, id431, id432 );
desc92.putReference( id429, ref27 );
var id433 = charIDToTypeID( "Nm " );
desc92.putString( id433, str ); // name
executeAction( id428, desc92, DialogModes.NO );
}
// function JPEG IT (file path + file name, jpeg quality)
// ----------------------------------------------------------------
function jpeg_it(filePath, jpgQuality)
{
if(! jpgQuality) jpgQuality = 12;
// jpg file options
var jpgFile = new File(filePath);
jpgSaveOptions = new JPEGSaveOptions();
jpgSaveOptions.formatOptions = FormatOptions.OPTIMIZEDBASELINE;
jpgSaveOptions.embedColorProfile = true;
jpgSaveOptions.matte = MatteType.NONE;
jpgSaveOptions.quality = jpgQuality;
activeDocument.saveAs(jpgFile, jpgSaveOptions, true, Extension.LOWERCASE);
}