Copy link to clipboard
Copied
welcome everybody
I want to select and activate the group layer by name
I tried this code but it didn't work
It only works on layers, but not on groups
What is the code responsible for identifying the group by name and activating it?
I have tried these lines
var aDoc = app.activeDocument;
aDoc.activeLayer = aDoc.layers.getByName("Hameed")
var set=aDoc.activeLayer
welcome everybody
I want to select and activate the group layer by nameI tried this code but it didn't work
It only works on layers, but not on groupsWhat is the code responsible for identifying the group by name and activating it?
I have tried these lines
var aDoc = app.activeDocument; aDoc.activeLayer = aDoc.layers.getByName("Hameed") var set=aDoc.activeLayer
By @Mohamed Hameed
Try replacing layers with layerSets:
aDoc.activeLayer = aDoc.layerSets.getByName("Hameed")
var aDoc = app.activeDocument;
var set = aDoc.layerSets.getByName("Hameed");
aDoc.activeLayer = set;
So, something like this:
reselect_active_layer();
var aDoc = app.activeDocument;
var set = aDoc.layerSets.getByName("Hameed");
aDoc.activeLayer = set;
function reselect_active_layer()
{
var r = new ActionReference();
r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("layerID"));
r.putEnumerated(stringIDToTypeID("layer"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));
var id = executeActionGet(r).getInteger(stringIDToTypeID("layerID"))
...
Another option, this selects all layers (except a locked Background image layer), then deselects a single named layer from the selected layers:
// =======================================================
var idselectAllLayers = stringIDToTypeID( "selectAllLayers" );
var desc364 = new ActionDescriptor();
var idnull = stringIDToTypeID( "null" );
var ref141 = new ActionReference();
var idlayer = stringIDToTypeID( "layer" );
var idordinal = stringIDToTypeID( "ordinal
...
I was thinking something like this:
// Select All Layers
app.runMenuItem(stringIDToTypeID('selectAllLayers'));
// Layer Names to Exclude from Selection
deselectLayer("Test");
deselectLayer("Test 2");
function deselectLayer(layerName) {
var s2t = function (s) {
return app.stringIDToTypeID(s);
};
var descriptor = new ActionDescriptor();
var list = new ActionList();
var reference = new ActionReference();
reference.putName( s2t( "layer" ), layerName );
descriptor.putReference( s2t( "null
...
The following isn't pretty, it was just the first method that I explored. There may be a more refined method.
/*
Merge Selected Layers.jsx
https://community.adobe.com/t5/photoshop-ecosystem-discussions/activate-the-group-layer-by-name/m-p/14006739
Stephen Marsh, 15th August 2023 - v1.0
*/
#target photoshop
var theDoc = app.activeDocument;
theDoc.suspendHistory("Merge Selected Layers", "main()");
function main() {
if (app.documents.length > 0) {
var sourceDoc = app.activeDocument
...
Your descriptions are really hard for me to comprehend.
Screenshots that illustrate what you actually want to achieve might be helpful.
The following Script would select all Layers except the top level Group of a certain name and the Layers within that Group (see screenshots).
I am not sure if this is what you want to achieve, though.
// select all layers except the group of a certain name and the ones in that group;
// 2023, use it at your own risk;
if (app.documents.length > 0) {
var th
...
Copy link to clipboard
Copied
welcome everybody
I want to select and activate the group layer by nameI tried this code but it didn't work
It only works on layers, but not on groupsWhat is the code responsible for identifying the group by name and activating it?
I have tried these lines
var aDoc = app.activeDocument; aDoc.activeLayer = aDoc.layers.getByName("Hameed") var set=aDoc.activeLayer
By @Mohamed Hameed
Try replacing layers with layerSets:
aDoc.activeLayer = aDoc.layerSets.getByName("Hameed")
Copy link to clipboard
Copied
I tried the code and it didn't work for me
Copy link to clipboard
Copied
var aDoc = app.activeDocument;
var set = aDoc.layerSets.getByName("Hameed");
aDoc.activeLayer = set;
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Please provide the file (feel free to black out all the pixel layers).
Copy link to clipboard
Copied
Sometimes a layer must be selected, if none are selected as per your screenshot... But I'm pretty sure that I tested it without any layer active and it worked fine.
EDIT: A layer must first be selected before the code can select the set!
You can add a layer and then delete it as a hack... I'm pretty sure that rbin or jazzy or somebody else once posted some bullet proof code to ensure that a layer is selected... Perhaps this was it:
Copy link to clipboard
Copied
So, something like this:
reselect_active_layer();
var aDoc = app.activeDocument;
var set = aDoc.layerSets.getByName("Hameed");
aDoc.activeLayer = set;
function reselect_active_layer()
{
var r = new ActionReference();
r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("layerID"));
r.putEnumerated(stringIDToTypeID("layer"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));
var id = executeActionGet(r).getInteger(stringIDToTypeID("layerID"));
var r = new ActionReference();
r.putEnumerated(stringIDToTypeID("layer"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));
var d = new ActionDescriptor();
d.putReference(stringIDToTypeID("null"), r);
try { executeAction(stringIDToTypeID("selectNoLayers"), d, DialogModes.NO); } catch(e) {}
var r = new ActionReference();
r.putIdentifier(stringIDToTypeID("layer"), id);
var d = new ActionDescriptor();
d.putReference(stringIDToTypeID("null"), r);
d.putBoolean(stringIDToTypeID( "makeVisible"), activeDocument.activeLayer.visible);
try { executeAction(stringIDToTypeID("select"), d, DialogModes.NO); } catch(e) {}
}
Or this:
app.activeDocument.artLayers.add();
app.activeDocument.activeLayer.remove();
var aDoc = app.activeDocument;
var set = aDoc.layerSets.getByName("Hameed");
aDoc.activeLayer = set;
Copy link to clipboard
Copied
@c.pfaffenbichler @Stephen Marsh
Thank you all for your beautifully appreciated help
I have a simple request is it possible here
Or do I put it in a new topic
I just want help with defining all layers except for a layer specified by name or a group and layers specified by names
Are there threads related to this problem?
Please just share it
Or help me solve it if possible
Thanks and sorry for the inconvenience
Copy link to clipboard
Copied
I just want help with defining all layers except for a layer specified by name or a group and layers specified by names
By @Mohamed Hameed
I don't understand what you mean by "defining"?
Please illustrate with before/after screenshots of the layers panel.
Copy link to clipboard
Copied
@Stephen Marsh
Sorry
I mean to select all layers except for specific layers based on name
Copy link to clipboard
Copied
So you would type into a prompt or something like:
Layer 7, Group 1
Then all other layers would be selected except for Layer 7 and Group 1?
What if you just selected the layers that you didn't want to include, then run a script to invert the layer selection?
Copy link to clipboard
Copied
I tried the script
( Invert Selected Layers )
And I found the code like this
It does nothing but invert the selection and keep the layer
((Background ))
So you would type into a prompt or something like:
Layer 7, Group 1
Then all other layers would be selected except for Layer 7 and Group 1?
If you can help how can I do this
I found this code and it is for selecting all layers
I just want to add a line where only selected layers are excluded
Is this possible??
selectAllLayers();
function selectAllLayers() {
// Select all layers (doesn't include Background)
try {
var desc = new ActionDescriptor();
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
desc.putReference( charIDToTypeID('null'), ref );
executeAction( stringIDToTypeID('selectAllLayers'), desc, DialogModes.NO );
} catch(e) {}
// Add Background Layer to the selection (if possible)
try {
activeDocument.backgroundLayer;
var bgID = activeDocument.backgroundLayer.id;
var ref = new ActionReference();
var desc = new ActionDescriptor();
ref.putIdentifier(charIDToTypeID('Lyr '), bgID);
desc.putReference(charIDToTypeID('null'), ref);
desc.putEnumerated( stringIDToTypeID('selectionModifier'), stringIDToTypeID('selectionModifierType'), stringIDToTypeID('addToSelection') );
desc.putBoolean(charIDToTypeID('MkVs'), false);
executeAction(charIDToTypeID('slct'), desc, DialogModes.NO);
} catch(e) {}
}
Copy link to clipboard
Copied
First selecting the unwanted layers, then running the invert layer selection script worked for me in my tests to exclude the unwanted layers and select the others.
Copy link to clipboard
Copied
Another option, this selects all layers (except a locked Background image layer), then deselects a single named layer from the selected layers:
// =======================================================
var idselectAllLayers = stringIDToTypeID( "selectAllLayers" );
var desc364 = new ActionDescriptor();
var idnull = stringIDToTypeID( "null" );
var ref141 = new ActionReference();
var idlayer = stringIDToTypeID( "layer" );
var idordinal = stringIDToTypeID( "ordinal" );
var idtargetEnum = stringIDToTypeID( "targetEnum" );
ref141.putEnumerated( idlayer, idordinal, idtargetEnum );
desc364.putReference( idnull, ref141 );
executeAction( idselectAllLayers, desc364, DialogModes.NO );
// =======================================================
var idselect = stringIDToTypeID( "select" );
var desc365 = new ActionDescriptor();
var idnull = stringIDToTypeID( "null" );
var ref142 = new ActionReference();
var idlayer = stringIDToTypeID( "layer" );
ref142.putName( idlayer, "Test" ); // The layer to exclude
desc365.putReference( idnull, ref142 );
var idselectionModifier = stringIDToTypeID( "selectionModifier" );
var idselectionModifierType = stringIDToTypeID( "selectionModifierType" );
var idremoveFromSelection = stringIDToTypeID( "removeFromSelection" );
desc365.putEnumerated( idselectionModifier, idselectionModifierType, idremoveFromSelection );
var idmakeVisible = stringIDToTypeID( "makeVisible" );
desc365.putBoolean( idmakeVisible, false );
var idlayerID = stringIDToTypeID( "layerID" );
var list58 = new ActionList();
desc365.putList( idlayerID, list58 );
executeAction( idselect, desc365, DialogModes.NO );
Copy link to clipboard
Copied
Very great but this code only selects one layer
I want to add another layer or another group
Copy link to clipboard
Copied
I came up with an idea to do another layer selection
var idselectAllLayers = stringIDToTypeID( "selectAllLayers" );
var desc364 = new ActionDescriptor();
var idnull = stringIDToTypeID( "null" );
var ref141 = new ActionReference();
var idlayer = stringIDToTypeID( "layer" );
var idordinal = stringIDToTypeID( "ordinal" );
var idtargetEnum = stringIDToTypeID( "targetEnum" );
ref141.putEnumerated( idlayer, idordinal, idtargetEnum );
desc364.putReference( idnull, ref141 );
executeAction( idselectAllLayers, desc364, DialogModes.NO );
// =======================================================
var idselect = stringIDToTypeID( "select" );
var desc365 = new ActionDescriptor();
var desc366 = new ActionDescriptor();
var idnull = stringIDToTypeID( "null" );
var ref142 = new ActionReference();
var idlayer = stringIDToTypeID( "layer" );
ref142.putName( idlayer, "Final" )
desc365.putReference( idnull, ref142 );
var ref142 = new ActionReference();
var idlayer = stringIDToTypeID( "layer" );
ref142.putName( idlayer, "Layer 11" )
desc366.putReference( idnull, ref142 );
var idselectionModifier = stringIDToTypeID( "selectionModifier" );
var idselectionModifierType = stringIDToTypeID( "selectionModifierType" );
var idremoveFromSelection = stringIDToTypeID( "removeFromSelection" );
desc365.putEnumerated( idselectionModifier, idselectionModifierType, idremoveFromSelection );
desc366.putEnumerated( idselectionModifier, idselectionModifierType, idremoveFromSelection );
var idmakeVisible = stringIDToTypeID( "makeVisible" );
desc365.putBoolean( idmakeVisible, false );
desc366.putBoolean( idmakeVisible, false );
var idlayerID = stringIDToTypeID( "layerID" );
var list58 = new ActionList();
desc365.putList( idlayerID, list58 );
desc366.putList( idlayerID, list58 );
executeAction( idselect, desc365, DialogModes.NO );
executeAction( idselect, desc366, DialogModes.NO );
Is what I did right?
Or is there another suggestion that is easier than this
Copy link to clipboard
Copied
I was thinking something like this:
// Select All Layers
app.runMenuItem(stringIDToTypeID('selectAllLayers'));
// Layer Names to Exclude from Selection
deselectLayer("Test");
deselectLayer("Test 2");
function deselectLayer(layerName) {
var s2t = function (s) {
return app.stringIDToTypeID(s);
};
var descriptor = new ActionDescriptor();
var list = new ActionList();
var reference = new ActionReference();
reference.putName( s2t( "layer" ), layerName );
descriptor.putReference( s2t( "null" ), reference );
descriptor.putEnumerated( s2t( "selectionModifier" ), s2t( "selectionModifierType" ), s2t( "removeFromSelection" ));
descriptor.putBoolean( s2t( "makeVisible" ), false );
descriptor.putList( s2t( "layerID" ), list );
executeAction( s2t( "select" ), descriptor, DialogModes.NO );
}
Copy link to clipboard
Copied
Thank you sir for your help
Very very very great
I think this is much easier and clearer
If I wanted a fanction
It merges the selected layers, is it available or exists??
Copy link to clipboard
Copied
I can foresee possible problems with merging selected layers, but it is possible with some work. I don't know of existing code for this, but until I put in the effort to search for it that is no surprise.
Copy link to clipboard
Copied
The following isn't pretty, it was just the first method that I explored. There may be a more refined method.
/*
Merge Selected Layers.jsx
https://community.adobe.com/t5/photoshop-ecosystem-discussions/activate-the-group-layer-by-name/m-p/14006739
Stephen Marsh, 15th August 2023 - v1.0
*/
#target photoshop
var theDoc = app.activeDocument;
theDoc.suspendHistory("Merge Selected Layers", "main()");
function main() {
if (app.documents.length > 0) {
var sourceDoc = app.activeDocument;
dupeSelectedLayers("tempDoc");
app.activeDocument.flatten();
var tempDoc = app.activeDocument;
app.activeDocument = sourceDoc;
deleteSelectedLayers();
app.activeDocument = tempDoc;
dupeDoc("Merged Selected Layers");
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
} else {
alert('You must have a document open!');
}
///// FUNCTIONS /////
function dupeSelectedLayers(theName) {
function s2t(s) {
return app.stringIDToTypeID(s);
}
var descriptor = new ActionDescriptor();
var reference = new ActionReference();
var reference2 = new ActionReference();
reference.putClass(s2t("document"));
descriptor.putReference(s2t("null"), reference);
descriptor.putString(s2t("name"), theName);
reference2.putEnumerated(s2t("layer"), s2t("ordinal"), s2t("targetEnum"));
descriptor.putReference(s2t("using"), reference2);
executeAction(s2t("make"), descriptor, DialogModes.NO);
}
function deleteSelectedLayers() {
function s2t(s) {
return app.stringIDToTypeID(s);
}
var descriptor = new ActionDescriptor();
var list = new ActionList();
var reference = new ActionReference();
reference.putEnumerated(s2t("layer"), s2t("ordinal"), s2t("targetEnum"));
descriptor.putReference(s2t("null"), reference);
descriptor.putList(s2t("layerID"), list);
executeAction(s2t("delete"), descriptor, DialogModes.NO);
}
function dupeDoc(docName) {
function s2t(s) {
return app.stringIDToTypeID(s);
}
var descriptor = new ActionDescriptor();
var reference = new ActionReference();
var reference2 = new ActionReference();
reference.putEnumerated(s2t("layer"), s2t("ordinal"), s2t("targetEnum"));
descriptor.putReference(s2t("null"), reference);
reference2.putName(s2t("document"), sourceDoc.name);
descriptor.putReference(s2t("to"), reference2);
descriptor.putString(s2t("name"), docName);
executeAction(s2t("duplicate"), descriptor, DialogModes.NO);
}
}
Copy link to clipboard
Copied
Very great sir, thank you for your concern and assistance
But I am facing a slight problem
Those selected layers are within the group and I want to exclude them from the merge, and the other layer is also excluded
I used the (
// Select All Layers
) code
It works fine but only selects the group layer without its dependent layers
How can I exclude the layers of the specified group
This image shows the problem
I used the code to select all layers and exclude the group and the selected layer, but I discovered that the layers inside the group were not excluded from the selection
This is the code that I am using
// Select All Layers
app.runMenuItem(stringIDToTypeID('selectAllLayers'));
// Layer Names to Exclude from Selection
deselectLayer("Hameed");
deselectLayer("Final");
function deselectLayer(layerName) {
var s2t = function (s) {
return app.stringIDToTypeID(s);
};
var descriptor = new ActionDescriptor();
var list = new ActionList();
var reference = new ActionReference();
reference.putName( s2t( "layer" ), layerName );
descriptor.putReference( s2t( "null" ), reference );
descriptor.putEnumerated( s2t( "selectionModifier" ), s2t( "selectionModifierType" ), s2t( "removeFromSelection" ));
descriptor.putBoolean( s2t( "makeVisible" ), false );
descriptor.putList( s2t( "layerID" ), list );
executeAction( s2t( "select" ), descriptor, DialogModes.NO );
}
Copy link to clipboard
Copied
@Mohamed Hameed – that isn't so simple for me, perhaps somebody else has an idea on how to select all top-level layers and groups, but not the content of the groups...
Copy link to clipboard
Copied
I zoned out … what exactly are you trying to achieve?
Please post screenshots illustrating both the start situation and the end situation.
Copy link to clipboard
Copied
Thank you for your interest, sir
I just want when selecting the group it selects and activates the layers inside it
I made an iterative loop to select the layers of the group, but it activates each layer with an iterative step, not all layers
I used these lines
But it didn't work to select all the layers inside the group
var aDoc = app.activeDocument;
var set = aDoc.layerSets.getByName("Hameed");
aDoc.activeLayer = set;
for (var i = 0; i < set.layers.length; i++) {
var layer = set.layers;
var aDoc = app.activeDocument;
aDoc.activeLayer=set.layers[i]
}
I just want to activate the layers inside the group
to be excluded from the overall selection of layers