How to check layer kind using javascript

Engaged ,
Sep 01, 2022 Sep 01, 2022

Copy link to clipboard

Copied

Is it possible using javascript to check what is kind of selected layer? To be more precise, i need to check is it selected layer art layer or adjustment layer and base on that procede with some other scripts?

TOPICS
Actions and scripting

Views

80

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Sep 01, 2022 Sep 01, 2022

@milevic – you can try:

 

https://community.adobe.com/t5/forums/searchpage/tab/message?advanced=false&allow_punctuation=false&q=layer.kind%20%3D%3D%20LayerKind

 

or:

 

https://gist.github.com/MarshySwamp/ef345ef3dec60a843465347ee6fcae2f

 

and:

 

 

// http://creativetuts.com/photoshop-script-determine-layer-kind/
// Photoshop Script
// "Determine Layer Kind"
// Script by Mehmet Şensoy
 
#target photoshop
app.bringToFront();

var doc = app.activeDocument;
var layer = doc.activeLayer;// Save select
...

Likes

Translate

Translate
Community Expert ,
Sep 01, 2022 Sep 01, 2022

Copy link to clipboard

Copied

@milevic – you can try:

 

https://community.adobe.com/t5/forums/searchpage/tab/message?advanced=false&allow_punctuation=false&...

 

or:

 

https://gist.github.com/MarshySwamp/ef345ef3dec60a843465347ee6fcae2f

 

and:

 

 

// http://creativetuts.com/photoshop-script-determine-layer-kind/
// Photoshop Script
// "Determine Layer Kind"
// Script by Mehmet Şensoy
 
#target photoshop
app.bringToFront();

var doc = app.activeDocument;
var layer = doc.activeLayer;// Save selected layer to variable:

function determinelayerkind(layer) {
    if (layer.kind == LayerKind.TEXT) { layerkind = "Text Layer"; return layerkind; } // Text Layer
    else if (layer.kind == LayerKind.SOLIDFILL) { layerkind = "Shape Layer"; return layerkind; } /* Shape Layer */
    else if (layer.kind == LayerKind.BLACKANDWHITE) { layerkind = "Black and White adjustment layer"; return layerkind; } /* Black and White */
    else if (layer.kind == LayerKind.BRIGHTNESSCONTRAST) { layerkind = "Brightness contrast adjustment layer"; return layerkind; } /* Brightness contrast adjustment layer */
    else if (layer.kind == LayerKind.CHANNELMIXER) { layerkind = "Channel mixer adjustment layer"; return layerkind; } /* Channel mixer adjustment layer */
    else if (layer.kind == LayerKind.COLORBALANCE) { layerkind = "Color balance adjustment layer"; return layerkind; } /* Color balance adjustment layer */
    else if (layer.kind == LayerKind.CURVES) { layerkind = "Curves adjustment layer"; return layerkind; } /* Curves adjustment layer */
    else if (layer.kind == LayerKind.EXPOSURE) { layerkind = "Exposure layer"; return layerkind; } /* Exposure layer */
    else if (layer.kind == LayerKind.GRADIENTFILL) { layerkind = "Gradient fill"; return layerkind; } /* Gradient fill */
    else if (layer.kind == LayerKind.GRADIENTMAP) { layerkind = "Gradient map adjustment layer"; return layerkind; } /* Gradient map adjustment layer */
    else if (layer.kind == LayerKind.HUESATURATION) { layerkind = "Hue saturation adjustment layer"; return layerkind; } /* Hue saturation adjustment layer */
    else if (layer.kind == LayerKind.INVERSION) { layerkind = "Invert adjustment layer"; return layerkind; } /* Invert adjustment layer */
    else if (layer.kind == LayerKind.LAYER3D) { layerkind = "3D layer"; return layerkind; } /* 3D layer */
    else if (layer.kind == LayerKind.LEVELS) { layerkind = "Levels adjustment layer"; return layerkind; } /* Levels adjustment layer */
    else if (layer.kind == LayerKind.NORMAL) { layerkind = "Normal layer"; return layerkind; } /* Normal Layer */
    else if (layer.kind == LayerKind.PATTERNFILL) { layerkind = "Pattern fill layer"; return layerkind; } /* Pattern fill layer */
    else if (layer.kind == LayerKind.PHOTOFILTER) { layerkind = "Photo filter layer"; return layerkind; } /* Photo filter layer */
    else if (layer.kind == LayerKind.POSTERIZE) { layerkind = "Posterize adjustment layer"; return layerkind; } /* Posterize adjustment layer */
    else if (layer.kind == LayerKind.SELECTIVECOLOR) { layerkind = "Selective color adjustment layer."; return layerkind; } /* Selective color adjustment layer */
    else if (layer.kind == LayerKind.SMARTOBJECT) { layerkind = "Smart object layer"; return layerkind; } /* Smart object layer */
    else if (layer.kind == LayerKind.THRESHOLD) { layerkind = "Threshold adjustment layer"; return layerkind; } /* Threshold adjustment layer */
    else if (layer.kind == LayerKind.VIBRANCE) { layerkind = "Vibrance layer"; return layerkind; } /* Vibrance layer */
    else if (layer.kind == LayerKind.VIDEO) { layerkind = "Video layer"; return layerkind; } /* Video layer */

    else if (app.activeDocument.activeLayer.typename === "LayerSet" && app.activeDocument.activeLayer.layers.length === 0) { layerkind = "Layer Set"; return layerkind; } /* Layer Set */
    else if (app.activeDocument.activeLayer.typename === "LayerSet" && app.activeDocument.activeLayer.layers.length > 0) { layerkind = "Layer Set + Content"; return layerkind; } /* Layer Set - Must have content */
}

