2 Replies Latest reply on Jan 23, 2016 8:48 PM by ojodegato

    Toggle mask single click

    ojodegato Level 1

      I am familiar with the OSX keyboard shortcut click + option to show/hide a layer mask.

      Trying to script this mask toggle behavior. I found a forum script which toggles a layer mask in three clicks.

       

      1-Click-1 (Selects layer mask)

      Screen Shot 2016-01-23 at 10.37.22 AM.png

      2-Click 2 (Shows mask)

      Screen Shot 2016-01-23 at 10.37.40 AM.png

      3-Click 3 (Hides  mask and makes layer icon active)

      Screen Shot 2016-01-23 at 10.38.01 AM.png

       

      Now for the tricky part. Can a script toggle a layer mask without making the layer icon active after the toggle effect?

      In other words the script selects the layer mask toggles the mask and keeps the mask icon selected.

       

       

      Link to forum script: Re: Selection toggle

        • 1. Re: Toggle mask single click
          cbuliarca Level 3

          You want something like this?

          function isChannelMaskSelected(){

              var ret = false;

              if(hasMask() == true){

                  if(getChannelMaskSel() == true)

                  {

                      ret = true;

                  }

              else{ret = false}

              }

              return ret;

          };

          function hasMask(){

                  var hasMask = false;

           

           

                  var ref = new ActionReference();

                  // var keyHasMask = app.stringIDToTypeID( 'hasUserMask' );

                  var keyHasMask = app.charIDToTypeID( 'UsrM' );

                  ref.putEnumerated( app.charIDToTypeID( 'Lyr ' ), app.charIDToTypeID( 'Ordn' ), app.charIDToTypeID( 'Trgt' ) );

                  var desc = executeActionGet( ref );

                  if ( desc.hasKey( keyHasMask ) ) {

                      hasMask = true;

                  }

              return hasMask;

          }

          function selectComponentChannel() {

              try{

                  var map = {}

                  map[DocumentMode.GRAYSCALE] = charIDToTypeID('Blck');

                  map[DocumentMode.RGB] = charIDToTypeID('RGB ');

                  map[DocumentMode.CMYK] = charIDToTypeID('CMYK');

                  map[DocumentMode.LAB] = charIDToTypeID('Lab ');

                  var desc = new ActionDescriptor();

                      var ref = new ActionReference();

                      ref.putEnumerated( charIDToTypeID('Chnl'), charIDToTypeID('Chnl'), map[app.activeDocument.mode] );

                  desc.putReference( charIDToTypeID('null'), ref );

                  executeAction( charIDToTypeID('slct'), desc, DialogModes.NO );

              }catch(e){}

          };

          function selectChannelMask(visible){

            try {

              var desc = new ActionDescriptor();

              var ref = new ActionReference();

              ref.putEnumerated( charIDToTypeID( "Chnl" ), charIDToTypeID( "Chnl" ), charIDToTypeID( "Msk " ) );

              desc.putReference( charIDToTypeID( "null" ), ref );

              desc.putBoolean( charIDToTypeID( "MkVs" ), visible );

              executeAction( charIDToTypeID( "slct" ), desc, DialogModes.NO );

              return true;

            } catch (e) { return false;}

          };

          function getChannelMaskVis(){

              var isVisible = false;

           

           

                  var ref = new ActionReference();

                  var keyVisChannels = app.stringIDToTypeID( 'visibleChannels' );

                  ref.putEnumerated( app.charIDToTypeID( 'Lyr ' ), app.charIDToTypeID( 'Ordn' ), app.charIDToTypeID( 'Trgt' ) );

                  var desc = executeActionGet( ref );

                  if ( desc.hasKey( keyVisChannels ) ) {

                      list = desc.getList(keyVisChannels);

                      // alert("kas key");

                      // alert(desc.getType(keyVisChannels));

                      // alert(list.count);

                      if(list.count > 1)

                      {

                          isVisible = false;

                      }

                      else{isVisible = true}

                  }

              return isVisible;

          }

          function getChannelMaskSel(){

              var ref = new ActionReference();

              ref.putProperty( charIDToTypeID( 'Prpr' ), stringIDToTypeID('numberOfChannels') );

              ref.putEnumerated( charIDToTypeID( "Dcmn" ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );

              var desc = executeActionGet(ref);

              var numberOfChannels = desc.getInteger(stringIDToTypeID('numberOfChannels'));

              var ref = new ActionReference();

              ref.putProperty( charIDToTypeID( 'Prpr' ), stringIDToTypeID( 'targetChannels' ) );

              ref.putEnumerated( app.charIDToTypeID( 'Lyr ' ), app.charIDToTypeID( 'Ordn' ), app.charIDToTypeID( 'Trgt' ) );

              var desc = executeActionGet( ref );

              var list = desc.getList( stringIDToTypeID( 'targetChannels' ) );

              var firstIndex = list.getReference(0).getIndex();

              return list.count == 1 && firstIndex == numberOfChannels+1;

          }

          function toggleChannelMask0(){

              var maskSelected = isChannelMaskSelected();

              if(maskSelected == true){

                  if(getChannelMaskVis() == true)

                  {

                      selectComponentChannel();

                  }else{selectChannelMask(true)};

              }else{

                    selectChannelMask(false);

              }

          };

           

           

          function toggleChannelMask(){

              var maskSelected = isChannelMaskSelected();

              if(maskSelected == true){

                  if(getChannelMaskVis() == true)

                  {

                      selectChannelMask(false);

                  }else{selectChannelMask(true)};

              }else{

                    selectChannelMask(false);

              }

          };

          toggleChannelMask();

          • 2. Re: Toggle mask single click
            ojodegato Level 1

            Right on! Many thanks!