Javascript to select background layer and apply solid color

Explorer ,
Mar 21, 2021 Mar 21, 2021

Copy link to clipboard

Copied

Hi i need a Javascript to select background layer and apply solid color for photoshop CC 2019. Can anyone give code for this,

Thanks in advance.

TOPICS
How to

Views

223

Likes

translate

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 2 Correct Answers

Adobe Community Professional , Mar 22, 2021 Mar 22, 2021
Here is my take:       selectBGLayer(false); var sel = app.activeDocument.selection; var fillColor = new SolidColor(); fillColor.rgb.red = 128; // Change as required fillColor.rgb.green = 128; // Change as required fillColor.rgb.blue = 128; // Change as required sel.fill(fillColor, ColorBlendMode.NORMAL, 100, false); function selectBGLayer(makeVisible) { var c2t = function (s) { return app.charIDToTypeID(s); }; var s2t = function (s) { return app.stringIDToTy...

Likes

translate

Translate

Translate
Adobe Community Professional , Mar 22, 2021 Mar 22, 2021
I think that this may be better than the previous two versions:     /* https://community.adobe.com/t5/photoshop/javascript-to-select-background-layer-and-apply-solid-color/td-p/11915853 */ // Select Background Layer & Fill.jsx selectLayerById(1); // Enter layer.id number function selectLayerById(id) { var desc1 = new ActionDescriptor(); var ref1 = new ActionReference(); ref1.putIdentifier(charIDToTypeID('Lyr '), id); desc1.putReference(charIDToTypeID('null'), ref1); execute...

Likes

translate

Translate

Translate
Adobe Community Professional ,
Mar 21, 2021 Mar 21, 2021

Copy link to clipboard

Copied

Can you post a cropped screenshot of the layers panel with all layer sets expanded?

 

Do you mean the locked Background layer at the very bottom of the layers panel, or just any layer that may be named Background, even if it is at the foot of the stack? They are technically different things (a layer with the JavaScript property of backgroundLayer).

 

What solid fill colour? Black, white or something else?

 

Should the colour be hard-coded into the script, or do you want the Photoshop colour picker to pick a colour?

 

What if there is no background layer, or is it expected that there will always be one?

 

Likes

translate

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
Explorer ,
Mar 21, 2021 Mar 21, 2021

Copy link to clipboard

Copied

Hi, Please find the screenshot of the layer panner below. I made some automated script to copy and paste the images from another document layers. I need to move my indicator to background layer which is locked and i need to change color of the background layer from white. 

 

Color shoud be coded into script.

 

 

Karthikeyan98A2_0-1616394543396.png

 

 

Likes

translate

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
Adobe Community Professional ,
Mar 21, 2021 Mar 21, 2021

Copy link to clipboard

Copied

You may need simple action instead. Can you record action on your own? Use keyboard shortcut to select bottom most layer (Alt/Opt + ,), that is my recommendation although if you are certain that document always have Background layer you can also click on it when recording action then simply go to Edit > Fill > Contents: White to record second step to fill with White color.

 

Requirement for Fill command is that layer is visible. Everything can be done in different way using keyboard shortcut to fill but that is more complicated because you must ensure that white color is Foreground or Background color befiore using shortcut to fill.

select and fill.jpg

Likes

translate

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
Adobe Community Professional ,
Mar 22, 2021 Mar 22, 2021

Copy link to clipboard

Copied

Here is my take:

 

example.png

 

 

selectBGLayer(false);

var sel = app.activeDocument.selection;
var fillColor = new SolidColor();
fillColor.rgb.red   =  128; // Change as required
fillColor.rgb.green =  128; // Change as required
fillColor.rgb.blue  =  128; // Change as required
sel.fill(fillColor, ColorBlendMode.NORMAL, 100, false);

function selectBGLayer(makeVisible) {
    var c2t = function (s) {
        return app.charIDToTypeID(s);
    };
    var s2t = function (s) {
        return app.stringIDToTypeID(s);
    };
    var descriptor = new ActionDescriptor();
    var list = new ActionList();
    var reference = new ActionReference();
    reference.putName(s2t("layer"), "Background");
    descriptor.putReference(c2t("null"), reference);
    descriptor.putBoolean(s2t("makeVisible"), makeVisible);
    list.putInteger(1); // Layer ID#
    descriptor.putList(s2t("layerID"), list);
    executeAction(s2t("select"), descriptor, DialogModes.NO);
}

 

Likes

translate

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
Explorer ,
Mar 22, 2021 Mar 22, 2021

Copy link to clipboard

Copied

Thank you very much. Working as expected.

Likes

translate

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
Adobe Community Professional ,
Mar 22, 2021 Mar 22, 2021

Copy link to clipboard

Copied

Thanks, glad it works for you, I'm still playing:

 

/* https://community.adobe.com/t5/photoshop/javascript-to-select-background-layer-and-apply-solid-color/td-p/11915853 */

// Select Background Layer & Fill.jsx

