Fernando Mendi
Fernando Mendi

Reputation: 15

Add Layer Effects using Photoshop scripts

I've been doing some research on how to do this but I haven't managed to find anything on this topic. My problem is that I want to add a color overlay to a layer given a color but I haven't managed to do this. I know there is a way of doing it using ActionDescriptors but I don't know how. I have also found that you can add a style to a layer, however, I don't want to use a preset style. So maybe that would be another way of doing it, create a custom layer style and then add that one. Another way which I thought of was to simply fill the selection, but for that I would have to get a selection accurate to my layer (not just a square but the actual shape of the layer) Also, I'm trying to write it in javascript.

Thanks in advance, any help would be appreciated!

Upvotes: 0

Views: 1136

Answers (1)

Sergey Kritskiy
Sergey Kritskiy

Reputation: 2269

You can get Action Manager code from ScriptingListener plugin.

Here's an example of code generated for Add Stroke:

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 idFrFX = charIDToTypeID( "FrFX" );
            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 idStyl = charIDToTypeID( "Styl" );
            var idFStl = charIDToTypeID( "FStl" );
            var idOutF = charIDToTypeID( "OutF" );
            desc6.putEnumerated( idStyl, idFStl, idOutF );
            var idPntT = charIDToTypeID( "PntT" );
            var idFrFl = charIDToTypeID( "FrFl" );
            var idSClr = charIDToTypeID( "SClr" );
            desc6.putEnumerated( idPntT, idFrFl, idSClr );
            var idMd = charIDToTypeID( "Md  " );
            var idBlnM = charIDToTypeID( "BlnM" );
            var idNrml = charIDToTypeID( "Nrml" );
            desc6.putEnumerated( idMd, idBlnM, idNrml );
            var idOpct = charIDToTypeID( "Opct" );
            var idPrc = charIDToTypeID( "#Prc" );
            desc6.putUnitDouble( idOpct, idPrc, 100.000000 );
            var idSz = charIDToTypeID( "Sz  " );
            var idPxl = charIDToTypeID( "#Pxl" );
            desc6.putUnitDouble( idSz, idPxl, 18.000000 );
            var idClr = charIDToTypeID( "Clr " );
                var desc7 = new ActionDescriptor();
                var idRd = charIDToTypeID( "Rd  " );
                desc7.putDouble( idRd, 255.000000 );
                var idGrn = charIDToTypeID( "Grn " );
                desc7.putDouble( idGrn, 0.000000 );
                var idBl = charIDToTypeID( "Bl  " );
                desc7.putDouble( idBl, 0.000000 );
            var idRGBC = charIDToTypeID( "RGBC" );
            desc6.putObject( idClr, idRGBC, desc7 );
            var idoverprint = stringIDToTypeID( "overprint" );
            desc6.putBoolean( idoverprint, false );
        var idFrFX = charIDToTypeID( "FrFX" );
        desc5.putObject( idFrFX, idFrFX, desc6 );
    var idLefx = charIDToTypeID( "Lefx" );
    desc4.putObject( idT, idLefx, desc5 );
executeAction( idsetd, desc4, DialogModes.NO );

It's quite difficult to read... xbytor's xtools include a SLCFix.jsx script that can make it a bit cleaner:

function ftn4() {
  function cTID(s) { return app.charIDToTypeID(s); };
  function sTID(s) { return app.stringIDToTypeID(s); };

    var desc4 = new ActionDescriptor();
        var ref1 = new ActionReference();
        ref1.putProperty( cTID('Prpr'), cTID('Lefx') );
        ref1.putEnumerated( cTID('Lyr '), cTID('Ordn'), cTID('Trgt') );
    desc4.putReference( cTID('null'), ref1 );
        var desc5 = new ActionDescriptor();
        desc5.putUnitDouble( cTID('Scl '), cTID('#Prc'), 100.000000 );
            var desc6 = new ActionDescriptor();
            desc6.putBoolean( cTID('enab'), true );
            desc6.putBoolean( sTID('present'), true );
            desc6.putBoolean( sTID('showInDialog'), true );
            desc6.putEnumerated( cTID('Styl'), cTID('FStl'), cTID('OutF') );
            desc6.putEnumerated( cTID('PntT'), cTID('FrFl'), cTID('SClr') );
            desc6.putEnumerated( cTID('Md  '), cTID('BlnM'), cTID('Nrml') );
            desc6.putUnitDouble( cTID('Opct'), cTID('#Prc'), 100.000000 );
            desc6.putUnitDouble( cTID('Sz  '), cTID('#Pxl'), 18.000000 );
                var desc7 = new ActionDescriptor();
                desc7.putDouble( cTID('Rd  '), 255.000000 );
                desc7.putDouble( cTID('Grn '), 0.000000 );
                desc7.putDouble( cTID('Bl  '), 0.000000 );
            desc6.putObject( cTID('Clr '), cTID('RGBC'), desc7 );
            desc6.putBoolean( sTID('overprint'), false );
        desc5.putObject( cTID('FrFX'), cTID('FrFX'), desc6 );
    desc4.putObject( cTID('T   '), cTID('Lefx'), desc5 );
    executeAction( cTID('setd'), desc4, DialogModes.NO );
};

what is left to do is to undertsand what values go where and add arguments:

addStroke({
  size: 5,
  opacity: 50,
  color: [255,0,128]
})

function addStroke(data) {
  function cTID(s) { return app.charIDToTypeID(s); };
  function sTID(s) { return app.stringIDToTypeID(s); };

  if (data == undefined) data = {};
  if (data.opacity == undefined) data.opacity = 100;
  if (data.size == undefined) data.size = 10;
  if (data.color == undefined) data.color = [0,0,0];


    var desc4 = new ActionDescriptor();
        var ref1 = new ActionReference();
        ref1.putProperty( cTID('Prpr'), cTID('Lefx') );
        ref1.putEnumerated( cTID('Lyr '), cTID('Ordn'), cTID('Trgt') );
    desc4.putReference( cTID('null'), ref1 );
        var desc5 = new ActionDescriptor();
        desc5.putUnitDouble( cTID('Scl '), cTID('#Prc'), 100);
            var desc6 = new ActionDescriptor();
            desc6.putBoolean( cTID('enab'), true );
            desc6.putBoolean( sTID('present'), true );
            desc6.putBoolean( sTID('showInDialog'), true );
            desc6.putEnumerated( cTID('Styl'), cTID('FStl'), cTID('OutF') );
            desc6.putEnumerated( cTID('PntT'), cTID('FrFl'), cTID('SClr') );
            desc6.putEnumerated( cTID('Md  '), cTID('BlnM'), cTID('Nrml') );
            desc6.putUnitDouble( cTID('Opct'), cTID('#Prc'), data.opacity );
            desc6.putUnitDouble( cTID('Sz  '), cTID('#Pxl'), data.size );
                var desc7 = new ActionDescriptor();
                desc7.putDouble( cTID('Rd  '), data.color[0] );
                desc7.putDouble( cTID('Grn '), data.color[1] );
                desc7.putDouble( cTID('Bl  '), data.color[2] );
            desc6.putObject( cTID('Clr '), cTID('RGBC'), desc7 );
            desc6.putBoolean( sTID('overprint'), false );
        desc5.putObject( cTID('FrFX'), cTID('FrFX'), desc6 );
    desc4.putObject( cTID('T   '), cTID('Lefx'), desc5 );
    executeAction( cTID('setd'), desc4, DialogModes.NO );
};

enter image description here

Upvotes: 1

Related Questions