Skip to main content
rcraighead
Legend
October 12, 2018
Answered

How to Toggle "Smart Filters" on/off?

  • October 12, 2018
  • 2 replies
  • 1965 views

I want to toggle smart filters on/off for a named layer. I used "ScriptListener" to create this code, but I'd rather have it toggle visibility. Can someone show me how to accomplish that in Javascript? Thank you!

// Hide Smart Filter Effects (Layer 1)

var idHd = charIDToTypeID( "Hd  " );

    var desc3 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref1 = new ActionReference();

        var idfilterFX = stringIDToTypeID( "filterFX" );

        ref1.putClass( idfilterFX );

        var idLyr = charIDToTypeID( "Lyr " );

        ref1.putName( idLyr, "Layer 1" );

    desc3.putReference( idnull, ref1 );

executeAction( idHd, desc3, DialogModes.NO );

// Show Smart Filter Effects (Layer 1)

var idShw = charIDToTypeID( "Shw " );

    var desc6 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref2 = new ActionReference();

        var idfilterFX = stringIDToTypeID( "filterFX" );

        ref2.putClass( idfilterFX );

        var idLyr = charIDToTypeID( "Lyr " );

        var idOrdn = charIDToTypeID( "Ordn" );

        var idTrgt = charIDToTypeID( "Trgt" );

        ref2.putEnumerated( idLyr, idOrdn, idTrgt );

    desc6.putReference( idnull, ref2 );

executeAction( idShw, desc6, DialogModes.NO );

This topic has been closed for replies.
Correct answer r-bin

CC2018

var r = new ActionReference();   

r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("smartObjectMore"));

r.putEnumerated(stringIDToTypeID("layer"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));

var vis = executeActionGet(r).getObjectValue(stringIDToTypeID("smartObjectMore")).getObjectValue(stringIDToTypeID("filterFX")).getBoolean(stringIDToTypeID("enabled"));

var d = new ActionDescriptor();

var r = new ActionReference();

r.putClass(stringIDToTypeID("filterFX"));

r.putEnumerated(stringIDToTypeID("layer"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));

d.putReference(stringIDToTypeID("null"), r);

executeAction(vis?stringIDToTypeID("hide"):stringIDToTypeID("show"), d, DialogModes.NO);

2 replies

r-binCorrect answer
Legend
October 12, 2018

CC2018

var r = new ActionReference();   

r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("smartObjectMore"));

r.putEnumerated(stringIDToTypeID("layer"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));

var vis = executeActionGet(r).getObjectValue(stringIDToTypeID("smartObjectMore")).getObjectValue(stringIDToTypeID("filterFX")).getBoolean(stringIDToTypeID("enabled"));

var d = new ActionDescriptor();

var r = new ActionReference();

r.putClass(stringIDToTypeID("filterFX"));

r.putEnumerated(stringIDToTypeID("layer"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));

d.putReference(stringIDToTypeID("null"), r);

executeAction(vis?stringIDToTypeID("hide"):stringIDToTypeID("show"), d, DialogModes.NO);

JJMack
Community Expert
Community Expert
October 13, 2018

Thank you I just knew that you would come through for to OP.  I would still wrap your code in a try catch. Maybe even add a test for PS version 19 or newer  as it does not work in CC 2014  my goto Photoshop version back up I added that also.

if (app.activeDocument.activeLayer.kind==LayerKind.SMARTOBJECT) {

   try {

      if (CheckVersion(19)) toggleFiltersVis();

      else alert("This Script requires Photoshop CC 2018 or newer");

   }

   //catch(e) { alert(e + ': on line ' + e.line, 'Script Error', true); }

   catch(e) { }

}

//else alert("Active Layer is not a smart object layer") 

// =======================================================

function toggleFiltersVis() { 

   var r = new ActionReference();       

   r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("smartObjectMore"));   

   r.putEnumerated(stringIDToTypeID("layer"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));   

   var vis = executeActionGet(r).getObjectValue(stringIDToTypeID("smartObjectMore")).getObjectValue(stringIDToTypeID("filterFX")).getBoolean(stringIDToTypeID("enabled"));   

   

   var d = new ActionDescriptor();   

   var r = new ActionReference();   

   r.putClass(stringIDToTypeID("filterFX"));   

   r.putEnumerated(stringIDToTypeID("layer"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));   

   d.putReference(stringIDToTypeID("null"), r);   

   executeAction(vis?stringIDToTypeID("hide"):stringIDToTypeID("show"), d, DialogModes.NO);  

}  

function CheckVersion(PSV) {

   var numberArray = version.split(".");

   if ( numberArray[0] < PSV ) { return false; }

   else return true;

}

JJMack
JJMack
Community Expert
Community Expert
October 12, 2018

To toggle all the smart fitters on a smart object layer you would need to get the current state of the visibility icon by the layers  filter mask then change that state. The Scriptlistener only seems to record executeAction  steps like you show.  To show and hide.  You  would need to get to do some executeActionGet(ref) for the layer or perhaps the active to get the current state and the use the correct setting "Shw " or "Hd  " in the executeAction( ?) step.  The code you show one step Hide seems to idenyify the layer name the Show step must be for the current actuve layer I see noe layer name.

I do not knonw how to write the executeActionGet(ref) part I always search for code where other have coded that part and steal(reuse) their code.

greek for me

JJMack
rcraighead
Legend
October 12, 2018

Thanks JJMack

I was hoping some variation of this script would work. It toggles a layer on or off.

var Doc = app.activeDocument

Doc.layers.getByName('Threshold 1').visible ^= 1;

Geppetto Luis
Legend
October 12, 2018

The Kukurykus script works for me

// script

function layers(v) { 

     function sTT(v) {return stringIDToTypeID(v)} 

 

     (ref = new ActionReference()).putEnumerated(sTT('document'), sTT('ordinal'), 

     sTT('targetEnum')); len = executeActionGet(ref).getInteger(sTT('numberOfLayers')) 

 

    for(i = 1; i <= len;) {  /// POTRESTI METTERE 0 MA NON NE SONO SICURO

          (ref = new ActionReference()).putIndex(sTT('layer'), i++) 

           if ((dsc = executeActionGet(ref)).getString(sTT('name')) == v) { 

               (lst = new ActionList()).putReference(ref), dsc.putList(sTT('null'), lst) 

               vis = sTT(dsc.getBoolean(sTT('visible')) ? 'hide' : 'show') 

               executeAction(vis, dsc, DialogModes.NO) 

          } 

     } 

 

layers('Threshold 1')