selectLayerById(1); // Enter layer.id number
function selectLayerById(id) {
    var desc1 = new ActionDescriptor();
    var ref1 = new ActionReference();
    ref1.putIdentifier(charIDToTypeID('Lyr '), id);
    desc1.putReference(charIDToTypeID('null'), ref1);
    executeAction(charIDToTypeID('slct'), desc1, DialogModes.NO);
}

if (app.activeDocument.activeLayer.name === 'Background' && app.activeDocument.activeLayer.id === 1) {

    var sel = app.activeDocument.selection;
    var fillColor = new SolidColor();
    fillColor.rgb.red = 128; // Change as required
    fillColor.rgb.green = 128; // Change as required
    fillColor.rgb.blue = 128; // Change as required
    sel.fill(fillColor, ColorBlendMode.NORMAL, 100, false);

} else {
    // alert('This layer does not appear to be a true backgroundLayer');
}

 

Likes

translate

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
Adobe Community Professional ,
Mar 22, 2021 Mar 22, 2021

Copy link to clipboard

Copied

I think that this may be better than the previous two versions:

 

 

/* https://community.adobe.com/t5/photoshop/javascript-to-select-background-layer-and-apply-solid-color/td-p/11915853 */

// Select Background Layer & Fill.jsx

selectLayerById(1); // Enter layer.id number
function selectLayerById(id) {
    var desc1 = new ActionDescriptor();
    var ref1 = new ActionReference();
    ref1.putIdentifier(charIDToTypeID('Lyr '), id);
    desc1.putReference(charIDToTypeID('null'), ref1);
    executeAction(charIDToTypeID('slct'), desc1, DialogModes.NO);
}

if (activeDocument.activeLayer.isBackgroundLayer) {

    var sel = app.activeDocument.selection;
    var fillColor = new SolidColor();
    fillColor.rgb.red = 128; // Change as required
    fillColor.rgb.green = 128; // Change as required
    fillColor.rgb.blue = 128; // Change as required
    sel.fill(fillColor, ColorBlendMode.NORMAL, 100, false);

} else {
    // alert('This layer does not appear to be a true backgroundLayer');
}

 

Likes

translate

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
Explorer ,
Mar 22, 2021 Mar 22, 2021

Copy link to clipboard

Copied

Thank you this works fine. I am just writing code to generate a new pdf from the gif frames. Facing an issue if frames are more than 3. I am attaching the PSD and script here. Can you correct this for me?

 

app.preferences.rulerUnits = Units.PIXELS; // Unit prefernces
//multiFrame();

//test();
if (File(activeDocument.path.fullName+'/'+app.activeDocument.name.slice(0, app.activeDocument.name.length-4)+'_Frames.pdf').exists)
{
var f1 = File(activeDocument.path.fullName+'/'+app.activeDocument.name.slice(0, app.activeDocument.name.length-4)+'_Frames.pdf');
f1.remove();
//alert(activeDocument.path.fullName+'/'+app.activeDocument.name.slice(0, app.activeDocument.name.length-4)+'_Frames.pdf'+'File deleted');
}
app.activeDocument.exportDocument(new File(activeDocument.path.fullName+'/'+app.activeDocument.name.slice(0, app.activeDocument.name.length-4)+'.gif'), ExportType.SAVEFORWEB, ExportOptionsSaveForWeb.COMPUSERVEGIF); // Save for Web GIF
playtime();
//alert(stringIDToTypeID("Ply"));
//executeAction(stringIDToTypeID("Ply"), desc, DialogModes.NO);

function playtime() //NUmber of frames
{
var r = new ActionReference();
r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("frameCount"));
r.putClass(stringIDToTypeID("animationClass"));
var ret = executeActionGet(r);
var fno = ret.getInteger(stringIDToTypeID("frameCount")); //Total Frame
for (var i=1; i<=fno; i++)
{
goToFrame(i, fno);//enter frame you want to goto here. now it shows the first frame;

}
}

function goToFrame(frameNum, fno)
{
var idslct = charIDToTypeID( "slct" );
var desc2 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var ref1 = new ActionReference();
var idanimationFrameClass = stringIDToTypeID( "animationFrameClass" );
ref1.putIndex( idanimationFrameClass, frameNum );
desc2.putReference( idnull, ref1 );
executeAction( idslct, desc2, DialogModes.NO );

if(app.activeDocument.height.value*1.5 > app.activeDocument.width.value)
{
newDocHorz(frameNum, fno);
}
else
{
newDocVerz(frameNum, fno);
}

//app.open(File(activeDocument.path.fullName+'/sample.psd'));
}

