Copy link to clipboard
Copied
How can I make a Load Selection for all selected layers?
Without pressing ctrl+ Mouse Click to select each layer every time
1 Correct answer
You can use an action:
A script has the added bonus of being able to offer a single history step:
/*
Load Selected Layers Transparency to Selection.jsx
Stephen Marsh, 8th September 2021
v1.0
NOTE: Previously selected layers are deselected, it would be nice to retain the selected layers...
*/
function loadSelLayersTransparency() {
// Select all layers (ignores Background layer, ignores visibility)
app.runMenuItem(stringIDToTypeID('selectAllLayers'));
// Cr
...
Explore related tutorials & articles
Copy link to clipboard
Copied
You can use an action:
A script has the added bonus of being able to offer a single history step:
/*
Load Selected Layers Transparency to Selection.jsx
Stephen Marsh, 8th September 2021
v1.0
NOTE: Previously selected layers are deselected, it would be nice to retain the selected layers...
*/
function loadSelLayersTransparency() {
// Select all layers (ignores Background layer, ignores visibility)
app.runMenuItem(stringIDToTypeID('selectAllLayers'));
// Create temp merged layer
mergedLayer();
// Rename layer
app.activeDocument.activeLayer.name = "_temp";
// Load layer transparency
loadLayerTrans();
// Remove temp merged layer
app.activeDocument.activeLayer.remove();
/****************** Functions ******************/
function mergedLayer() {
var idmergeLayersNew = stringIDToTypeID("mergeLayersNew");
var desc291 = new ActionDescriptor();
var idduplicate = stringIDToTypeID("duplicate");
desc291.putBoolean(idduplicate, true);
executeAction(idmergeLayersNew, desc291, DialogModes.NO);
}
function loadLayerTrans() {
var idset = stringIDToTypeID("set");
var desc323 = new ActionDescriptor();
var idnull = stringIDToTypeID("null");
var ref75 = new ActionReference();
var idchannel = stringIDToTypeID("channel");
var idselection = stringIDToTypeID("selection");
ref75.putProperty(idchannel, idselection);
desc323.putReference(idnull, ref75);
var idto = stringIDToTypeID("to");
var ref76 = new ActionReference();
var idchannel = stringIDToTypeID("channel");
var idchannel = stringIDToTypeID("channel");
var idtransparencyEnum = stringIDToTypeID("transparencyEnum");
ref76.putEnumerated(idchannel, idchannel, idtransparencyEnum);
desc323.putReference(idto, ref76);
executeAction(idset, desc323, DialogModes.NO);
}
}
// Single history step
app.activeDocument.suspendHistory("Load Selected Layers Transparency to Selection", "loadSelLayersTransparency()");
Remove or comment out the following to only use the selected layers instead of all layers:
// Select all layers (ignores Background layer, ignores visibility)
app.runMenuItem(stringIDToTypeID('selectAllLayers'));
Copy link to clipboard
Copied
Thank you very much for helping me, sir
Copy link to clipboard
Copied
You're welcome!
There may be other ways*, this is just the way that naturally came to me.
*loop over all layers, load transparency as selection with modifier to add to selection.
Copy link to clipboard
Copied
The following code would work for pixel layers, but would disregard Layers Masks and Vector Masks.
So in that regard your approach is actually more reliable.
// load selection of selected layers;
// 2023, use it at your own risk;
if (documents.length > 0) {
var theSelectedLayers = collectSelectedLayers ();
var theAdd = false;
for (var m = 0; m < theSelectedLayers.length; m++) {
loadTransparencyAsSelection (theSelectedLayers[m][2], false, theAdd);
theAdd = true;
}
};
////// load transparency of layer //////
function loadTransparencyAsSelection (theID, theInvert, theAdd) {
try {
var idchannel = stringIDToTypeID( "channel" );
if (theAdd == true) {
var desc7 = new ActionDescriptor();
var ref1 = new ActionReference();
ref1.putEnumerated( idchannel, idchannel, stringIDToTypeID( "transparencyEnum" ) );
ref1.putIdentifier( stringIDToTypeID( "layer" ), theID );
desc7.putReference( stringIDToTypeID( "null" ), ref1 );
var ref2 = new ActionReference();
var idchannel = stringIDToTypeID( "channel" );
var idselection = stringIDToTypeID( "selection" );
ref2.putProperty( idchannel, idselection );
desc7.putReference( stringIDToTypeID( "to" ), ref2 );
executeAction( stringIDToTypeID( "add" ), desc7, DialogModes.NO );}
else {
var desc7 = new ActionDescriptor();
var ref3 = new ActionReference();
ref3.putProperty( idchannel, stringIDToTypeID( "selection" ) );
desc7.putReference( stringIDToTypeID( "null" ), ref3 );
var ref4 = new ActionReference();
ref4.putEnumerated( idchannel, idchannel, stringIDToTypeID( "transparencyEnum" ) );
ref4.putIdentifier( stringIDToTypeID( "layer" ), theID );
desc7.putReference( stringIDToTypeID( "to" ), ref4 );
executeAction( stringIDToTypeID( "set" ), desc7, DialogModes.NO )
};
} catch (e) {}
};
////// collect bounds of selected layers //////
function collectSelectedLayers () {
// set to pixels;
var originalRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
// get selected layers;
var selectedLayers = new Array;
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var desc = executeActionGet(ref);
if (desc.getBoolean(stringIDToTypeID("hasBackgroundLayer")) == true) {var theAdd =0}
else {var theAdd = 1};
if( desc.hasKey( stringIDToTypeID( 'targetLayers' ) ) ){
desc = desc.getList( stringIDToTypeID( 'targetLayers' ));
var c = desc.count;
var selectedLayers = new Array();
// run through selected layers;
for(var i=0;i<c;i++){
var theIndex = desc.getReference( i ).getIndex()+theAdd;
// get id for solid color layers;
try {
var ref = new ActionReference();
ref.putIndex( charIDToTypeID("Lyr "), theIndex );
var layerDesc = executeActionGet(ref);
var theName = layerDesc.getString(stringIDToTypeID('name'));
var theIdentifier = layerDesc.getInteger(stringIDToTypeID ("layerID"));
/*var theClippingMasked = layerDesc.getBoolean(stringIDToTypeID ("group"));
var theBounds = layerDesc.getObjectValue(stringIDToTypeID("bounds"));
var theseBounds = [theBounds.getUnitDoubleValue(stringIDToTypeID("left")), theBounds.getUnitDoubleValue(stringIDToTypeID("top")), theBounds.getUnitDoubleValue(stringIDToTypeID("right")), theBounds.getUnitDoubleValue(stringIDToTypeID("bottom"))];
var theXCenter = theseBounds[0]+(theseBounds[2]-theseBounds[0])/2;
var theYCenter = theseBounds[1]+(theseBounds[3]-theseBounds[1])/2;*/
selectedLayers.push([theName, theIndex, theIdentifier/*, theseBounds, theXCenter, theYCenter*/]);
} catch (e) {};
};
// if only one:
}else{
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var layerDesc = executeActionGet(ref);
try {
var theName = layerDesc.getString(stringIDToTypeID('name'));
var theIdentifier = layerDesc.getInteger(stringIDToTypeID ("layerID"));
/*var theClippingMasked = layerDesc.getBoolean(stringIDToTypeID ("group"));
var theBounds = layerDesc.getObjectValue(stringIDToTypeID("bounds"));
var theseBounds = [theBounds.getUnitDoubleValue(stringIDToTypeID("left")), theBounds.getUnitDoubleValue(stringIDToTypeID("top")), theBounds.getUnitDoubleValue(stringIDToTypeID("right")), theBounds.getUnitDoubleValue(stringIDToTypeID("bottom"))];
var theXCenter = theseBounds[0]+(theseBounds[2]-theseBounds[0])/2;
var theYCenter = theseBounds[1]+(theseBounds[3]-theseBounds[1])/2;*/
selectedLayers = [[theName, theIndex, theIdentifier/*, theseBounds, theXCenter, theYCenter*/]]
} catch (e) {};
};
// reset;
app.preferences.rulerUnits = originalRulerUnits;
return selectedLayers;
};
Copy link to clipboard
Copied
Copy link to clipboard
Copied
No, it works with and without a Background Layer.
What is the alert you get?
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Have you tried running the script outside of ESTK, directly in Photoshop?
Copy link to clipboard
Copied
Could you please post screenshots with the pertinent Panels (Toolbar, Layers, Options Bar, …) visible?
What is the image’s Color Mode etc.?
Copy link to clipboard
Copied
@c.pfaffenbichler Ok sir
What is the image’s Color Mode etc.?
Color Mode (RGB/8)when running the script outside of ESTK, directly in Photoshop?
this message show to me
error show in line number 48
Copy link to clipboard
Copied
The Panel names being written in All Caps seems peculiar to me (Mac user myself), which version of Photoshop are you using?
Can you provide a sample file (feel free to black out all design elements)?
Copy link to clipboard
Copied
Is the problem in the Photoshop version? I have a version of Photoshop CS5. I know it is old, but this is due to my work circumstances.
Copy link to clipboard
Copied
I work with Photoshop 2024, so if you want to pursue a Scripting approach (despite the existing Action-approach) your problem with CS5 is yours to fix.
Copy link to clipboard
Copied
If the issue is that the Action Manager code isn't compatible with your old version, then you can either use older supported AM code, or use supported DOM code instead.
try {
app.activeDocument.backgroundLayer;
alert("A Background layer exists!");
} catch (e) {
alert("No Background layer exists!");
}
Copy link to clipboard
Copied
And if the images always have a background Layer the whole check and the variable theAdd are useless and could be removed.
@Mohamed Hameed , didn’t you use a function called »getSelectedLayersIdx« (or something similar) in one of your other projects?
If that works then you can also adapt it to collect IDs instead of (or additional to) indices.
Copy link to clipboard
Copied
@c.pfaffenbichler
I didn't understand what you mean
Is this a modification within the code or what?
Copy link to clipboard
Copied
You can modify the code anyway you want or need to adapt it to Photoshop CS5.
Copy link to clipboard
Copied
@c.pfaffenbichler – Nice one! Now I can see why this method was beyond my reach.

