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

Can someone help with this script?

Contributor ,
Oct 19, 2025 Oct 19, 2025

I am trying to get a script that selects the background of a layer and fills it with green.  This is repeated for all layers.  There might well be more than one error, but so far, it is erroring out "doc.selection.load(layer transparency = false);"  

mangurian_0-1760910593698.png

 

#target photoshop
app.activeDocument.suspendHistory("Fill Background Green All Layers", "main()");

function main() {
    var doc = app.activeDocument;
    var originalLayer = doc.activeLayer;

    for (var i = 0; i < doc.layers.length; i++) {
        var layer = doc.layers[i];
        if (layer.kind == LayerKind.NORMAL && layer.visible) {
            doc.activeLayer = layer;

            // Select transparent areas (background)
            doc.selection.selectAll();
            doc.selection.load(layer transparency = false);
            doc.selection.invert();

            // Set green color
            var fillColor = new SolidColor();
            fillColor.rgb.red = 0;
            fillColor.rgb.green = 255;
            fillColor.rgb.blue = 0;

            // Fill the selection
            doc.selection.fill(fillColor, ColorBlendMode.NORMAL, 100, false);
            doc.selection.deselect();
        }
    }

    doc.activeLayer = originalLayer;
}

 

TOPICS
Actions and scripting , Windows
140
Translate
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
Adobe
Community Expert ,
Oct 19, 2025 Oct 19, 2025

@mangurian 

 

Where did you get the code from, AI?

 

doc.selection.load(layer transparency = false);

 

https://theiviaxx.github.io/photoshop-docs/Photoshop/Selection/load.html

 

Translate
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
Contributor ,
Oct 20, 2025 Oct 20, 2025

yes......chat GPT

Translate
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 ,
Oct 23, 2025 Oct 23, 2025
LATEST

@mangurian 

 

Did you try the corrected script that I posted? It works, but I don't know if the result is what you're looking for or not.

Translate
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 ,
Oct 19, 2025 Oct 19, 2025

@mangurian 

 

Try this:

 

#target photoshop
app.activeDocument.suspendHistory("Fill Background Green All Layers", "main()");

function main() {
    var doc = app.activeDocument;
    var originalLayer = doc.activeLayer;

    // Ensure that a layer is selected - by r-bin
    try {
        activeDocument.activeLayer.link(activeDocument.activeLayer);
    } catch (e) { }


    for (var i = 0; i < doc.layers.length; i++) {
        var layer = doc.layers[i];
        if (layer.kind == LayerKind.NORMAL && layer.visible) {
            doc.activeLayer = layer;

            // Select all
            //doc.selection.selectAll();
            
            // AM code to load transparency as a selection
            var idsetd = charIDToTypeID("setd");
            var desc = new ActionDescriptor();
            var idnull = charIDToTypeID("null");
            var ref = new ActionReference();
            ref.putProperty(charIDToTypeID("Chnl"), charIDToTypeID("fsel"));
            desc.putReference(idnull, ref);
            var idT = charIDToTypeID("T   ");
            var ref2 = new ActionReference();
            ref2.putEnumerated(charIDToTypeID("Chnl"), charIDToTypeID("Chnl"), charIDToTypeID("Trsp"));
            desc.putReference(idT, ref2);
            executeAction(idsetd, desc, DialogModes.NO);

            doc.selection.invert();

            // Set green color
            var fillColor = new SolidColor();
            fillColor.rgb.red = 0;
            fillColor.rgb.green = 255;
            fillColor.rgb.blue = 0;

            // Fill the selection
            doc.selection.fill(fillColor, ColorBlendMode.NORMAL, 100, false);
            doc.selection.deselect();
        }
    }

    doc.activeLayer = originalLayer;
}

 

I'm not sure if this is a what you want, or the opposite?!

Translate
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