3 Replies Latest reply on Apr 7, 2014 8:04 AM by Chuck Uebele

    Script select parent group by layer

    smartobjecter

      I have complicated workflow in photoshop in which i often use selection of layers by ctrl+click (auto-select is unchecked and selecting layers is chosen) That works good for me. But some time i need quickly select parent group of active layer (only one level up) (it can be achived by using some keyboard shortcut)  I think the only way to do that is PS scripting.

       

      Im new to photoshop scripting but i have a lot of expirience in other languages

      As i understand its sould take just couple commands:

       

      1) select parent folder from active object (it can be any type of layer)

      2) Collapse this folder (cos it was uncollapsed before)

       

      what syntax shoud i use for this commands? i tried to use help but couldn't find even how to select parent folder of active layer..

       

      Any help?

        • 1. Re: Script select parent group by layer
          c.pfaffenbichler Level 9

          I recommend you forget about collapsing the Group.

          I think there was a thread on this over at http://ps-scripts.com/bb/ but the solutions were not »ideal« if I remember correctly.

           

          Selecting the Group should not be hard and can be done with the Photoshop DOM.

          // 2014, use it at your own risk;

          #target photoshop

          if (app.documents.length > 0) {

          var myDocument = app.activeDocument;

          var theParent = myDocument.activeLayer.parent;

          if (theParent != myDocument) {

          myDocument.activeLayer = theParent

          }

          };

          • 2. Re: Script select parent group by layer
            boo radley 2013 Level 3

            I've tried the following codes and it works for me. It selects the parent layer of the group and then collapses it:

             

            1. copy the following script and paste it into notepad. Save it as LayerSetSupport.jsx and save it in the same folder as all your other scripts:

             

             

            //START HERE------------------------------------------

            //For code readability
            function cTID(s){return charIDToTypeID(s)}
            function sTID(s){return stringIDToTypeID(s)}
            // =============================


            function openAllLayerSets( parent ){
                for(var setIndex=0;setIndex<parent.layerSets.length;setIndex++){
                    app.activeDocument.activeLayer = parent.layerSets[setIndex].layers[0];
                    openAllLayerSets( parent.layerSets[setIndex]);
                }
            };




            function closeAllLayerSets(ref) {
                      var layers = ref.layers;
                      var len = layers.length;
                      for ( var i = 0; i < len; i ++) {
                                var layer = layers[i];
                                if (layer.typename == 'LayerSet') {closeGroup(layer); var layer = layers[i]; closeAllLayerSets(layer);};
                      }
            }


            function openGroup(layerSet) {
               var m_activeLayer = activeDocument.activeLayer;

               var m_Layer_Dummy01 = layerSet.artLayers.add();
               var m_Layer_Dummy02 = layerSet.artLayers.add();
               layerSet.layers[1].name = layerSet.layers[1].name;
               m_Layer_Dummy01.remove();
               m_Layer_Dummy02.remove();

               activeDocument.activeLayer = m_activeLayer;
            }


            function closeGroup(layerSet) {
               var m_Name = layerSet.name;
               var m_Opacity = layerSet.opacity;
               var m_BlendMode = layerSet.blendMode;
               var m_LinkedLayers = layerSet.linkedLayers;

               var m_bHasMask = hasLayerMask();
               if(m_bHasMask) loadSelectionOfMask();


               if(layerSet.layers.length <= 1) {
                  addLayer();
                  var m_Tmp = activeDocument.activeLayer;
                  m_Tmp.name = "dummy - feel free to remove me";
                  activeDocument.activeLayer = layerSet;
                  ungroup();
                  addToSelection("dummy - feel free to remove me");
                  groupSelected(m_Name);

               } else {
                  activeDocument.activeLayer = layerSet;
                  ungroup();
                  groupSelected(m_Name);
               }


               var m_Closed = activeDocument.activeLayer;
               m_Closed.opacity = m_Opacity;
               m_Closed.blendMode = m_BlendMode;

               for(x in m_LinkedLayers) {
                  if(m_LinkedLayers[x].typename == "LayerSet")
                     activeDocument.activeLayer.link(m_LinkedLayers[x]);
               }

               if(m_bHasMask) maskFromSelection();

               return m_Closed;
            }


            function ungroup() {
               var m_Dsc01 = new ActionDescriptor();
               var m_Ref01 = new ActionReference();
               m_Ref01.putEnumerated( cTID( "Lyr " ), cTID( "Ordn" ), cTID( "Trgt" ) );
               m_Dsc01.putReference( cTID( "null" ), m_Ref01 );

               try {
                  executeAction( sTID( "ungroupLayersEvent" ), m_Dsc01, DialogModes.NO );
               } catch(e) {}
            }


            function addLayer() {
               var m_ActiveLayer          =    activeDocument.activeLayer;
               var m_NewLayer             =    activeDocument.artLayers.add();
               m_NewLayer.move(m_ActiveLayer, ElementPlacement.PLACEBEFORE);

               return m_NewLayer;
            }


            function hasLayerMask() {
               var m_Ref01 = new ActionReference();
               m_Ref01.putEnumerated( sTID( "layer" ), cTID( "Ordn" ), cTID( "Trgt" ));
               var m_Dsc01= executeActionGet( m_Ref01 );
               return m_Dsc01.hasKey(cTID('Usrs'));
            }


            function activateLayerMask() {
               var m_Dsc01 = new ActionDescriptor();
               var m_Ref01 = new ActionReference();
               m_Ref01.putEnumerated( cTID( "Chnl" ), cTID( "Chnl" ), cTID( "Msk " ) );
               m_Dsc01.putReference( cTID( "null" ), m_Ref01 );

               try {
                  executeAction( cTID( "slct" ), m_Dsc01, DialogModes.NO );
               } catch(e) {
                  var m_TmpAlpha = new TemporaryAlpha();

                  maskFromSelection();
                  activateLayerMask();

                  m_TmpAlpha.consume();
               }
            }


            function deleteMask(makeSelection) {
               if(makeSelection) {
                  loadSelectionOfMask();
               }

               var m_Dsc01 = new ActionDescriptor();
               var m_Ref01 = new ActionReference();
               m_Ref01.putEnumerated( cTID( "Chnl" ), cTID( "Ordn" ), cTID( "Trgt" ) );
               m_Dsc01.putReference( cTID( "null" ), m_Ref01 );

               try {
                  executeAction( cTID( "Dlt " ), m_Dsc01, DialogModes.NO );
               } catch(e) {}
            }


            function selectLayerMask() {
               var m_Dsc01 = new ActionDescriptor();
               var m_Ref01 = new ActionReference();


               m_Ref01.putEnumerated(cTID("Chnl"), cTID("Chnl"), cTID("Msk "));
               m_Dsc01.putReference(cTID("null"), m_Ref01);
               m_Dsc01.putBoolean(cTID("MkVs"), false );


               try {
                  executeAction(cTID("slct"), m_Dsc01, DialogModes.NO );
               } catch(e) {}
            }


            function loadSelectionOfMask() {
               selectLayerMask();

               var m_Dsc01 = new ActionDescriptor();
               var m_Ref01 = new ActionReference();
               m_Ref01.putProperty( cTID( "Chnl" ), cTID( "fsel" ) );
               m_Dsc01.putReference( cTID( "null" ), m_Ref01 );
               var m_Ref02 = new ActionReference();
               m_Ref02.putEnumerated( cTID( "Chnl" ), cTID( "Ordn" ), cTID( "Trgt" ) );
               m_Dsc01.putReference( cTID( "T   " ), m_Ref02 );

               try {
                  executeAction( cTID( "setd" ), m_Dsc01, DialogModes.NO );
               } catch(e) {}
            }


            function maskFromSelection() {
               if(!hasLayerMask()) {
                  var m_Dsc01 = new ActionDescriptor();
                  m_Dsc01.putClass( cTID( "Nw  " ), cTID( "Chnl" ) );
                  var m_Ref01 = new ActionReference();
                  m_Ref01.putEnumerated( cTID( "Chnl" ), cTID( "Chnl" ), cTID( "Msk " ) );
                  m_Dsc01.putReference( cTID( "At  " ), m_Ref01 );
                  m_Dsc01.putEnumerated( cTID( "Usng" ), cTID( "UsrM" ), cTID( "RvlS" ) );

                  try {
                     executeAction( cTID( "Mk  " ), m_Dsc01, DialogModes.NO );
                  } catch(e) {
                     activeDocument.selection.selectAll();
                     maskFromSelection();
                  }
               } else {
                  if(confirm("Delete existing mask?", true, "Warning")) {
                     activateLayerMask();
                     deleteMask();
                  }
               }
            }


            function groupSelected(name) {
               var m_Dsc01 = new ActionDescriptor();
               var m_Ref01 = new ActionReference();
               m_Ref01.putClass( sTID( "layerSection" ) );
               m_Dsc01.putReference(  cTID( "null" ), m_Ref01 );
               var m_Ref02 = new ActionReference();
               m_Ref02.putEnumerated( cTID( "Lyr " ), cTID( "Ordn" ), cTID( "Trgt" ) );
               m_Dsc01.putReference( cTID( "From" ), m_Ref02 );
               var m_Dsc02 = new ActionDescriptor();
               m_Dsc02.putString( cTID( "Nm  " ), name);
               m_Dsc01.putObject( cTID( "Usng" ), sTID( "layerSection" ), m_Dsc02 );
               executeAction( cTID( "Mk  " ), m_Dsc01, DialogModes.NO );

               return activeDocument.activeLayer;
            }


            function addToSelection(layerName) {
               var m_Dsc01 = new ActionDescriptor();
               var m_Ref01 = new ActionReference();
               m_Ref01.putName( cTID( "Lyr " ), layerName );
               m_Dsc01.putReference( cTID( "null" ), m_Ref01 );
               m_Dsc01.putEnumerated( sTID( "selectionModifier" ), sTID( "selectionModifierType" ), sTID( "addToSelection" ) );
               m_Dsc01.putBoolean( cTID( "MkVs" ), false );

               try {
                  executeAction( cTID( "slct" ), m_Dsc01, DialogModes.NO );
               } catch(e) {}
            }


            function TemporaryAlpha() {
               activeDocument.selection.store((this.alpha = activeDocument.channels.add()));
               activeDocument.selection.deselect();

               this.consume = function() {
                  activeDocument.selection.load(this.alpha);
                  this.alpha.remove();
               }
            }


            // The main function


            //openGroup(activeDocument.activeLayer);
            //openAllLayerSets( app.activeDocument );


            //closeGroup(activeDocument.activeLayer);
            //closeAllLayerSets( app.activeDocument );

            //END HERE-----------------------------------------------

             

             

             

             

             

             

             

             

             

             

             

             

            then, copy the following code and save it into the same folder, and save it under any filename you wish:

             

             

             

             

            //START HERE-----------------------------------------------

            var doc = app.activeDocument;
            var theLayer = activeDocument.activeLayer;
            var theParent = theLayer.parent;
            doc.activeLayer = theParent;

             


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

             

            // The main function
            //@include "LayerSetSupport.jsx"


            if (activeDocument.activeLayer.typename == 'LayerSet')

             

            {app.activeDocument.suspendHistory('closeGroup','closeGroup

             

            (activeDocument.activeLayer)');}

             

            //END HERE-----------------------------------------------

             

             

             

             

             

            now once you run the above script (the one which you gave it your own filename), it should select the parent layer and then collpase it. First make sure that the selected(active) layer is not the parent layer of the group. I beleive the main scripting here was done by csuebele, so thank him for this

             

             

             

            (sorry I don't know how to paste the above code into its own sub-window)

            • 3. Re: Script select parent group by layer
              Chuck Uebele Adobe Community Professional & MVP

              Boo, to put it in a window, select all the text and click on the quote mark icon.  Took me a while to figure that out too.