Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
1

Photoshop Scripting - Solid Fill or Shape Layer

Explorer ,
Apr 01, 2020 Apr 01, 2020

Copy link to clipboard

Copied

When I run this code on a "solid fill layer" or a "shape layer" I get the same result

alert(app.activeDocument.activeLayer.kind);

 Result: LayerKind.SOLIDFILL

 

I assume that a regular solid fill layer and a shape layer are both "LayerKind.SOLIDFILL" under the hood. However, I need to perform different functions for a solid fill vs a shape layer. Is there a way to detect if the layer is a shape layer?

 

It appears that a shape layer has two properties that a solid fill layer doesn't have, but I haven't been able to figure out how to test if the properties exist on the layer.

 

Shape layers have these two extra properties:

pathBounds
AGMStrokeStyleInfo

 

Any information would be greatly appreciated

TOPICS
Actions and scripting

Views

3.6K
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

Guide , Apr 01, 2020 Apr 01, 2020

Action Manager uses a more detailed description of layer types:

 

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;

 

 

try to use:

s2t = stringIDToTypeID;
(ref = new ActionReference()).putPrope
...

Votes

Translate
Adobe
Explorer ,
Apr 01, 2020 Apr 01, 2020

Copy link to clipboard

Copied

This seems to work.  If there is a more elegant or more efficient way to do this please let me know.

var actionReference = new ActionReference();
actionReference.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));

var actionDescriptor = executeActionGet(actionReference);

isShapeLayer = false;
for (i = 0; i < actionDescriptor.count; i += 1) {
    if (typeIDToStringID(actionDescriptor.getKey(i)) === "pathBounds") {
        isShapeLayer = true;
    }
}

alert(isShapeLayer);

 

Votes

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
Guide ,
Apr 01, 2020 Apr 01, 2020

Copy link to clipboard

Copied

Action Manager uses a more detailed description of layer types:

 

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;

 

 

try to use:

s2t = stringIDToTypeID;
(ref = new ActionReference()).putProperty(s2t("property"), s2t("layerKind"))
ref.putEnumerated(s2t("layer"), s2t("ordinal"), s2t("targetEnum"))
var activeLayerType = executeActionGet(ref).getInteger (s2t("layerKind"))

alert (activeLayerType)

 

Votes

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
Explorer ,
Apr 02, 2020 Apr 02, 2020

Copy link to clipboard

Copied

Dmitry and Stephen,

 

Thanks for the replies! I really appreciate it.

 

Dmitry your script worked perfectly.

Votes

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 ,
Apr 02, 2020 Apr 02, 2020

Copy link to clipboard

Copied

Yet another option:

 

 
// 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 */ 
}
 
alert ("Current Layer is a " + determinelayerkind(layer));

 

EDIT: I just tested and this script reports a solid fill and a shape as both of a kind of "shape layer" with no distinction between them.

Votes

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 Beginner ,
Feb 29, 2024 Feb 29, 2024

Copy link to clipboard

Copied

That's right, there is no property to distinguish between a shape or a solid color fill .... I need a solution.

Votes

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 ,
Feb 29, 2024 Feb 29, 2024

Copy link to clipboard

Copied

@microbians com 

 

The AM code script from @jazz-y  (marked as the correct answer) would appear to correctly distinguish between shape (4) and color fill (11).

 

Are you having problems adapting that code into say an if/else or some other structure for conditional flow?

Votes

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 Beginner ,
Feb 29, 2024 Feb 29, 2024

Copy link to clipboard

Copied

Sorry finally is solved. I used a sort of simmilar code based on that correct answer. Thanks!

function isLayerShape(l) {

app.activeDocument.activeLayer = l;
var s2t =stringIDToTypeID;
var r = new ActionReference();
r.putEnumerated(s2t("layer"), s2t("ordinal"), s2t("targetEnum"));
var d = new ActionDescriptor();
d.putObject(s2t("object"), s2t("object"), executeActionGet(r));
var obj = executeAction(s2t("convertJSONdescriptor"), d).getString(s2t("json"));
return obj.indexOf("pathBounds")!=-1;

}

Votes

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 ,
Feb 29, 2024 Feb 29, 2024

Copy link to clipboard

Copied

quote

That's right, there is no property to distinguish between a shape or a solid color fill .... I need a solution.


By microbians com

Please provide a fuller description of your problem and, ideally, also a sample file or at least screenshots with the pertinent Panels (Toolbar, Layers, Options Bar, …) visible. 

Votes

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 Beginner ,
Mar 08, 2024 Mar 08, 2024

Copy link to clipboard

Copied

LATEST

I added a control to mantain visibility of the layer, just because the action excecution change it to visible always:

 

 

function isLayerShape(l) {
    var activeDoc = app.activeDocument;
    var currentVisiblity = l.visible;
activeDoc.activeLayer = l;
    var s2t =stringIDToTypeID;
    var r = new ActionReference();
r.putEnumerated(s2t("layer"), s2t("ordinal"), s2t("targetEnum"));
    var d = new ActionDescriptor();
d.putObject(s2t("object"), s2t("object"), executeActionGet(r));
    var obj = executeAction(s2t("convertJSONdescriptor"), d).getString(s2t("json"));
    l.visible=currentVisiblity;
    return obj.indexOf("pathBounds")!=-1;
}

 

Votes

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