解決済み
Convert all alpha channels into layers.
Hi everyone. I really need your help. I'm trying to find a script that can convert all alpha channels into different layers and if one or more alpha channels is/are empty/black to be deleted.
Thank you
Hi everyone. I really need your help. I'm trying to find a script that can convert all alpha channels into different layers and if one or more alpha channels is/are empty/black to be deleted.
Thank you
It’s best to use the “Apply Image” command.
You are right, Apply Image might also be faster.
// create layers for channels that are not empty in rgb images;
// 2020, use it at your own risk;
if (app.documents.length > 0) {
if (activeDocument.mode == DocumentMode.RGB) {
var theChannels = collectChannels ();
activeDocument.selection.deselect();
// create layers;
for (var m = theChannels.length-1; m >= 0; m--) {
loadAndLayer (theChannels[m][1], theChannels[m][0], [0,0,0]);
};
};
};
////// collect layers //////
function collectChannels () {
// get number of layers;
var ref = new ActionReference();
//ref.putProperty(stringIDToTypeID('property'), stringIDToTypeID('numberOfChannels'));
ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var applicationDesc = executeActionGet(ref);
var theNumber = applicationDesc.getInteger(stringIDToTypeID("numberOfChannels"));
// pixel dimensions;
var theWidth = applicationDesc.getUnitDoubleValue(stringIDToTypeID("width"));
var theHeight = applicationDesc.getUnitDoubleValue(stringIDToTypeID("height"));
var theRes = applicationDesc.getUnitDoubleValue(stringIDToTypeID("resolution"));
var thePixels = (theWidth*theRes/72)*(theHeight*theRes/72);
// process the channels;
var theChannels = new Array;
for (var m = 0; m <= theNumber; m++) {
try {
var ref = new ActionReference();
ref.putIndex(stringIDToTypeID( "channel" ), m);
var channelDesc = executeActionGet(ref);
// collect values;
var theName = channelDesc.getString(stringIDToTypeID('channelName'));
var theIndex = channelDesc.getInteger(stringIDToTypeID('itemIndex'));
var theID = channelDesc.getInteger(stringIDToTypeID('ID'));
if (channelDesc.hasKey(stringIDToTypeID('alphaChannelOptions')) == true) {
var theHistogram = channelDesc.getList(stringIDToTypeID('histogram'));
var value0 = theHistogram.getInteger(0);
// if not completely black;
if (value0 != thePixels) {
theChannels.push([theName, theIndex, theID])
} else {}
};
} catch (e) {};
};
return theChannels
};
//////
function loadAndLayer (theIndex, theName, theColor) {
var idnull = stringIDToTypeID( "null" );
var idcontentLayer = stringIDToTypeID( "contentLayer" );
var idchannel = stringIDToTypeID( "channel" );
var idmake = stringIDToTypeID( "make" );
// =======================================================
var desc11 = new ActionDescriptor();
var ref4 = new ActionReference();
ref4.putClass( idcontentLayer );
desc11.putReference( idnull, ref4 );
var desc12 = new ActionDescriptor();
var desc13 = new ActionDescriptor();
var desc14 = new ActionDescriptor();
desc14.putDouble( stringIDToTypeID( "red" ), theColor[0] );
desc14.putDouble( stringIDToTypeID( "grain" ), theColor[1] );
desc14.putDouble( stringIDToTypeID( "blue" ), theColor[2] );
var idRGBColor = stringIDToTypeID( "RGBColor" );
desc13.putObject( stringIDToTypeID( "color" ), idRGBColor, desc14 );
desc12.putString( stringIDToTypeID( "name" ), theName );
desc12.putObject( stringIDToTypeID( "type" ), stringIDToTypeID( "solidColorLayer" ), desc13 );
desc11.putObject( stringIDToTypeID( "using" ), idcontentLayer, desc12 );
executeAction( idmake, desc11, DialogModes.NO );
// =======================================================
var desc2 = new ActionDescriptor();
desc2.putClass( stringIDToTypeID( "new" ), idchannel );
var ref1 = new ActionReference();
ref1.putEnumerated( idchannel, idchannel, idmask = stringIDToTypeID( "mask" ) );
desc2.putReference( stringIDToTypeID( "at" ), ref1 );
desc2.putEnumerated( stringIDToTypeID( "using" ), stringIDToTypeID( "userMaskEnabled" ), stringIDToTypeID( "hideAll" ) );
executeAction( idmake, desc2, DialogModes.NO );
// =======================================================
var desc3 = new ActionDescriptor();
var desc4 = new ActionDescriptor();
var ref2 = new ActionReference();
ref2.putIndex( idchannel, theIndex );
desc4.putReference( stringIDToTypeID( "to" ), ref2 );
var idpreserveTransparency = stringIDToTypeID( "preserveTransparency" );
desc4.putBoolean( idpreserveTransparency, true );
desc3.putObject( stringIDToTypeID( "with" ), stringIDToTypeID( "calculation" ), desc4 );
executeAction( idapplyImageEvent = stringIDToTypeID( "applyImageEvent" ), desc3, DialogModes.NO );
};
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.