Participating Frequently
March 2, 2021
Answered
Select layers by kind script
- March 2, 2021
- 5 replies
- 3889 views
Photoshop, InDeign and Illustrator all use layers/groups to organise stacking order.
Acrobat uses the term "layers" as they are familiar, however, layers in Acrobat are actually called "optional content groups" or OCG. These behave as filters in the sense that conent can be isolated and have their order changed or have visibility turned on and off. This does not affect stacking order. OCG essentially behave in a similar way to visibility filters in the Photoshop layer panel.
I agree with JJMack that it is potentially damaging to the composition of the image to change the stacking order of layers by isolating them into layer groups based on their layer kind. I say potentially as it will depend on how simple the layer structure is. There are scripts to rename or colour layers if the idea is to have layers that are easier to identify and still retain more complex stacking orders.
Here are some links that may be of interest:
https://community.adobe.com/t5/photoshop/select-only-the-text-layers/m-p/11017397
https://community.adobe.com/t5/photoshop/select-all-text-layers/m-p/8489549
https://github.com/nvkzNemo/experimental-ps-scripts/blob/master/selectAllTextLayers.jsx
Here is a proof of concept script, it only works on text layers, however you can easily extend the script for additional layer kinds:
// community.adobe.com/t5/photoshop/select-layers-by-kind-script/td-p/11867138
// community.adobe.com/t5/photoshop/select-only-the-text-layers/m-p/11017843
// DmitryEgorov
// Select ALL Layers of Specific Layer Kind - Works with layer sets and nested sets!
var AM = new ActionManager
if (app.documents.length) selectLayerKind ()
function selectLayerKind() {
var output = [],
from = AM.getDocProperty('hasBackgroundLayer') ? 0 : 1,
to = AM.getDocProperty('numberOfLayers')
if (to) AM.deselectLayers ()
for (var i = from; i <= to; i++) {
// Any Layer = 0
// Pixel Layer = 1 (including Background layer)
// Adjustment Layer = 2
// Text Layer = 3
// Vector Layer = 4
// SmartObject Layer = 5
// Video Layer = 6
// LayerGroup Layer = 7
// 3D Layer = 8
// Gradient Layer = 9
// Pattern Layer = 10
// Solid Color Layer = 11
// Background Layer = 12
// HiddenSectionBounder = 13
if (AM.getLayerProperty('layerKind', i) == 3) AM.selectLayerByIndex(i, true) // Change the number as above!
}
/* Stephen Marsh 2021 - Call the function to group selected layers and name as "Text Layers" */
makeGroup("Text Layers", 7, 8, "Text Layers");
}
function ActionManager() {
this.getDocProperty = function (property) {
property = s2t(property)
var ref = new ActionReference()
ref.putProperty(s2t("property"), property)
ref.putEnumerated(s2t("document"), s2t("ordinal"), s2t("targetEnum"))
return getDescValue(executeActionGet(ref), property)
}
this.getLayerProperty = function (property, index) {
property = s2t(property)
var ref = new ActionReference()
ref.putProperty(s2t("property"), property)
ref.putIndex(s2t("layer"), index)
return getDescValue(executeActionGet(ref), property)
}
this.selectLayerByIndex = function (idx, addToSelection) {
var desc = new ActionDescriptor()
var ref = new ActionReference()
ref.putIndex(s2t("layer"), idx)
desc.putReference(s2t("null"), ref)
if (addToSelection) desc.putEnumerated(s2t("selectionModifier"), s2t("addToSelectionContinuous"), s2t("addToSelection"))
executeAction(s2t("select"), desc, DialogModes.NO);
}
this.deselectLayers = function () {
var desc = new ActionDescriptor()
var ref = new ActionReference()
ref.putEnumerated(s2t("layer"), s2t("ordinal"), s2t("targetEnum"))
desc.putReference(s2t("null"), ref)
executeAction(s2t("selectNoLayers"), desc, DialogModes.NO)
}
function getDescValue(desc, property) {
switch (desc.getType(property)) {
case DescValueType.OBJECTTYPE:
return (desc.getObjectValue(property));
break;
case DescValueType.LISTTYPE:
return desc.getList(property);
break;
case DescValueType.REFERENCETYPE:
return desc.getReference(property);
break;
case DescValueType.BOOLEANTYPE:
return desc.getBoolean(property);
break;
case DescValueType.STRINGTYPE:
return desc.getString(property);
break;
case DescValueType.INTEGERTYPE:
return desc.getInteger(property);
break;
case DescValueType.LARGEINTEGERTYPE:
return desc.getLargeInteger(property);
break;
case DescValueType.DOUBLETYPE:
return desc.getDouble(property);
break;
case DescValueType.ALIASTYPE:
return desc.getPath(property);
break;
case DescValueType.CLASSTYPE:
return desc.getClass(property);
break;
case DescValueType.UNITDOUBLE:
return (desc.getUnitDoubleValue(property));
break;
case DescValueType.ENUMERATEDTYPE:
return (t2s(desc.getEnumerationValue(property)));
break;
case DescValueType.RAWTYPE:
var tempStr = desc.getData(property);
var rawData = new Array();
for (var tempi = 0; tempi < tempStr.length; tempi++) {
rawData[tempi] = tempStr.charCodeAt(tempi);
}
return rawData;
break;
default:
break;
};
}
function s2t(s) { return stringIDToTypeID(s) }
function t2s(t) { return typeIDToStringID(t) }
}
/* Stephen Marsh 2021 - Function to group selected layers */
function makeGroup(name2, layerSectionStart, layerSectionEnd, name3) {
var c2t = function (s) {
return app.charIDToTypeID(s);
};
var s2t = function (s) {
return app.stringIDToTypeID(s);
};
var descriptor = new ActionDescriptor();
var descriptor2 = new ActionDescriptor();
var reference = new ActionReference();
var reference2 = new ActionReference();
reference.putClass(s2t("layerSection"));
descriptor.putReference(c2t("null"), reference);
reference2.putEnumerated(s2t("layer"), s2t("ordinal"), s2t("targetEnum"));
descriptor.putReference(s2t("from"), reference2);
descriptor2.putString(s2t("name"), name2);
descriptor.putObject(s2t("using"), s2t("layerSection"), descriptor2);
descriptor.putInteger(s2t("layerSectionStart"), layerSectionStart);
descriptor.putInteger(s2t("layerSectionEnd"), layerSectionEnd);
descriptor.putString(s2t("name"), name3);
executeAction(s2t("make"), descriptor, DialogModes.NO);
}
Already have an account? Login
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.