Copy link to clipboard
Copied
I want to export layers to image files(png, jpg). To export smaller image files, I want to export layers with transparent pixels to PNG file, and layers without any transparent pixels to JPG files. I think I need to get the color of every pixel of the layer, then check if any of the color contains transparency. So how can I do this with
ExtendScript? I checked the API reference and google a lot, but didn't find a solution.
...alert("Layer " + (layer_is_solid()?"does not have ":"has a ") + "transparency")
function layer_is_solid()
{
try {
var solid = false;
selection_from_layer();
try { app.activeDocument.selection.feather(0); } catch(e) { solid = true; }
app.activeDocument.selection.deselect();
return solid;
}
catch(e) { alert(e); throw(e); }
}
function selection_from_layer()
{
try {
var r1 = new ActionReference();
r1.putProperty(charIDToTypeID(
Thanks for catching that @Stephen_A_Marsh. I have updated the code below with a layer_is_empty function to handle that special case.
// https://community.adobe.com/t5/photoshop-ecosystem-discussions/how-to-check-if-a-layer-contains-any-transparent-pixels/td-p/9917339
alert( "Layer is " +( layer_is_solid() ? "solid" : "transparent" ) )
function layer_is_solid() {
try {
displayDialogs = DialogModes.NO
var solid = false
var doc = app.activeDocument
selection_from_layer()
...
Copy link to clipboard
Copied
Copy link to clipboard
Copied
alert("Layer " + (layer_is_solid()?"does not have ":"has a ") + "transparency")
function layer_is_solid()
{
try {
var solid = false;
selection_from_layer();
try { app.activeDocument.selection.feather(0); } catch(e) { solid = true; }
app.activeDocument.selection.deselect();
return solid;
}
catch(e) { alert(e); throw(e); }
}
function selection_from_layer()
{
try {
var r1 = new ActionReference();
r1.putProperty(charIDToTypeID("Chnl"), charIDToTypeID("fsel"));
var d = new ActionDescriptor();
d.putReference(charIDToTypeID("null"), r1);
var r2 = new ActionReference();
r2.putEnumerated(charIDToTypeID("Chnl"), charIDToTypeID("Chnl"), charIDToTypeID("Trsp"));
d.putReference(charIDToTypeID("T "), r2);
executeAction(charIDToTypeID("setd"), d, DialogModes.NO);
}
catch(e) { throw(e); }
}
Copy link to clipboard
Copied
Interesting workaround. For me, it gives false on an empty layer and true on layers with any non-transparent pixels. But I'm looking for a way to flag any layer that isn't completely solid.
Copy link to clipboard
Copied
Here is the modification that works for me. I added the invert_selection_from_layer function and some displayDialogs managment.
// https://community.adobe.com/t5/photoshop-ecosystem-discussions/how-to-check-if-a-layer-contains-any-transparent-pixels/td-p/9917339
alert("Layer " + (layer_is_solid() ? "does not have " : "has a ") + "transparency")
function layer_is_solid()
{
try {
displayDialogs = DialogModes.NO
var solid = false;
selection_from_layer();
invert_selection_from_layer();
try {
app.activeDocument.selection.feather(0);
} catch (e) {
solid = true;
}
app.activeDocument.selection.deselect();
displayDialogs = DialogModes.ERROR
return solid;
} catch (e) {
displayDialogs = DialogModes.ERROR
alert(e);
throw (e);
}
} // layer_is_solid
function selection_from_layer()
{
try {
var r1 = new ActionReference();
r1.putProperty(charIDToTypeID("Chnl"), charIDToTypeID("fsel"));
var d = new ActionDescriptor();
d.putReference(charIDToTypeID("null"), r1);
var r2 = new ActionReference();
r2.putEnumerated(charIDToTypeID("Chnl"), charIDToTypeID("Chnl"), charIDToTypeID("Trsp"));
d.putReference(charIDToTypeID("T "), r2);
executeAction(charIDToTypeID("setd"), d, DialogModes.NO);
} catch (e) {
throw (e);
}
} // selection_from_layer
function invert_selection_from_layer() {
try {
executeAction( charIDToTypeID( "Invs" ), undefined, DialogModes.NO );
} catch (e) {
throw (e);
}
} // invert_selection_from_layer
Copy link to clipboard
Copied
@nickcombs thanks for contributing... For me, your code is still flagging a single transparent layer with no content as not having transparency.
Copy link to clipboard
Copied
Thanks for catching that @Stephen_A_Marsh. I have updated the code below with a layer_is_empty function to handle that special case.
// https://community.adobe.com/t5/photoshop-ecosystem-discussions/how-to-check-if-a-layer-contains-any-transparent-pixels/td-p/9917339
alert( "Layer is " +( layer_is_solid() ? "solid" : "transparent" ) )
function layer_is_solid() {
try {
displayDialogs = DialogModes.NO
var solid = false
var doc = app.activeDocument
selection_from_layer()
invert_selection_from_layer()
try { doc.selection.feather(0) }
catch (e) { solid = true }
if( layer_is_empty( doc.activeLayer) ) solid = false
doc.selection.deselect()
displayDialogs = DialogModes.ERROR
return solid
} catch (e) {
displayDialogs = DialogModes.ERROR
throw ( 'layer_is_solid >> '+ e )
}
} // layer_is_solid
function selection_from_layer() {
try {
var r1 = new ActionReference()
r1.putProperty( chID("Chnl"), chID("fsel") )
var d = new ActionDescriptor()
d.putReference( chID("null"), r1 )
var r2 = new ActionReference()
r2.putEnumerated( chID("Chnl"), chID("Chnl"), chID("Trsp") )
d.putReference( chID("T "), r2 )
executeAction( chID("setd"), d, DialogModes.NO )
} catch (e) {
throw ( 'selection_from_layer >> '+ e )
}
} // selection_from_layer
function invert_selection_from_layer() {
try {
executeAction( chID( "Invs" ), undefined, DialogModes.NO )
} catch (e) {
throw ( 'invert_selection_from_layer >> '+ e )
}
} // invert_selection_from_layer
function layer_is_empty( artLayer ) {
try {
var bds = artLayer.bounds
if( bds[0].value || bds[1].value || bds[2].value || bds[3].value )
return false
return true
} catch (e) {
throw ( 'layer_is_empty >> '+ e )
}
} // layer_is_empty
function chID( a ) { return charIDToTypeID( a )}