Skip to main content
Known Participant
June 25, 2023
Question

How to get Brightness/Contrast levels of a layer, using .jsx Adobe Photoshop scripting?

  • June 25, 2023
  • 1 reply
  • 1118 views

I have a .jsx Photoshop script workflow that begins by first applying a Brightness/Contrast adjustment to the current image, then uses the "Auto" setting.

 

For the next step, I'd like to get the properties of that Brightness/Contrast layer, and then IF the brightness meets a certain criteria? Change the brightness to some other value. Same for the contrast.

 

What's the best way to automate this in my .jsx file?

 

Thanks!

This topic has been closed for replies.

1 reply

c.pfaffenbichler
Community Expert
Community Expert
June 25, 2023

You should be able to get the Layer’s settings via AM-code. 

Known Participant
June 25, 2023

Any idea what that code would look like? I tried for HOURS yesterday and just couldn't get it to work. Kept throwing errors. Here's one I tried that outputs a Photoshop general error:

    var ref = new ActionReference();
    ref.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
    var desc = executeActionGet(ref);
    var adjs = desc.getList(charIDToTypeID("Adjs"));
    var brConDesc = adjs.getObjectValue(0);
    var brightness = brConDesc.getInteger(charIDToTypeID("Brgh"));
    var contrast = brConDesc.getInteger(charIDToTypeID("Cntr"));
   
    alert("Brightness: " + brightness + "\nContrast: " + contrast);
Known Participant
June 25, 2023
quote

Any idea what that code would look like? I tried for HOURS yesterday and just couldn't get it to work. Kept throwing errors.

 
By @the_king_of_goats

 

It would be better if you looked at the topics nearby or used the search.

 

 


adj.brightness is showing up as undefined when I run this function. Not sure why because the active layer should be a Brightness/Contrast layer.

    // Apply auto brightness/contrast adjustment
    var autoDescriptor = new ActionDescriptor();
    var reference = new ActionReference();
    reference.putClass(stringIDToTypeID("adjustmentLayer"));
    autoDescriptor.putReference(stringIDToTypeID("null"), reference);
    var typeDescriptor = new ActionDescriptor();
    var brightnessDescriptor = new ActionDescriptor();
    brightnessDescriptor.putBoolean(stringIDToTypeID("auto"), true);
    brightnessDescriptor.putBoolean(stringIDToTypeID("useLegacy"), false);
    typeDescriptor.putObject(stringIDToTypeID("type"), stringIDToTypeID("brightnessEvent"), brightnessDescriptor);
    autoDescriptor.putObject(stringIDToTypeID("using"), stringIDToTypeID("adjustmentLayer"), typeDescriptor);
    var resultDesc = executeAction(stringIDToTypeID("make"), autoDescriptor, DialogModes.NO);

    var ref = new ActionReference();
    ref.putEnumerated(charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt')); // reference is active layer
    var layerDesc = executeActionGet(ref);
    var layerID = layerDesc.getInteger(stringIDToTypeID('layerID'));

    // Use the function you found to get the brightness descriptor
    var brConDesc = get_contrast_desc(layerID);
 
function get_contrast_desc(id) {
    try {
        var r = new ActionReference();
        var d = new ActionDescriptor();

        r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("json"));
        r.putEnumerated(stringIDToTypeID("document"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));
        d.putReference(stringIDToTypeID("null"), r);

        d.putInteger(stringIDToTypeID("layerID"), id);
        d.putBoolean(stringIDToTypeID("layerInfo"), true);

        d.putBoolean(stringIDToTypeID( "includeAncestors" ), false);

        eval("var json="+executeAction(stringIDToTypeID("get"), d, DialogModes.NO).getString(stringIDToTypeID("json")));

        var d = new ActionDescriptor();

        if (!json.layers) return d;
        if (json.layers.length != 1) return d;
        if (json.layers[0].id != id) return d;

        var adj = json.layers[0].adjustment;
        alert(adj.brightness); // shows undefined, even though the active layer is a Brightness/Contrast layer

        if (!adj) return d;

        if (adj.brightness != undefined) d.putInteger(stringIDToTypeID("brightness"), adj.brightness);
        if (adj.center     != undefined) d.putInteger(stringIDToTypeID("center"),     adj.center);
        if (adj.useLegacy  != undefined) d.putBoolean(stringIDToTypeID("useLegacy"),  adj.useLegacy);

        return d;
        }
    catch (e) { throw(e); }
}