alert("Current Layer is a " + determinelayerkind(layer));

 

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Sep 01, 2022 Sep 01, 2022

Copy link to clipboard

Copied

This is great job. Thanks @Stephen_A_Marsh 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 02, 2022 Sep 02, 2022

Copy link to clipboard

Copied

When I need to figure out what type of layer I'm working with, I run the following code:

s2t = stringIDToTypeID;
(r = new ActionReference()).putProperty(s2t('property'), p = s2t('layerKind'));
r.putEnumerated(s2t('layer'), s2t('ordinal'), s2t('targetEnum'));
var layerKind = executeActionGet(r).getInteger(p);

the values we can get are:

const kAnySheet             = 0; 
const kPixelSheet           = 1;  
const kAdjustmentSheet      = 2;  
const kTextSheet            = 3;  
const kVectorSheet          = 4;  
const kSmartObjectSheet     = 5;  
const kVideoSheet           = 6;  
const kLayerGroupSheet      = 7;  
const k3DSheet              = 8;  
const kGradientSheet        = 9;  
const kPatternSheet         = 10;  
const kSolidColorSheet      = 11;  
const kBackgroundSheet      = 12;  
const kHiddenSectionBounder = 13;  

If you know that you are working with an adjustment layer, then its type can be found like this:

s2t = stringIDToTypeID;
(r = new ActionReference()).putProperty(s2t('property'), p = s2t('adjustment'));
r.putEnumerated(s2t('layer'), s2t('ordinal'), s2t('targetEnum'));
alert(typeIDToStringID(executeActionGet(r).getList(p).getObjectType(0)));

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 02, 2022 Sep 02, 2022

Copy link to clipboard

Copied

LATEST

@jazz-y – now that you mention this, I recall it being discussed in the past...

 

Using your AM examples, I made the equivalent of the DOM script from Mehmet Şensoy:

 

s2t = stringIDToTypeID;
(r = new ActionReference()).putProperty(s2t('property'), p = s2t('layerKind'));
r.putEnumerated(s2t('layer'), s2t('ordinal'), s2t('targetEnum'));
var layerKind = executeActionGet(r).getInteger(p);

if (layerKind == 0) {
    alert("kAnySheet");
}
else if (layerKind == 1) {
    alert("kPixelSheet");
}
else if (layerKind == 2) {
    s2t = stringIDToTypeID;
    (r = new ActionReference()).putProperty(s2t('property'), p = s2t('adjustment'));
    r.putEnumerated(s2t('layer'), s2t('ordinal'), s2t('targetEnum'));
    alert("kAdjustmentSheet: " + typeIDToStringID(executeActionGet(r).getList(p).getObjectType(0)));
}
else if (layerKind == 3) {
    alert("kTextSheet");
}
else if (layerKind == 4) {
    alert("kVectorSheet");
}
else if (layerKind == 5) {
    alert("kSmartObjectSheet");
}
else if (layerKind == 6) {
    alert("kVideoSheet");
}
else if (layerKind == 7) {
    alert("kLayerGroupSheet");
}
else if (layerKind ==  {
    alert("k3DSheet");
}
else if (layerKind == 9) {
    alert("kGradientSheet");
}
else if (layerKind == 10) {
    alert("kPatternSheet");
}
else if (layerKind == 11) {
    alert("kSolidColorSheet");
}
else if (layerKind == 12) {
    alert("kBackgroundSheet");
}
else if (layerKind == 13) {
    alert("kHiddenSectionBounder");
}
else {
    alert("Why am I seeing this message?!");
}

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines