9 Replies Latest reply on Oct 14, 2013 5:20 PM by boo radley 2013

    Select 2 layers

    boo radley 2013 Level 3

      does anyone know if there's a way to keep the active layer selected, and also select the layer above it? In other words there would be 2 selected layers: the active layer, and the layer directly above it (regardless of its name)

       

      It can be done easily by holdinmg down the shift key and just clicking on the layer above the active one in the layers panel, but I'm not quite sure that it could be done via scripting? Thanks in advance..

        • 1. Re: Select 2 layers
          boo radley 2013 Level 3

          update- I found the keyboard shortcut (had to research the forums a bit). The keyboard shortcut to keep the active layer selected and as well select the layer above it is ALT+SHIFT+]

           

          the problem is that this shortcut ignores invisible layers (layers with their visibility turned off).. it selects the next layer above that has its visibility set to on... so if the layer above is turned off, it ignores it and jumps up to the next 'visible' layer..is there a way to select a layer even if it has its visibility off?

          • 2. Re: Select 2 layers
            Michael L Hale Level 5

            This should work. You can give it a better name if you like.

            function getLayerLayerSectionByIndex( index ) {            
                 var ref = new ActionReference(); 
                 ref.putProperty( charIDToTypeID("Prpr") , stringIDToTypeID("layerSection")); 
                 ref.putIndex(charIDToTypeID("Lyr "), index);
                 return typeIDToStringID(executeActionGet(ref).getEnumerationValue(stringIDToTypeID("layerSection")));
            }; 
            function someFunctionName() {
                if(activeDocument.activeLayer==activeDocument.layers[0]) return;// if top layer do nothing
                var ref = new ActionReference();// get am index of activeLayer
                ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "ItmI" ));
                ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
                var index = executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" ))+Number(activeDocument.layers[activeDocument.layers.length-1].isBackgroundLayer);
                var desc = new ActionDescriptor();
                    var ref = new ActionReference();
                    ref.putIndex( charIDToTypeID('Lyr '), getLayerLayerSectionByIndex( index+1 ) == 'layerSectionEnd' ? index+2:index+1 );// make sure next layer is valid( not invisible layerSet end )
                desc.putReference( charIDToTypeID('null'), ref );
                desc.putEnumerated( stringIDToTypeID('selectionModifier'), stringIDToTypeID('selectionModifierType'), stringIDToTypeID('addToSelection') );
                desc.putBoolean( charIDToTypeID('MkVs'), false );
                executeAction( charIDToTypeID('slct'), desc, DialogModes.NO );
            }
            someFunctionName()
            • 3. Re: Select 2 layers
              c.pfaffenbichler Level 9

              Is it possible the Script overlooks the parental Group if the active Layer is a Group and topmost within a Group?

              • 4. Re: Select 2 layers
                Michael L Hale Level 5

                I threw that together quickly without much testing. It is possible there are several case where it will fail.

                • 5. Re: Select 2 layers
                  boo radley 2013 Level 3

                  so close... it's keeping the active layer selected, then it also selects the layer 3 levels up (for example, the active layer is selected, then the 2 layers above it are left alone, but the next one above those two becomes selected as well)

                   

                  it all leads me back to a similar problem I was having a while ago, which was that the keyboard shortcut to select up or down one layer is ALT+[ (select next layer down) or ALT+] (select next layer up), but it ignored invisible layers, and I was never able to figure out a way to solve it- just want to select layer up or layer down, regardless of its visibility

                  • 6. Re: Select 2 layers
                    Michael L Hale Level 5

                    oops, change this line

                    var index = executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" ))-Number(activeDocument.layers[activeDocument.layers.length-1].isBackgroundLayer);

                     

                    This should do what you want now. The only way I know to add the layer above the active layer is to get the active layer's am index and use that to select the layer above. It doesn't help that the am index has no relation to the DOM layer index, changes depending on if there is a background layer in the document, and that there is an index for the bottom of a layerSet.

                    • 7. Re: Select 2 layers
                      boo radley 2013 Level 3

                      It works now Michael, thanks! Can I ask one more thing - how would you select the next layer above (or down for that matter), regardless of its visibility? It would be the same as ALT+[ or ALT+], but it would also select invisible layers, too... 

                      • 8. Re: Select 2 layers
                        Michael L Hale Level 5

                        The code I posted adds the layer above to the selected layers. To just select the layer above comment out the line

                        desc.putEnumerated( stringIDToTypeID('selectionModifier'), stringIDToTypeID('selectionModifierType'), stringIDToTypeID('addToSelection') );

                         

                        To select the layer below change this line

                         

                        ref.putIndex( charIDToTypeID('Lyr '), getLayerLayerSectionByIndex( index-1 ) == 'layerSectionEnd' ? index-2:index-1 )

                         

                        And you should add a test to check if you are already on the bottom layer.

                        • 9. Re: Select 2 layers
                          boo radley 2013 Level 3

                          Thanks Mike- I adjusted the script and it works - finally, a solution to a problem I've been scoping out for many months now