function newDocHorz(d, fno)
{
var n = app.activeDocument.name; // Document name
n = n.slice(0, n.length-4); // To remove document Name extension
var savedState = app.activeDocument.activeHistoryState; // Save active history
var filePath = activeDocument.path.fullName;
//app.activeDocument.saveAs(new File(activeDocument.path.fullName+'/'+n+'gif'), new GIFSaveOptions(), true, Extension.NONE);
//app.activeDocument.mergeVisibleLayers();
app.activeDocument.selection.selectAll();
app.activeDocument.selection.copy(true);
app.activeDocument.activeHistoryState = savedState;
var h = app.activeDocument.height.value+200, w = app.activeDocument.width.value+200;
var wid = 0;
if (File(filePath+'/'+n+'_Frames.pdf').exists)
{
app.open(File(activeDocument.path.fullName+'/'+n+'_Frames.pdf'));
wid = w + app.activeDocument.width.value;
app.activeDocument.resizeCanvas(wid, h, AnchorPosition.TOPLEFT);
}
else
{
var ndoc = app.documents.add(w, h, 72, n+'_Frames.pdf', NewDocumentMode.RGB); // Create file
wid = w;
}
// Create file
//app.activeDocument.artLayers.add();
app.activeDocument.paste();
//alert(w+'/'+wid+'/'+ h);
var k = app.activeDocument.activeLayer.bounds;
//alert(k[0]);
app.activeDocument.activeLayer.translate(k[0], h*.01);
var myLayers;
myLayers = activeDocument.artLayers.add(); // Cate New Empty Layer
myLayers.kind = LayerKind.TEXT; // Covert the layer into text layer
myLayers.textItem.contents = "Frame "+d; // Layer content
myLayers.textItem.position = Array(wid-w*.5+52,h-50);
//myLayers.textItem.position = Array(app.activeDocument.width*.5-48, app.activeDocument.height-50);
myLayers.textItem.size = 30;
//app.activeDocument.activeLayer.translate(k[0], app.activeDocument.height-50);
if(d==fno)
{
app.activeDocument.resizeCanvas(wid+200, h, AnchorPosition.TOPLEFT);
app.activeDocument.mergeVisibleLayers();
app.activeDocument.saveAs(new File(filePath+'/'+app.activeDocument.name), PDFSaveOptions); // Save pdf
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES); // Close pdf
}
else
{
app.activeDocument.saveAs(new File(filePath+'/'+app.activeDocument.name), PDFSaveOptions); // Save pdf
}

app.open(File(filePath+'/'+n+'.psd'));
}

 

function newDocVerz(d, fno)
{
var n = app.activeDocument.name; // Document name
n = n.slice(0, n.length-4); // To remove document Name extension
var savedState = app.activeDocument.activeHistoryState; // Save active history
var filePath = activeDocument.path.fullName;
//app.activeDocument.saveAs(new File(activeDocument.path.fullName+'/'+n+'gif'), new GIFSaveOptions(), true, Extension.NONE);
//app.activeDocument.mergeVisibleLayers();
app.activeDocument.selection.selectAll();
app.activeDocument.selection.copy(true);
app.activeDocument.activeHistoryState = savedState;
var h = app.activeDocument.height.value+200, w = app.activeDocument.width.value+200;
var hr = 0;
if (File(filePath+'/'+n+'_Frames.pdf').exists)
{
app.open(File(activeDocument.path.fullName+'/'+n+'_Frames.pdf'));
hr = h + app.activeDocument.height.value;
app.activeDocument.resizeCanvas(w, hr, AnchorPosition.TOPLEFT);
}
else
{
var ndoc = app.documents.add(w, h, 72, n+'_Frames.pdf', NewDocumentMode.RGB); // Create file
hr = h;
}
// Create file
//app.activeDocument.artLayers.add();
app.activeDocument.paste();
//alert(w+'/'+wid+'/'+ h);
var k = app.activeDocument.activeLayer.bounds;
alert(k);
app.activeDocument.activeLayer.translate(w*.01, k[1]);
var myLayers;
myLayers = activeDocument.artLayers.add(); // Cate New Empty Layer
myLayers.kind = LayerKind.TEXT; // Covert the layer into text layer
myLayers.textItem.contents = "Frame "+d; // Layer content
myLayers.textItem.position = Array(w*.5-50,hr+h*.5-80);
//myLayers.textItem.position = Array(app.activeDocument.width*.5-48, app.activeDocument.height-50);
myLayers.textItem.size = 30;
//app.activeDocument.activeLayer.translate(k[0], app.activeDocument.height-50);
if(d==fno)
{
app.activeDocument.resizeCanvas(w, hr+200, AnchorPosition.TOPLEFT);
//app.activeDocument.mergeVisibleLayers();
app.activeDocument.saveAs(new File(filePath+'/'+app.activeDocument.name), PDFSaveOptions); // Save pdf
//app.activeDocument.close(SaveOptions.DONOTSAVECHANGES); // Close pdf
}
else
{
app.activeDocument.saveAs(new File(filePath+'/'+app.activeDocument.name), PDFSaveOptions); // Save pdf
}

app.open(File(filePath+'/'+n+'.psd'));
}

Likes

translate

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
Adobe Community Professional ,
Mar 22, 2021 Mar 22, 2021

Copy link to clipboard

Copied

What is the exact issue?

 

I think that it would be best if you started a new topic and then remove the code and link to the new topic.

Likes

translate

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
Explorer ,
Mar 22, 2021 Mar 22, 2021

Copy link to clipboard

Copied

I am not able to delete the comment. I am posting this as a new topic with the issue and sample.

Likes

translate

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
Adobe Community Professional ,
Mar 23, 2021 Mar 23, 2021

Copy link to clipboard

Copied

Likes

translate

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