Copy link to clipboard
Copied
Hey!
I am fairly new to JS/photoshop scripting, so this is probably a super simple question.
I am just looking for help in creating the first part of a script that will select the layers based on position and also a way to select layers based on layer type.
I will be running a script to select the first Art Layer in a file that has multiple layers.
Thank you all for the help, I hope to be able to contribute as well soon!
This script dupes the selected layers into a temporary doc in order to count them. This script does not include any selected layer groups in the count, it is not bulletproof in my tests with nested groups inside groups, so YMMV:
selectedLayersLength();
function selectedLayersLength() {
// Dupe selected layers to temp doc
var idMk = charIDToTypeID("Mk ");
var desc302 = new ActionDescriptor();
var idnull = charIDToTypeID("null");
var ref122 = new ActionReference();
var
...
Copy link to clipboard
Copied
I think I may have solved this myself.
Would this work?
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Can you post a screenshot of the layers panel structure, with the target layer clearly marked, thanks!
Copy link to clipboard
Copied
Hey!
There are a few situations for how this layer structure will be, with very little consistency to start, and I am hoping to make this a Powershell operable code, which are all probably redflags for this...
Situation 1
Just one layer with a layer mask
Situation two
One layer with layer mask, one solid color layer on the bottom. The target layer will not always be name original either
Situation 3
One text layer, one image/artlayer, one solid color layer.
I have a script to select all text layers, so I may just use that script and switch the layer kind to art layers.
This is the thread I got this code from
https://community.adobe.com/t5/photoshop-ecosystem-discussions/select-only-the-text-layers/td-p/1101...
I am assuming that I would switch the text sheetKind from 3 to 1 for Pixel?
Copy link to clipboard
Copied
So far this seems like it works, Stephen, but I am curious if you know a good way to get the length of the selected acitve layers afterwards/
I was trying this, but had no luck.
Copy link to clipboard
Copied
@J.C.C.1 wrote:
So far this seems like it works, Stephen, but I am curious if you know a good way to get the length of the selected acitve layers afterwards/
I was trying this, but had no luck.var doc = app.activeDocumentvar activeSelectionLength = doc.activeDocument.activeLayer.lengthalert(activeSelectionLength)
There is no native method that I am aware of, perhaps there is some AM code... All of the ways that I know rely on "hacks", which is sometimes all that we have.
https://gist.github.com/hilukasz/03b17ee78414aadff995
Copy link to clipboard
Copied
This script groups the selected layers into a temporary layerSet in order to count them. This script does not include any selected layer groups in the count, it is not bulletproof in my tests with nested groups inside groups, so YMMV:
countSelectedLayers();
function countSelectedLayers() {
// Ignores layer groups in the count
var A = [];
var desc11 = new ActionDescriptor();
var ref9 = new ActionReference();
ref9.putClass(stringIDToTypeID('layerSection'));
desc11.putReference(charIDToTypeID('null'), ref9);
var ref10 = new ActionReference();
ref10.putEnumerated(charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt'));
desc11.putReference(charIDToTypeID('From'), ref10);
executeAction(charIDToTypeID('Mk '), desc11, DialogModes.NO);
var gL = activeDocument.activeLayer.layers;
// Notification
alert(gL.length);
for (var i = 0; i < gL.length; i++) {
A.push(gL[i]);
}
executeAction(charIDToTypeID('undo'), undefined, DialogModes.NO);
return A;
}
Copy link to clipboard
Copied
This script dupes the selected layers into a temporary doc in order to count them. This script does not include any selected layer groups in the count, it is not bulletproof in my tests with nested groups inside groups, so YMMV:
selectedLayersLength();
function selectedLayersLength() {
// Dupe selected layers to temp doc
var idMk = charIDToTypeID("Mk ");
var desc302 = new ActionDescriptor();
var idnull = charIDToTypeID("null");
var ref122 = new ActionReference();
var idDcmn = charIDToTypeID("Dcmn");
ref122.putClass(idDcmn);
desc302.putReference(idnull, ref122);
var idNm = charIDToTypeID("Nm ");
desc302.putString(idNm, "", "TempLayerCountDoc", "");
var idUsng = charIDToTypeID("Usng");
var ref123 = new ActionReference();
var idLyr = charIDToTypeID("Lyr ");
var idOrdn = charIDToTypeID("Ordn");
var idTrgt = charIDToTypeID("Trgt");
ref123.putEnumerated(idLyr, idOrdn, idTrgt);
desc302.putReference(idUsng, ref123);
var idVrsn = charIDToTypeID("Vrsn");
desc302.putInteger(idVrsn, 5);
executeAction(idMk, desc302, DialogModes.NO);
// Get the layer count
var layerCount = app.activeDocument.layers.length;
// Close the temp doc
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
// Count notification
alert(layerCount);
}
Copy link to clipboard
Copied
Okay awesome! I think that will work, and I can always set an error message for files with layers nested more than a twice. I think between these two segements of code I have everything I need for this script! Thank you as always!
Copy link to clipboard
Copied
@J.C.C.1 wrote:
Okay awesome! I think that will work, and I can always set an error message for files with layers nested more than a twice. I think between these two segements of code I have everything I need for this script! Thank you as always!
Glad it works for you, I would like a more robust script, however, as long as you know the limitations. Looping into all the nested layer sets within layer sets isn't that easy for me.