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
Please post screenshots, because
»I just want to activate the layers inside the group
to be excluded from the overall selection of layers«
seems contradictory to me.
Copy link to clipboard
Copied
Good
The first picture shows
- I want to define the group and the layers of the group
second picture
- I want to exclude the group and its layers outside of selecting all layers
In order to understand the topic clearly
I want to select, merge and delete all unused layers that I don't want
But the selection includes the group layers I want
And I don't want to delete it
Copy link to clipboard
Copied
I am sorry but I do not seem to understand the description.
Are you trying to select ALL Layers expect the Group (identified by name) and the Layers in that Group?
Copy link to clipboard
Copied
Sir, I don't know if there is a problem with my explanation of what I want or what??
I have attached pictures and code, however I see that there is a problem in understanding the problem
Well sir, thank you for your interest
Copy link to clipboard
Copied
In the second screenshot not all the Layers outside of the Group are selected and all the Layer in the Group are – so I am not sure which Layers should be selected at the end of the process?
Copy link to clipboard
Copied
Yes sir
The group layer is excluded from the selection
Why are the layers inside the group not excluded as the group was excluded?
- This is a suggestion
Another suggestion
- I want when selecting a group by name, all layers within the group are also selected
Is this simple or complicated
Copy link to clipboard
Copied
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 theName = "xxx";
var ref = new ActionReference();
ref.putProperty (stringIDToTypeID ("property"), stringIDToTypeID ("numberOfLayers"));
ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var applicationDesc = executeActionGet(ref);
var theNumber = applicationDesc.getInteger(stringIDToTypeID("numberOfLayers"));
var theLayers = getLayersAndItsParentsIndexAndID(theName);
var numberOne = theLayers[theLayers.length-1][0][1] - 1;
var numberTwo = theLayers[0][theLayers[0].length-1][1] + 1;
var add = false;
for (var m = 0; m < numberOne; m++) {
selectLayerByIndex(m, add);
var add = true;
};
for (var n = numberTwo; n <= theNumber; n++) {
selectLayerByIndex(n, true);
};
};
////////////////////////////////////
////// collect layers and their parents if the top level group has a specific name //////
function getLayersAndItsParentsIndexAndID (folderName) {
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var applicationDesc = executeActionGet(ref);
var theNumber = applicationDesc.getInteger(stringIDToTypeID("numberOfLayers"));
// anumber is intended to keep track of layerset depth;
var aNumber = 0;
var theArray = new Array;
//var theGroups = new Array;
// work through layers;
for (var m = theNumber; m >= 0; m--) {
try {
var ref = new ActionReference();
ref.putIndex( charIDToTypeID( "Lyr " ), m);
var layerDesc = executeActionGet(ref);
var layerSet = typeIDToStringID(layerDesc.getEnumerationValue(stringIDToTypeID("layerSection")));
var isBackground = layerDesc.getBoolean(stringIDToTypeID("background"));
var theName = layerDesc.getString(stringIDToTypeID('name'));
var theID = layerDesc.getInteger(stringIDToTypeID('layerID'));
//var hasLayerMask = layerDesc.hasKey(charIDToTypeID("Usrs"));
var thisArray = [[theName, m, theID]];
////////////////////////////////////
// if group start:
if (layerSet == "layerSectionStart" && isBackground != true/* && theName == folderName*/) {
if (aNumber == 0) {var setArray = [[theName, m, theID]]}
else {
// include groups in array;
for (var o = setArray.length - 1; o >= 0; o--) {thisArray.push(setArray[o])};
// set array;
setArray.push([theName, m, theID])
};
//theGroups.push([theName, m, theID]);
// add to mark group;
aNumber++
};
// if group end;
if (layerSet == "layerSectionEnd" && isBackground != true) {
// subtract to mark end of group;
aNumber--;
if (aNumber == 0) {var setArray = []}
else {setArray.pop()}
};
// if neither group start or end;
if (layerSet != "layerSectionEnd" && layerSet != "layerSectionStart" && isBackground != true) {
// if anumber is 0 layer is top level;
if (aNumber != 0) {
for (var o = setArray.length - 1; o >= 0; o--) {thisArray.push(setArray[o])};
// check the to plevel folder name;
if (thisArray[thisArray.length-1][0] == folderName) {
theArray.push(thisArray)
}
};
};
////////////////////////////////////
} catch (e) {};
};
// the results;
return theArray
};
////// by mike hale, via paul riggott //////
function selectLayerByIndex(index,add){
add = undefined ? add = false:add
var ref = new ActionReference();
ref.putIndex(charIDToTypeID("Lyr "), index);
var desc = new ActionDescriptor();
desc.putReference(charIDToTypeID("null"), ref );
if(add) desc.putEnumerated( stringIDToTypeID( "selectionModifier" ), stringIDToTypeID( "selectionModifierType" ), stringIDToTypeID( "addToSelection" ) );
desc.putBoolean( charIDToTypeID( "MkVs" ), false );
try{
executeAction(charIDToTypeID("slct"), desc, DialogModes.NO );
}catch(e){
alert(e.message);
}
};
////// based on code by mike hale, via paul riggott //////
function selectLayerByID(id,add){
try {
add = undefined ? add = false:add
var ref = new ActionReference();
ref.putIdentifier(charIDToTypeID("Lyr "), id);
var desc = new ActionDescriptor();
desc.putReference(charIDToTypeID("null"), ref );
if(add) desc.putEnumerated( stringIDToTypeID( "selectionModifier" ), stringIDToTypeID( "selectionModifierType" ), stringIDToTypeID( "addToSelection" ) );
desc.putBoolean( charIDToTypeID( "MkVs" ), false );
try{
executeAction(charIDToTypeID("slct"), desc, DialogModes.NO );
}catch(e){
alert(e.message);
}
} catch (e) {}
};
Copy link to clipboard
Copied
I made a file with exactly the same layers as yours
When I run the script, this problem appears
Clarification: I am working on Photoshop CS5
Is the version related to the malfunction of the script?
Copy link to clipboard
Copied
It seems you names the Group »XXX«, not »xxx«.
Maybe you should add a check with an if-clause to verify that the Array theLayers has a length greater than 0.
Copy link to clipboard
Copied
Indeed sir
The group name is XXX, not xxx
Sir, I think I tired you too much
Well, you don't have to, sir
Sorry to bother you with me
And thank you very much for your interest in providing assistance
Copy link to clipboard
Copied
If you want to identify the Folder’s name case-insensitively you should probably use a RegExp instead of a String.
Copy link to clipboard
Copied
Ok bro, I tried the last code and it worked fine
But I want the idea of defining the folder with the layers it contains by the name of the folder
Also, I discovered that the last code excludes only one folder based on the name
If I wanted more than one folder
Or a folder and a layer, how is that??
Copy link to clipboard
Copied
Ok bro, I tried the last code and it worked fine
But I want the idea of defining the folder with the layers it contains by the name of the folder
I don’t understand what you mean.
Please post screenshot that actually illustrate the situation as is and the result you want to achieve.
Also, I discovered that the last code excludes only one folder based on the name
If I wanted more than one folder
Or a folder and a layer, how is that??
The code is in editable form, so feel free to amend it to meet your needs.
Copy link to clipboard
Copied
Sir, I already put a picture in the previous comment
I mean how to select the folder by name and also with its layers
As shown in the picture
Sir, I thought of a method, which is to select the folder and ungroup it in order to select the layers, then create a new folder with the same name and transfer the layers to it
Is this method appropriate, or is there an easier way to select the folder and its layers?
Copy link to clipboard
Copied
The screenshot does not seem very helpful in that it essentially only shows the one Group.
So for example the issue of multiple Groups of identical names is not illustrated.
I mean how to select the folder by name and also with its layers
Not the most elegant approach, but it should work, if I understand your intention correctly.
code edited 2023-08-20 to include groups of the name within groups
// selectAllGroupsOfNameAndLayersInTheGroups
// select all groups of a certain name and the layers in those groups;
// 2023, use it at your own risk;
if (app.documents.length > 0) {
var theName = "xxx";
var ref = new ActionReference();
ref.putProperty (stringIDToTypeID ("property"), stringIDToTypeID ("numberOfLayers"));
ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var applicationDesc = executeActionGet(ref);
var theNumber = applicationDesc.getInteger(stringIDToTypeID("numberOfLayers"));
var theLayers = getLayersAndItsParentsIndexAndID(theName);
var add = false;
for (var m = 0; m < theLayers.length; m++) {
var check = false;
for (var n = theLayers[m].length-1; n > 0; n--) {
if (check == true) {
selectLayerByIndex(theLayers[m][n][1], add);
};
if (theLayers[m][n][0] == theName) {
selectLayerByIndex(theLayers[m][n][1], add);
add = true;
check = true
};
};
selectLayerByIndex(theLayers[m][0][1], add);
};
};
////////////////////////////////////
////// collect layers and their parents if the top level group has a specific name //////
function getLayersAndItsParentsIndexAndID (folderName) {
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var applicationDesc = executeActionGet(ref);
var theNumber = applicationDesc.getInteger(stringIDToTypeID("numberOfLayers"));
// anumber is intended to keep track of layerset depth;
var aNumber = 0;
var theArray = new Array;
//var theGroups = new Array;
// work through layers;
for (var m = theNumber; m >= 0; m--) {
try {
var ref = new ActionReference();
ref.putIndex( charIDToTypeID( "Lyr " ), m);
var layerDesc = executeActionGet(ref);
var layerSet = typeIDToStringID(layerDesc.getEnumerationValue(stringIDToTypeID("layerSection")));
var isBackground = layerDesc.getBoolean(stringIDToTypeID("background"));
var theName = layerDesc.getString(stringIDToTypeID('name'));
var theID = layerDesc.getInteger(stringIDToTypeID('layerID'));
//var hasLayerMask = layerDesc.hasKey(charIDToTypeID("Usrs"));
var thisArray = [[theName, m, theID]];
////////////////////////////////////
// if group start:
if (layerSet == "layerSectionStart" && isBackground != true) {
if (aNumber == 0) {var setArray = [[theName, m, theID]]}
else {
// include groups in array;
for (var o = setArray.length - 1; o >= 0; o--) {thisArray.push(setArray[o])};
// set array;
setArray.push([theName, m, theID])
};
// add to mark group;
aNumber++
};
// if group end;
if (layerSet == "layerSectionEnd" && isBackground != true) {
// subtract to mark end of group;
aNumber--;
if (aNumber == 0) {var setArray = []}
else {setArray.pop()}
};
// if neither group start or end;
if (layerSet != "layerSectionEnd" && layerSet != "layerSectionStart" && isBackground != true) {
// if anumber is 0 layer is top level;
if (aNumber != 0) {
for (var o = setArray.length - 1; o >= 0; o--) {thisArray.push(setArray[o])};
// check the to level folder name;
var theCheck = false;
for (var a = thisArray.length-1; a > 0; a--) {
if (thisArray[a][0] == folderName) {theCheck = true}
};
if (theCheck == true) {
theArray.push(thisArray)
}
};
};
////////////////////////////////////
} catch (e) {};
};
// the results;
return theArray
};
////// by mike hale, via paul riggott //////
function selectLayerByIndex(index,add){
add = undefined ? add = false:add
var ref = new ActionReference();
ref.putIndex(charIDToTypeID("Lyr "), index);
var desc = new ActionDescriptor();
desc.putReference(charIDToTypeID("null"), ref );
if(add) desc.putEnumerated( stringIDToTypeID( "selectionModifier" ), stringIDToTypeID( "selectionModifierType" ), stringIDToTypeID( "addToSelection" ) );
desc.putBoolean( charIDToTypeID( "MkVs" ), false );
try{
executeAction(charIDToTypeID("slct"), desc, DialogModes.NO );
}catch(e){
alert(e.message);
}
};
before running the Script:
after running the Script: