Skip to main content
Simonetos The Greek
Inspiring
July 2, 2019
Answered

Script which will find specific layers and apply Bevel and Emboss.

  • July 2, 2019
  • 1 reply
  • 1437 views

Hello everybody, I am new to this forum and Photoshop scripting too!!!

I need help to make a script (and study it of course) which will search in every group with specific name, then find every layer with specific name or layerkind, then count (length) those layers and then apply desired Bevel and Emboss style. Can anybody help me on this? Thank you for your time!!!

This topic has been closed for replies.
Correct answer Chuck Uebele

You will need a recursive layer to go through all the layers in groups. So this script will do that. You will need to change the name of the layer that you're looking for in line 11. You will also have to change the kind of layer that you want, also in line 11. If you don't want both conditions to meet: layer name and kind, then change the '||' to '&&'. You will also need to go in and change the parameters of the bevel function to what you want. You can record that code with scriptListener.

#target Photoshop

var doc = activeDocument;

var curLayer

goThroughLayers (doc);

function goThroughLayers(parentLayer){

    for(var i=0;i<parentLayer.layers.length;i++){

        curLayer = parentLayer.layers;

        doc.activeLayer = curLayer;

        if(curLayer.typename =='LayerSet'){goThroughLayers (curLayer)}

        else{

            if(curLayer.name=='my layer name'|| curLayer.kind = LayerKind.NORMAL){

                bevel ();

                }//end if

            }//end else

        }//end loop

    }//end function

function bevel(){

    var idsetd = charIDToTypeID( "setd" );

        var desc4 = new ActionDescriptor();

        var idnull = charIDToTypeID( "null" );

            var ref1 = new ActionReference();

            var idPrpr = charIDToTypeID( "Prpr" );

            var idLefx = charIDToTypeID( "Lefx" );

            ref1.putProperty( idPrpr, idLefx );

            var idLyr = charIDToTypeID( "Lyr " );

            var idOrdn = charIDToTypeID( "Ordn" );

            var idTrgt = charIDToTypeID( "Trgt" );

            ref1.putEnumerated( idLyr, idOrdn, idTrgt );

        desc4.putReference( idnull, ref1 );

        var idT = charIDToTypeID( "T   " );

            var desc5 = new ActionDescriptor();

            var idScl = charIDToTypeID( "Scl " );

            var idPrc = charIDToTypeID( "#Prc" );

            desc5.putUnitDouble( idScl, idPrc, 100.000000 );

            var idebbl = charIDToTypeID( "ebbl" );

                var desc6 = new ActionDescriptor();

                var idenab = charIDToTypeID( "enab" );

                desc6.putBoolean( idenab, true );

                var idpresent = stringIDToTypeID( "present" );

                desc6.putBoolean( idpresent, true );

                var idshowInDialog = stringIDToTypeID( "showInDialog" );

                desc6.putBoolean( idshowInDialog, true );

                var idhglM = charIDToTypeID( "hglM" );

                var idBlnM = charIDToTypeID( "BlnM" );

                var idScrn = charIDToTypeID( "Scrn" );

                desc6.putEnumerated( idhglM, idBlnM, idScrn );

                var idhglC = charIDToTypeID( "hglC" );

                    var desc7 = new ActionDescriptor();

                    var idRd = charIDToTypeID( "Rd  " );

                    desc7.putDouble( idRd, 255.000000 );

                    var idGrn = charIDToTypeID( "Grn " );

                    desc7.putDouble( idGrn, 255.000000 );

                    var idBl = charIDToTypeID( "Bl  " );

                    desc7.putDouble( idBl, 255.000000 );

                var idRGBC = charIDToTypeID( "RGBC" );

                desc6.putObject( idhglC, idRGBC, desc7 );

                var idhglO = charIDToTypeID( "hglO" );

                var idPrc = charIDToTypeID( "#Prc" );

                desc6.putUnitDouble( idhglO, idPrc, 50.000000 );

                var idsdwM = charIDToTypeID( "sdwM" );

                var idBlnM = charIDToTypeID( "BlnM" );

                var idMltp = charIDToTypeID( "Mltp" );

                desc6.putEnumerated( idsdwM, idBlnM, idMltp );

                var idsdwC = charIDToTypeID( "sdwC" );

                    var desc8 = new ActionDescriptor();

                    var idRd = charIDToTypeID( "Rd  " );

                    desc8.putDouble( idRd, 0.000000 );

                    var idGrn = charIDToTypeID( "Grn " );

                    desc8.putDouble( idGrn, 0.000000 );

                    var idBl = charIDToTypeID( "Bl  " );

                    desc8.putDouble( idBl, 0.000000 );

                var idRGBC = charIDToTypeID( "RGBC" );

                desc6.putObject( idsdwC, idRGBC, desc8 );

                var idsdwO = charIDToTypeID( "sdwO" );

                var idPrc = charIDToTypeID( "#Prc" );

                desc6.putUnitDouble( idsdwO, idPrc, 50.000000 );

                var idbvlT = charIDToTypeID( "bvlT" );

                var idbvlT = charIDToTypeID( "bvlT" );

                var idSfBL = charIDToTypeID( "SfBL" );

                desc6.putEnumerated( idbvlT, idbvlT, idSfBL );

                var idbvlS = charIDToTypeID( "bvlS" );

                var idBESl = charIDToTypeID( "BESl" );

                var idInrB = charIDToTypeID( "InrB" );

                desc6.putEnumerated( idbvlS, idBESl, idInrB );

                var iduglg = charIDToTypeID( "uglg" );

                desc6.putBoolean( iduglg, true );

                var idlagl = charIDToTypeID( "lagl" );

                var idAng = charIDToTypeID( "#Ang" );

                desc6.putUnitDouble( idlagl, idAng, 90.000000 );

                var idLald = charIDToTypeID( "Lald" );

                var idAng = charIDToTypeID( "#Ang" );

                desc6.putUnitDouble( idLald, idAng, 30.000000 );

                var idsrgR = charIDToTypeID( "srgR" );

                var idPrc = charIDToTypeID( "#Prc" );

                desc6.putUnitDouble( idsrgR, idPrc, 100.000000 );

                var idblur = charIDToTypeID( "blur" );

                var idPxl = charIDToTypeID( "#Pxl" );

                desc6.putUnitDouble( idblur, idPxl, 2.000000 );

                var idbvlD = charIDToTypeID( "bvlD" );

                var idBESs = charIDToTypeID( "BESs" );

                var idIn = charIDToTypeID( "In  " );

                desc6.putEnumerated( idbvlD, idBESs, idIn );

                var idTrnS = charIDToTypeID( "TrnS" );

                    var desc9 = new ActionDescriptor();

                    var idNm = charIDToTypeID( "Nm  " );

                    desc9.putString( idNm, """Linear""" );

                var idShpC = charIDToTypeID( "ShpC" );

                desc6.putObject( idTrnS, idShpC, desc9 );

                var idantialiasGloss = stringIDToTypeID( "antialiasGloss" );

                desc6.putBoolean( idantialiasGloss, false );

                var idSftn = charIDToTypeID( "Sftn" );

                var idPxl = charIDToTypeID( "#Pxl" );

                desc6.putUnitDouble( idSftn, idPxl, 0.000000 );

                var iduseShape = stringIDToTypeID( "useShape" );

                desc6.putBoolean( iduseShape, false );

                var iduseTexture = stringIDToTypeID( "useTexture" );

                desc6.putBoolean( iduseTexture, false );

            var idebbl = charIDToTypeID( "ebbl" );

            desc5.putObject( idebbl, idebbl, desc6 );

        var idLefx = charIDToTypeID( "Lefx" );

        desc4.putObject( idT, idLefx, desc5 );

    executeAction( idsetd, desc4, DialogModes.NO );

    }

Edit: corrected script to include fix for active layer on line 9.

1 reply

Chuck Uebele
Community Expert
Chuck UebeleCommunity ExpertCorrect answer
Community Expert
July 2, 2019

You will need a recursive layer to go through all the layers in groups. So this script will do that. You will need to change the name of the layer that you're looking for in line 11. You will also have to change the kind of layer that you want, also in line 11. If you don't want both conditions to meet: layer name and kind, then change the '||' to '&&'. You will also need to go in and change the parameters of the bevel function to what you want. You can record that code with scriptListener.

#target Photoshop

var doc = activeDocument;

var curLayer

goThroughLayers (doc);

function goThroughLayers(parentLayer){

    for(var i=0;i<parentLayer.layers.length;i++){

        curLayer = parentLayer.layers;

        doc.activeLayer = curLayer;

        if(curLayer.typename =='LayerSet'){goThroughLayers (curLayer)}

        else{

            if(curLayer.name=='my layer name'|| curLayer.kind = LayerKind.NORMAL){

                bevel ();

                }//end if

            }//end else

        }//end loop

    }//end function

