31 Replies Latest reply on Oct 25, 2013 12:40 AM by cbuliarca

    Expand/Contract Layer Groups

    boo radley 2013 Level 3

      So I spent the evening researching a way to expand/contract a layer group.. normally we have to click on the small triangle icon that appears before the layer name in the layers panel to expand the group, and click it again to fold it all up. So far, I've found information stating that it can't be done via scripting...

       

      I thought up one way to do it, and that would be to search a layer by name (using a different script), and make sure that the layer name being searched for is inside of a contracted group.. and when that layer name is found and selected, it will unfold the layer group and show select the layer. It's quite a runaround though. Is there a more efficient way to do it with a script?

        • 2. Re: Expand/Contract Layer Groups
          boo radley 2013 Level 3

          Thanks, had a look through the topic (I had actualy browsed through one of those links a few months back) and it seems that there's no way to do it via a script. Tried all the sample code there and none of it was able to pull it off... seems the only way to do it is to activate a layer within the layer group and then it will unfold it.

           

          Just gotta make sure one of the layer names within the group is always the same (create an empty layer and name it "unfold" for example) and then use a script which activates the layer "unfold" and then it will expand that group.. but what if several layer groups all contain "unfold".. it might not know which layer group to go to (or it will go to the first one)

           

          It's quite a workaround but it would likely do the trick...

          • 3. Re: Expand/Contract Layer Groups
            Michael L Hale Level 5

            To expand or open a layerSet in the layers panel all you need to do is make it active as you said. But you don't have to worry about layer names and you especially don't need to create a empty layer just so you can expand the set with a script.

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

            Because selecting and working with a layer expands the layerSet more people ask about closing the set or how to determine if a set is open or closed. I don't think there is a good simple way to close a layerSet via scripting. Paul's code at ps-scripts does work to close a layerSet but I don't think it is worth the extra steps just to make a change that doesn't really effect the document. And I don't know of any way to tell the current state of the layerSet in an open document.

            • 4. Re: Expand/Contract Layer Groups
              boo radley 2013 Level 3

              wow, you guys are incredible... I recalled finding a way to open all existing layer groups (as your script does), but I was never able to retrieve it... but this works! Does this script need to open all groups or could it open just the active layer group and then keep the top level layer of that group active (the layer that contains the small triangle to expand/contract)?

              • 5. Re: Expand/Contract Layer Groups
                boo radley 2013 Level 3

                I've played around with the script a little bit, trying to understand it... I've come up with this:

                 

                function openGroup(layerSet){

                    for(var setIndex=0;setIndex<layerSet.length;setIndex++){

                        app.activeDocument.activeLayer = 

                layerSet[setIndex].layers[0];

                        openGroup( layerSet[setIndex]);

                    }

                };

                openGroup( app.activeDocument );

                 

                 

                 

                 

                 

                ...but it does nothing - on the bright side, it doesn't cause an error message either. So I tried another one, closer to Michael's script, and changed 'OpenAllLayerSets' to 'OpenActiveLayerSets' and for some reason it doesn't change anything - all layer groups are unfolded instead of just the active group:

                 

                 

                 

                 

                 

                function openActiveLayerSets( parent ){

                    for(var 

                setIndex=0;setIndex<parent.layerSets.length;setIndex++){

                        app.activeDocument.activeLayer = 

                parent.layerSets[setIndex].layers[0];

                        openActiveLayerSets( parent.layerSets[setIndex]);

                    }

                };

                openActiveLayerSets( app.activeDocument );

                • 6. Re: Expand/Contract Layer Groups
                  Michael L Hale Level 5

                  I am not sure what you want to do. Normally selecting a layer that is inside a layer set opens the layerSet so if the activeLayer is inside a set that set should already be open. In other words if you just want to open one layerSet you don't need a recursive function like those above. Just select one layer.

                   

                  If you want to open all the nested sets inside the selected set then pass that set as the argument( pass the set instead of the document ).

                  • 7. Re: Expand/Contract Layer Groups
                    boo radley 2013 Level 3

                    I'm attempting to open just one layer group, not all. So I would select the parent layer of that group (by parent layer I mean the top layer of the group which contains the small triangle icon to open/close the group), then run your script, and it opens the layer group like we want to, but it also opens all other layer groups in the document as well. I was looking for a way to open just one layer group that is currently selected, and leave all the other layer groups (if any) untouched. Then, I'll examine the code to perform the reverse function which  would be to close the selected layer group (I hope I'm making sense!!) I've been modifying the script for a few hours, and trying to merge portions of other scripts in with it, but so far am not getting anywhere...I can show you all the modifications I've made but it might not be worthwhile since they aren't doing anything just yet..thanks

                    • 8. Re: Expand/Contract Layer Groups
                      Michael L Hale Level 5

                      If a layerSet is the activeLayer and you just want to open that one layerSet do something like this.

                       

                      if(app.activeDocument.activeLayer.typename == 'LayerSet' && app.activeDocument.activeLayer.layers.length>0) app.activeDocument.activeLayer = app.activeDocument.activeLayer.layers[0];

                      • 9. Re: Expand/Contract Layer Groups
                        boo radley 2013 Level 3

                        Michael, I'm so grateful - this is it. I have to thank you again.

                         

                        I examined the code, and from what I can tell, it's stating that if the layer group has 1 layer or more (app.activeDocument.activeLayer.layers.length>0)), then it will activate the layer which is assigned as Index 0 (app.activeDocument.activeLayer.layers[0];).. and as a result, when the layer (Index 0) is activated, it automatically unfolds the group.

                         

                        I'm guessing then, that since it's an indirect approach unfold the layer group, that there actually is no way to fold it up again?

                         

                        This is kind of exciting, because I'm starting to figure out how this coding language works

                         

                         

                         

                        edit- nevermind, I re-read this topic and can see that as of now there seems to be no way to fold a layer group back up. I'll keep searching in the meantime though..

                        • 10. Re: Expand/Contract Layer Groups
                          cbuliarca Level 3

                          Hello, I don't know if you seen my post or not , I've made a script that toggles open/close layer set. You can check it out here:

                           

                          http://buliarcatools.blog.fc2.com/blog-entry-5.html

                          • 11. Re: Expand/Contract Layer Groups
                            boo radley 2013 Level 3

                            cbuliarca - these scritps are FANTASTIC. Every one of them is useful and solves a lot of problems I was trying to overcome with the layers panel. Only one script does not work for me: 'SelectByName'. Here is the error message I get when running the script:

                             

                            error-message.jpg

                            • 12. Re: Expand/Contract Layer Groups
                              cbuliarca Level 3

                              Great, I'm glad they helped, I'm guessin it's a error with the ScriptUI listbox subitems, can you open the script aand modify all the lines that looks like this:

                               

                              var mbSimpleSwitch = false;//make this true if you have problems with the UI

                               

                              to this:

                               

                              var mbSimpleSwitch = true;//make this true if you have problems with the UI

                               

                              tell me if you will have this problem

                              • 13. Re: Expand/Contract Layer Groups
                                boo radley 2013 Level 3

                                it works!! I have been searching for a way to do this for a while now... this is the best script to search for layers.

                                 

                                the only difference is that my 'BCM FastSelection' looks different - it's missing the titles across the top ("Layer Name", "parents", Index", "isLayerSet?"). Also, I cannot left-click to select the layer. Here is the image I get:

                                 

                                fast-selection.jpg

                                • 14. Re: Expand/Contract Layer Groups
                                  cbuliarca Level 3

                                  this is wierd, on my computer I am able to select, to be honest I don't kow how I can solve this....probably it's another bug in the ScriptUI....what version of Photoshop are you using?

                                   

                                  about the names ("Layer Name", "parents", Index", "isLayerSet?") on the top I've decided to not show them in this version of the ui, because I thought to go throw fields by pressing tab on the keyboard, and than select with the rightdown arrow and control or shift pressed.

                                   

                                  If I would have added another text fiel with the names you would need to press tab once more. To pass throw that field.

                                   

                                  But if you think that will help, I will add it.

                                  • 15. Re: Expand/Contract Layer Groups
                                    boo radley 2013 Level 3

                                    Excellent! It works perfect now (I didn't know about hitting tab and/or using 'up/down' to select the layers). Thanks to you and everyone else here who helps with these awesome scripts- it's always very appreciated

                                    • 16. Re: Expand/Contract Layer Groups
                                      boo radley 2013 Level 3

                                      question - how do we make sure that the columns are aligned properly? Right now, some of the columns are off-center. I noticed that if a layer name is more than 10 letters, then it begins to indent. So I tried changing all the "x:10" values in the script to a higher number but so far it hasn't worked.

                                       

                                      fast-selection2.jpg

                                      • 17. Re: Expand/Contract Layer Groups
                                        cbuliarca Level 3

                                        till now I didn't found any fast good solution to align the columns in this version of ui. The problem is that the entire line is a string and the solution I found, but doesn't always work, is to create some tabs character to align the "|" character. The problem is that I can't exactly measure the text before the tabs. Because every character has his own width and even if I've made a table with each character's width the combination between them is actually not as expected. For example let's say I have the name "TAI"

                                        the width of the entire text should be : 7(T)+1(the space)+9(A)+1(the space)+2(I) = 20 but actually it's less because the A character goes inside T so the width of them will be less.... probably I should create some conditional code for each special cases like this but this will probably be some tedious process, to find all of the combinations possible....

                                         

                                        If anyone has another idea I will be grateful if he will share it.

                                        • 18. Re: Expand/Contract Layer Groups
                                          boo radley 2013 Level 3

                                          that's fine... your script is excellent .  It's much easier than using the normal Photoshop layers panel.  I use it every day now.

                                          • 19. Re: Expand/Contract Layer Groups
                                            cbuliarca Level 3

                                            Great, I'm glad it's helping you.

                                            • 20. Re: Expand/Contract Layer Groups
                                              Michael L Hale Level 5

                                              cbuliarca wrote:

                                               

                                              The problem is that the entire line is a string...

                                              Couldn't you set the font for that control to use a mono spaced font?

                                              • 21. Re: Expand/Contract Layer Groups
                                                cbuliarca Level 3

                                                Hi Michael,

                                                 

                                                I never knew about the mono spaced fonts, I did a search and seen what's about, and seams to me that this kind of font could solve my problems. I will give it a try.

                                                Thank you very much for your suggestion

                                                • 22. Re: Expand/Contract Layer Groups
                                                  cbuliarca Level 3

                                                  following Michael's suggestion I've managed to adjust the columns for the second UI as well.

                                                   

                                                  Thank you Michael.

                                                   

                                                  You need to take the scripts again from my site:

                                                   

                                                  http://buliarcatools.blog.fc2.com/blog-entry-5.html

                                                  • 23. Re: Expand/Contract Layer Groups
                                                    boo radley 2013 Level 3

                                                    hello cbuliarca, I have tried the updated script and am receiving this error:

                                                     

                                                    fast-selection3.jpg

                                                    • 24. Re: Expand/Contract Layer Groups
                                                      cbuliarca Level 3

                                                      just do the same thing with the

                                                       

                                                      var mbSimpleSwitch = true;

                                                       

                                                      The uploaded script uses the first version of interface but if you put the variable to true it will use the other version.

                                                      • 25. Re: Expand/Contract Layer Groups
                                                        JJMack Most Valuable Participant

                                                        Using script code found here I put some together that give me the functions needed.  I tested creating actions and assigned F keys during testing for fast access.

                                                         

                                                        Open Target

                                                         

                                                         

                                                        // The main function
                                                        //@include "LayerSetSupport.jsx"
                                                        
                                                        if (activeDocument.activeLayer.typename == 'LayerSet') {app.activeDocument.suspendHistory('openGroup','openGroup(activeDocument.activeLayer)');}
                                                        
                                                        

                                                         

                                                        Close Target

                                                         

                                                        // The main function
                                                        //@include "LayerSetSupport.jsx"
                                                        
                                                        
                                                        if (activeDocument.activeLayer.typename == 'LayerSet') {app.activeDocument.suspendHistory('closeGroup','closeGroup(activeDocument.activeLayer)');}
                                                        
                                                        

                                                         

                                                        Open All Layer Groups

                                                         

                                                        // The main function
                                                        //@include "LayerSetSupport.jsx"
                                                        
                                                        
                                                        openAllLayerSets( app.activeDocument );
                                                        
                                                        

                                                         

                                                        Close All Layer Groups

                                                         

                                                        // The main function
                                                        //@include "LayerSetSupport.jsx"
                                                        
                                                        
                                                        app.activeDocument.suspendHistory('closeAllGroup','closeAllLayerSets( app.activeDocument )');
                                                        
                                                        
                                                        

                                                         

                                                         

                                                        LaySetSupport

                                                         

                                                         

                                                        //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 );
                                                        
                                                        
                                                        

                                                         

                                                        Message was edited by: JJMack

                                                        • 26. Re: Expand/Contract Layer Groups
                                                          boo radley 2013 Level 3

                                                          cbuliarca - works great now!! definitely a script that will stay in my collection and I'll be using it every day - the nice thing is that it has a 'search layer' function just like CS6.. (it's actually a little better than CS6 because it auto-completes your layer name for you when you start typing - very nice)

                                                           

                                                          JJmack - these are perfect - the only thing with 'close target' and 'close all layer groups' is that when it closes the layer group(s), it also deletes the color coding of the layer (for example if the layer was set to blue, it resets it back to nothing when closing the group) - I always need to color code my layers or else I'd lose my mind (if I haven't already lol)

                                                          • 27. Re: Expand/Contract Layer Groups
                                                            Michael L Hale Level 5

                                                            That is one reason I don't think there is a good way to close a layerSet. To be clear the code posted does not close the set. It mimics closing by making a new layerSet and moving layers.

                                                             

                                                            It should be possible for the script to maintain the set's layer color by getting the current set's color so it can set the color of the new set.. But there are some settings like blend-if settings that  can't be accessed by a script so it can't copy to the new set.

                                                            • 28. Re: Expand/Contract Layer Groups
                                                              JJMack Most Valuable Participant

                                                              I agree the script uses a sledge hammer and flying chips  may loose some things. Script is better then actions for actions can not open or close layer sets themselves.  You can use a script an action to open layer sets,  but Closeing a layer set is an other story.  In an action when you create a group from selected layer the layer set is created closed.  That is more or less what these script do. They save  some information about a layer set ungroup the layer set  then select all that was in the group and recreates the best it can the group.  I see problem with layer set with layer mask for example  hide all layer mask  get changed to reveal all mask and vector mask are lost.... My testing was poor in fact the close all layerset routine messes up all layerset with layer mask Layers mask are changed and if you run open all layerset and close all layersets a couple of time all layer set layer mask will be lost.

                                                               

                                                              I have address thelayerset  layer mask problem some, Vector mask on layer sets are not supported and will be lost. Hide all layer mask still get invetted.  However nornal raster layter mask  on layer groups are retained.

                                                               

                                                              LayerSetSupport.jsx

                                                               

                                                              //For code readability
                                                              function cTID(s){return charIDToTypeID(s)}
                                                              function sTID(s){return stringIDToTypeID(s)}
                                                              // =============================
                                                              
                                                              
                                                              function openAllLayerSets( parent ){
                                                                  var saveActivelayer = app.activeDocument.activeLayer;
                                                                  for(var setIndex=0;setIndex<parent.layerSets.length;setIndex++){
                                                                      app.activeDocument.activeLayer = parent.layerSets[setIndex].layers[0];
                                                                      openAllLayerSets( parent.layerSets[setIndex]);
                                                                  }
                                                                  app.activeDocument.activeLayer = saveActivelayer;
                                                              };
                                                              
                                                              
                                                              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') {app.activeDocument.activeLayer = layer; 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 );
                                                              
                                                              
                                                              

                                                              Message was edited by: JJMack

                                                              • 29. Re: Expand/Contract Layer Groups
                                                                cbuliarca Level 3

                                                                Yes that's right, the script on toggleOpen_CloseGroup is restrictive, I have this problem as well, for example the script can't see if a vector mask is enabled or linked, and as Michael said there are some functions that can't be accessed by script.

                                                                One other problem the script has is that if you have some colored layers inside your set and your set has another color when you will close the set with the script you will lose the color to your layers as well, they will change to the color of the set.

                                                                • 30. Re: Expand/Contract Layer Groups
                                                                  JJMack Most Valuable Participant

                                                                  Yes you and I started with the script from posted on ps-scripts by TeddyBear.  He call it ridiculous but it works and is fast. It has its faults and I don't use vector mask or color coding so its OK for me.  You took it to up many notches to the height of ridiculousness.  To code that much to stimulate what can be done better by pointing the mouse cursor and clicking the left mouse button.  The good part is you learned a lot about scripting Photoshop in the process.  I learned that its not possible to write a perfect script for it, So I really don use it. Sill I learned quite a bit about the workings of Photoshop in the process.

                                                                  • 31. Re: Expand/Contract Layer Groups
                                                                    cbuliarca Level 3

                                                                    Yes you are so right, I've learned a lot on doing this script, before it I didn't knew how to use AM, it's great, to bad that there is no documentation for it. All I've learned I've learned from here and ps-scripts.com. Once again thank you all for taking the time to answer the questions and more important thank you for sharing your knowledge. You are right about the script as well, so much time spent on something that can be solved by moving and clicking the mouse.... but you know at first I thought that it can be a good idea just to press a key and open the group and TeddyBear gave a great ridiculous script on doing that, now as I seen that it can't be perfect I'm not so exited anymore, but anyway I'm grateful for the fact that it opened my eyes on AM.