Skip to main content
Participant
August 12, 2018
Answered

Layer - new Adjustment Layer - Black And White...

  • August 12, 2018
  • 1 reply
  • 1338 views

Hi all.

I would create a new Black and White Adjustment layer inside a CEP Panel (CC 2018) but it seems a big trouble.

Reading the Photoshop CC Javascript Reference is simple:

var newLayer = app.activeDocument.artLayers.add();

newLayer.kind = LayerKind.BLACKANDWHITE;

Why doesn't work? Nobody knows. Is it a bug?

I know that exist the Script Listener Plugin, but wouldn't use it because I would lose the control of my code.

I don't think that commercial panels are written using the Script Listener Plugin

Are there alternatives or suggestions?

This topic has been closed for replies.
Correct answer r-bin

This will not work. Read the documentation.

Try this.

add_bw_layer("BW Layer", 1,2,3,4,5,6);

add_bw_layer();

///////////////////////////////////////////////////////////////////////////////

function add_bw_layer(name, r,y,g,c,b,m)

    {

    try {

        var d = new ActionDescriptor();

        var r0 = new ActionReference();

        r0.putClass(stringIDToTypeID("adjustmentLayer"));

        d.putReference(stringIDToTypeID("null"), r0);

        var d1 = new ActionDescriptor();

        if (name) d1.putString(stringIDToTypeID("name" ), name);

        var d2 = new ActionDescriptor();

        if (r != undefined) d2.putInteger(stringIDToTypeID("red"),     r);

        if (y != undefined) d2.putInteger(stringIDToTypeID("yellow"),  y);

        if (g != undefined) d2.putInteger(stringIDToTypeID("green"),   g);

        if (c != undefined) d2.putInteger(stringIDToTypeID("cyan"),    c);

        if (b != undefined) d2.putInteger(stringIDToTypeID("blue"),    b);

        if (m != undefined) d2.putInteger(stringIDToTypeID("magenta"), m);

        d1.putObject(stringIDToTypeID("type"), stringIDToTypeID("blackAndWhite"), d2);

        d.putObject(stringIDToTypeID("using"), stringIDToTypeID("adjustmentLayer"), d1);

        executeAction(stringIDToTypeID("make"), d, DialogModes.NO);

        }

    catch (e) { alert(e); throw(e); }

    }

1 reply

r-binCorrect answer
Legend
August 12, 2018

This will not work. Read the documentation.

Try this.

add_bw_layer("BW Layer", 1,2,3,4,5,6);

add_bw_layer();

///////////////////////////////////////////////////////////////////////////////

function add_bw_layer(name, r,y,g,c,b,m)

    {

    try {

        var d = new ActionDescriptor();

        var r0 = new ActionReference();

        r0.putClass(stringIDToTypeID("adjustmentLayer"));

        d.putReference(stringIDToTypeID("null"), r0);

        var d1 = new ActionDescriptor();

        if (name) d1.putString(stringIDToTypeID("name" ), name);

        var d2 = new ActionDescriptor();

        if (r != undefined) d2.putInteger(stringIDToTypeID("red"),     r);

        if (y != undefined) d2.putInteger(stringIDToTypeID("yellow"),  y);

        if (g != undefined) d2.putInteger(stringIDToTypeID("green"),   g);

        if (c != undefined) d2.putInteger(stringIDToTypeID("cyan"),    c);

        if (b != undefined) d2.putInteger(stringIDToTypeID("blue"),    b);

        if (m != undefined) d2.putInteger(stringIDToTypeID("magenta"), m);

        d1.putObject(stringIDToTypeID("type"), stringIDToTypeID("blackAndWhite"), d2);

        d.putObject(stringIDToTypeID("using"), stringIDToTypeID("adjustmentLayer"), d1);

        executeAction(stringIDToTypeID("make"), d, DialogModes.NO);

        }

    catch (e) { alert(e); throw(e); }

    }

Participant
August 12, 2018

Ok great, it works and I like it. But let me ask the big question: Where did you find in the documentation all the string constants you have written? I think the key is the method stringIDToTypeID but I'd like to find in the documentation all possible string values that it can accept. Example: If I would add a second layer, a hue/saturation or a channel mixer, where I can find the correct stringID? Thanks all

Legend
August 12, 2018

Constants can be obtained from ScriptingListenerJS.log. In addition, knowledge gradually comes with experience.
You can also look at this script Script Events Listener