function bevel(){

    var idsetd = charIDToTypeID( "setd" );

        var desc4 = new ActionDescriptor();

        var idnull = charIDToTypeID( "null" );

            var ref1 = new ActionReference();

            var idPrpr = charIDToTypeID( "Prpr" );

            var idLefx = charIDToTypeID( "Lefx" );

            ref1.putProperty( idPrpr, idLefx );

            var idLyr = charIDToTypeID( "Lyr " );

            var idOrdn = charIDToTypeID( "Ordn" );

            var idTrgt = charIDToTypeID( "Trgt" );

            ref1.putEnumerated( idLyr, idOrdn, idTrgt );

        desc4.putReference( idnull, ref1 );

        var idT = charIDToTypeID( "T   " );

            var desc5 = new ActionDescriptor();

            var idScl = charIDToTypeID( "Scl " );

            var idPrc = charIDToTypeID( "#Prc" );

            desc5.putUnitDouble( idScl, idPrc, 100.000000 );

            var idebbl = charIDToTypeID( "ebbl" );

                var desc6 = new ActionDescriptor();

                var idenab = charIDToTypeID( "enab" );

                desc6.putBoolean( idenab, true );

                var idpresent = stringIDToTypeID( "present" );

                desc6.putBoolean( idpresent, true );

                var idshowInDialog = stringIDToTypeID( "showInDialog" );

                desc6.putBoolean( idshowInDialog, true );

                var idhglM = charIDToTypeID( "hglM" );

                var idBlnM = charIDToTypeID( "BlnM" );

                var idScrn = charIDToTypeID( "Scrn" );

                desc6.putEnumerated( idhglM, idBlnM, idScrn );

                var idhglC = charIDToTypeID( "hglC" );

                    var desc7 = new ActionDescriptor();

                    var idRd = charIDToTypeID( "Rd  " );

                    desc7.putDouble( idRd, 255.000000 );

                    var idGrn = charIDToTypeID( "Grn " );

                    desc7.putDouble( idGrn, 255.000000 );

                    var idBl = charIDToTypeID( "Bl  " );

                    desc7.putDouble( idBl, 255.000000 );

                var idRGBC = charIDToTypeID( "RGBC" );

                desc6.putObject( idhglC, idRGBC, desc7 );

                var idhglO = charIDToTypeID( "hglO" );

                var idPrc = charIDToTypeID( "#Prc" );

                desc6.putUnitDouble( idhglO, idPrc, 50.000000 );

                var idsdwM = charIDToTypeID( "sdwM" );

                var idBlnM = charIDToTypeID( "BlnM" );

                var idMltp = charIDToTypeID( "Mltp" );

                desc6.putEnumerated( idsdwM, idBlnM, idMltp );

                var idsdwC = charIDToTypeID( "sdwC" );

                    var desc8 = new ActionDescriptor();

                    var idRd = charIDToTypeID( "Rd  " );

                    desc8.putDouble( idRd, 0.000000 );

                    var idGrn = charIDToTypeID( "Grn " );

                    desc8.putDouble( idGrn, 0.000000 );

                    var idBl = charIDToTypeID( "Bl  " );

                    desc8.putDouble( idBl, 0.000000 );

                var idRGBC = charIDToTypeID( "RGBC" );

                desc6.putObject( idsdwC, idRGBC, desc8 );

                var idsdwO = charIDToTypeID( "sdwO" );

                var idPrc = charIDToTypeID( "#Prc" );

                desc6.putUnitDouble( idsdwO, idPrc, 50.000000 );

                var idbvlT = charIDToTypeID( "bvlT" );

                var idbvlT = charIDToTypeID( "bvlT" );

                var idSfBL = charIDToTypeID( "SfBL" );

                desc6.putEnumerated( idbvlT, idbvlT, idSfBL );

                var idbvlS = charIDToTypeID( "bvlS" );

                var idBESl = charIDToTypeID( "BESl" );

                var idInrB = charIDToTypeID( "InrB" );

                desc6.putEnumerated( idbvlS, idBESl, idInrB );

                var iduglg = charIDToTypeID( "uglg" );

                desc6.putBoolean( iduglg, true );

                var idlagl = charIDToTypeID( "lagl" );

                var idAng = charIDToTypeID( "#Ang" );

                desc6.putUnitDouble( idlagl, idAng, 90.000000 );

                var idLald = charIDToTypeID( "Lald" );

                var idAng = charIDToTypeID( "#Ang" );

                desc6.putUnitDouble( idLald, idAng, 30.000000 );

                var idsrgR = charIDToTypeID( "srgR" );

                var idPrc = charIDToTypeID( "#Prc" );

                desc6.putUnitDouble( idsrgR, idPrc, 100.000000 );

                var idblur = charIDToTypeID( "blur" );

                var idPxl = charIDToTypeID( "#Pxl" );

                desc6.putUnitDouble( idblur, idPxl, 2.000000 );

                var idbvlD = charIDToTypeID( "bvlD" );

                var idBESs = charIDToTypeID( "BESs" );

                var idIn = charIDToTypeID( "In  " );

                desc6.putEnumerated( idbvlD, idBESs, idIn );

                var idTrnS = charIDToTypeID( "TrnS" );

                    var desc9 = new ActionDescriptor();

                    var idNm = charIDToTypeID( "Nm  " );

                    desc9.putString( idNm, """Linear""" );

                var idShpC = charIDToTypeID( "ShpC" );

                desc6.putObject( idTrnS, idShpC, desc9 );

                var idantialiasGloss = stringIDToTypeID( "antialiasGloss" );

                desc6.putBoolean( idantialiasGloss, false );

                var idSftn = charIDToTypeID( "Sftn" );

                var idPxl = charIDToTypeID( "#Pxl" );

                desc6.putUnitDouble( idSftn, idPxl, 0.000000 );

                var iduseShape = stringIDToTypeID( "useShape" );

                desc6.putBoolean( iduseShape, false );

                var iduseTexture = stringIDToTypeID( "useTexture" );

                desc6.putBoolean( iduseTexture, false );

            var idebbl = charIDToTypeID( "ebbl" );

            desc5.putObject( idebbl, idebbl, desc6 );

        var idLefx = charIDToTypeID( "Lefx" );

        desc4.putObject( idT, idLefx, desc5 );

    executeAction( idsetd, desc4, DialogModes.NO );

    }

Edit: corrected script to include fix for active layer on line 9.

Simonetos The Greek
Inspiring
July 3, 2019

First of all, thank you for your fast and detailed answer!!!

I tested your script and I have some issues...

1st try: I open a document with six rectangle shapes. Three of them by the name Rectangle 1 and the rest by the name Rectangle 2. Into your script, as you said, in line 11, I do this if(curLayer.name=='Rectangle 1' && curLayer.kind = LayerKind.NORMAL){ and run the script. I get this message, "You can only change the layer's kind to text or normal."!!! Why? And how can I set it to shape layer for example? Or normal is for all layers?

2nd try:
I remove the second part && curLayer.kind = LayerKind.NORMAL){ from if condition and I leave it like this if(curLayer.name=='Rectangle 1'{ .I run the script and It adds Bevel and Emboss only in selected layer!!! Even if it's name is Rectangle 2!!! Like script "works" only with selected layer.

3rd try: I deselect all layers, with if condition like this if(curLayer.name=='Rectangle 1'){ I run the script and I get this error message, "General Photoshop error occurred. This function may not be available in this version of Photoshop."!!!

/// EDIT ///

I found a syntax error in if condition, it was just a double "=" at && curLayer.kind = LayerKind.NORMAL){. But still have issues.

For example, if I do this if(curLayer.name=='Rectangle 1' && curLayer.kind == LayerKind.NORMAL){ so to select all shape layers by the name Rectangle 1 and apply Bevel and Emboss, nothing happens!!!

I removed the layerkind part from if condition, I changed executeAction( idsetd, desc4, DialogModes.NO); to executeAction( idsetd, desc4, DialogModes.ALL ); so to get dialogs and see what's happening and I realized that it just apply Bevel and Emboss, only at first shape layer by the name Rectangle 1 and doing it three times!!! Three times in the same layer and not one time in each of three layers.

Also, now, with DialogModes.ALL, if I deselect all layers and run the script, I get this message, "The command Set is not currently available".

So...

1. We need a way to find all these layers by specific name and do our stuff, even if none is selected.
2. Also I would like to have the ability to find these layers with specific name but if they exist in specific group.
3. Maybe LayerKind is not what I need so to find if a layer is shape, text, pixel etc?

Thank you for your time!!!

Chuck Uebele
Community Expert
Community Expert
July 3, 2019

Yes, it's best just to use the name of the layer, unless you have multiple layers with the same name and that are different types. The reason it didn't work with the layer kind is that shape layers aren't normal layers, they're either SOLIDFILL OR GRADIENTFILL. For the other issue, there needs to be this line inserted after line 8:

doc.activeLayer = curLay;