21 Replies Latest reply on Jan 18, 2016 10:58 PM by mickyhulse

    Select only highlighted layers?

    mickyhulse Level 1

      Is there any way to get at only highlighted layers?

       

      For example, I want to get an array/object that contains only the layers highlighted in blue:

       

      Screenshot 2015-12-20 21.40.27.png

       

      I'd prefer to not select the artwork in order to do this.

       

      Thanks!

      M

        • 1. Re: Select only highlighted layers?
          CarlosCanto Adobe Community Professional & MVP

          record an Action to Hide or Lock Other Layers, play such action, then loop thru all layers and do your thing on the ones that are not locked or hidden.

           

          you can incorporate the action creation in your script if you need to distribute it to others.

          • 2. Re: Select only highlighted layers?
            tpk1982 Level 4

            Hi..

             

            If image is the main problem..then you can get the layer objects..if a layer has images then you ignore that.. simple

             

            var myDoc = app.activeDocument;  
                for(var i = 0; i < myDoc.layers.length; i++)  
                {     
                    if(myDoc.layers[i].allGraphics.length!=0)
                    {
                        alert(myDoc.layers[i].name)
                        }
                 }   
            
            • 3. Re: Select only highlighted layers?
              Vinicius Baptista Level 2

              Hi Micky,

               

              Please somebody correct me if I'm wrong, I think is not possible because layers is not a object with selection properties, but you can find other ways to manipulate the layers like locked, visible, selected pageItems, selected placedItems, selected rasterItems or whatever others particularities in your layers, depending of the purpose you need.

               

              I hope you will be able to solve your problem.

               

              Best regards

              -Vinicius

              • 4. Re: Select only highlighted layers?
                Silly-V Adobe Community Professional

                Isn't "allGraphics" from InDesign scripting?

                • 5. Re: Select only highlighted layers?
                  Qwertyfly... Level 4

                  Give this a shot.

                  it will leave you with an array containing the layers that you had selected in the layers panel when the script is run.

                  you can see down the bottom I loop this array so you can see each ones name and original visibility

                   

                  function get_selected_layers(){
                      var doc = app.activeDocument;
                      var lays = doc.layers;
                      var OriginalLayers = [];
                      for(var i = 0; i < lays.length; i++){
                          OriginalLayers.push(lays[i].visible);
                          if(!lays[i].visible){
                              lays[i].visible = true;
                          }
                      }
                  
                  
                      make_action();
                  
                  
                      var SelectedLayers = [];
                      for(var i = 0; i < lays.length; i++){
                          if(lays[i].visible){
                              SelectedLayers.push(lays[i]);
                          }
                      }
                  
                  
                      for(var i = 0; i < lays.length; i++){
                              lays[i].visible = OriginalLayers[i];
                      }
                  
                  
                      return SelectedLayers;
                  
                  
                      function make_action(){
                          // Set you action name and the set name that it belongs to here
                          var myAction = "Select_Layers";
                          var mySet = "Scripted_Actions";
                          //---------------------------------------------------------------------------------
                          var currentInteractionLevel = app.userInteractionLevel;
                          app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;
                          var actionStr = [
                              '/version 3',
                              '/name [ 16',
                              '53637269707465645f416374696f6e73',
                              ']',
                              '/isOpen 0',
                              '/actionCount 1',
                              '/action-1 {',
                              '/name [ 13',
                              '53656c6563745f4c6179657273',
                              ']',
                              '/keyIndex 0',
                              '/colorIndex 0',
                              '/isOpen 0',
                              '/eventCount 1',
                              '/event-1 {',
                              '/useRulersIn1stQuadrant 0',
                              '/internalName (ai_plugin_Layer)',
                              '/localizedName [ 5',
                              '4c61796572',
                              ']',
                              '/isOpen 0',
                              '/isOn 1',
                              '/hasDialog 0',
                              '/parameterCount 3',
                              '/parameter-1 {',
                              '/key 1836411236',
                              '/showInPalette -1',
                              '/type (integer)',
                              '/value 7',
                              '}',
                              '/parameter-2 {',
                              '/key 1937008996',
                              '/showInPalette -1',
                              '/type (integer)',
                              '/value 23',
                              '}',
                              '/parameter-3 {',
                              '/key 1851878757',
                              '/showInPalette -1',
                              '/type (ustring)',
                              '/value [ 11',
                              '48696465204f7468657273',
                              ']',
                              '}',
                              '}',
                              '}'
                          ].join('\n');
                          createAction(actionStr);  
                          app.doScript(myAction, mySet, false);
                          actionStr = null;
                          app.unloadAction(mySet,"");
                          app.userInteractionLevel = currentInteractionLevel;
                      }
                  
                  
                      function createAction (str) {  
                          var f = new File('~/ScriptAction.aia');  
                          f.open('w');  
                          f.write(str);  
                          f.close();  
                          app.loadAction(f);  
                          f.remove();  
                      }
                  }
                  
                  
                  var myLayers = get_selected_layers();
                  for(var i = 0; i < myLayers.length; i++){
                      alert(myLayers[i].name + ", " + myLayers[i].visible);
                  }
                  
                  1 person found this helpful
                  • 6. Re: Select only highlighted layers?
                    mickyhulse Level 1

                    Wow! Thanks for the replies everyone!!! I really appreciate the help!

                     

                    I'm going to play around with all the ideas/code mentioned in this thread and I'll be back with my findings.


                    Thanks again for taking the time to help out!

                     

                    Also, Qwertyfly..., your code looks really cool! You're creating an action on the fly? That's so interesting! I am looking forward to testing the code. Seems like that technique could really open the doors to a whole bunch of cool scripting ideas.

                     

                    I'll be back!

                    Thanks again.

                     

                    Cheers,

                    M

                    • 7. Re: Select only highlighted layers?
                      Qwertyfly... Level 4

                      I love the ability to include actions in js scripts, it opens so many possibles.

                      I do the same with VB.

                      shame VB is not supported on mac.

                       

                      as this action is only used once this is fairly straight forward,

                      there are a few other tricks for actions you want access to multiple time in a script to help speed things up.

                       

                      Hope it does what your after.

                      Let me know if you need any help implementing it with the rest of your script

                      • 8. Re: Select only highlighted layers?
                        Vinicius Baptista Level 2

                        Hey Qwertyfly... you are incredible man hahahah..... Your solution is wonderful, working like a charm, I love the way you have found send to array the original visibility of layers, and use a action to identify the selected layers, congratulations, thank you for one more time helped us.

                        • 9. Re: Select only highlighted layers?
                          Qwertyfly... Level 4

                          this one was fun.

                          tthere were a number of things I had to try before I settled on the visibility property.

                          THis is was the cleanest solution I could come up with.

                          Hope it helps out a few people.

                          • 10. Re: Select only highlighted layers?
                            mickyhulse Level 1

                            Hi Qwertyfly...,

                             

                            Your script works perfectly! I'm in the process of getting it to work with a BTM-based script. I'll update this thread if I make any significant progress.

                             

                            A few questions … What does this code do(?):

                             

                            app.userInteractionLevel;

                             

                            ... and:

                             

                            UserInteractionLevel.DONTDISPLAYALERTS;

                             

                            What is userInteractionLevel?

                             

                            Also, in the action string, what do the hashes mean?

                             

                            53637269707465645f416374696f6e73

                            53656c6563745f4c6179657273

                            4c61796572

                            1836411236

                            1937008996

                            1851878757

                            48696465204f7468657273

                             

                            Does it matter what those strings are? Do they have any meaning?

                             

                            Also, how did you make the action? I'm guessing you used the standard actions palette, and then saved/copied/pasted/converted the action code into the format we see in your script?

                             

                            Sorry for all the questions. Just trying to fully grok your script.

                             

                            Thanks again for the interesting and functional solution!


                            Cheers,

                            Micky

                            • 11. Re: Select only highlighted layers?
                              mickyhulse Level 1

                              Also, what doe the action actually do? I hate to sound ignorant, but I don't understand what exactly it does by just looking at the code.

                               

                              What are the action parameters for?

                               

                              I am looking forward to fully understanding this code!

                              • 12. Re: Select only highlighted layers?
                                mickyhulse Level 1

                                mickyhulse wrote:

                                 

                                Also, what doe the action actually do? I hate to sound ignorant, but I don't understand what exactly it does by just looking at the code.

                                 

                                What are the action parameters for?

                                 

                                I am looking forward to fully understanding this code!


                                Ok, so, I think I understand the basic logic of get_selected_layers:


                                1. First for loop, you record existing layer visibility
                                2. The call to make_action will make all selected layers visible and hide the rest (though, I don't yet understand how the action does this)
                                3. Next for loop, you grab the selected visible layers
                                4. Next for loop, you restore visibility
                                5. Finally, return the selected layers array


                                Pretty dang cool!

                                • 13. Re: Select only highlighted layers?
                                  mickyhulse Level 1

                                  app.userInteractionLevel;

                                   

                                  ... and:

                                   

                                  UserInteractionLevel.DONTDISPLAYALERTS;

                                   

                                  What is userInteractionLevel?

                                   

                                  Ah, I see what that is now.

                                   

                                  That bit of code saves the current UserInteractionLevel and then temporarily turns off alerts. After executing the action your code resets the userInteractionLevel back to what it was before the script ran.

                                   

                                  It's funny, I totally mis-read DONTDISPLAYALERTS as "don't display layer TS". Lol. That damn constant needs underscores for readability. Ha!

                                  • 14. Re: Select only highlighted layers?
                                    Silly-V Adobe Community Professional

                                    In the action strings, the hashes are hex-encoded words that the action language uses to store the various strings. The way this entire action string is obtained is by saving the action .aia file from the action panel and at that point you can read the .aia as a text file in a text editor. It's actually possible to alter the parameters of actions by changing these strings in some limited ways - some more useful than others - such as a file path of the destination file of a saving or exporting procedure!

                                    1 person found this helpful
                                    • 15. Re: Select only highlighted layers?
                                      mickyhulse Level 1

                                      Ah, I see! Thanks for the reply and explanation Silly-V!

                                       

                                      I'm playing with action scripts now. I never really utilized actions in Illustrator before this. Pretty cool stuff.

                                       

                                      Thanks again!!!!

                                      • 16. Re: Select only highlighted layers?
                                        Qwertyfly... Level 4

                                        I think you've got the main gist of it. I'm happy to go through any parts your still un sure about. Only on the phone for today and tomorrow. So I'll be back on a computer after that and I'll bash together a bit of a rundown.

                                        glad you like the script. It's good to know your going to get more out of it then just a script. your enthusiasm is great.

                                        • 17. Re: Select only highlighted layers?
                                          mickyhulse Level 1

                                          Qwertyfly... wrote:

                                           

                                          I think you've got the main gist of it. I'm happy to go through any parts your still un sure about. Only on the phone for today and tomorrow. So I'll be back on a computer after that and I'll bash together a bit of a rundown.

                                          glad you like the script. It's good to know your going to get more out of it then just a script. your enthusiasm is great.

                                           

                                          Awesome! Thanks again Qwertyfly...!

                                           

                                          I think I now understand most of the script.

                                           

                                          If I had to pick one thing for you to explain in more detail, I'd love to know how you built the Action. I was able to convert the action string into an installable action:

                                           

                                          /version 3
                                          /name [
                                               4
                                               74656d70
                                          ]
                                          /isOpen 0
                                          /actionCount 1
                                          /action-1 {
                                               /name [
                                                    4
                                                    74656d70
                                               ]
                                               /keyIndex 0
                                               /colorIndex 0
                                               /isOpen 0
                                               /eventCount 1
                                               /event-1 {
                                                    /useRulersIn1stQuadrant 0
                                                    /internalName (ai_plugin_Layer)
                                                    /localizedName [
                                                         5
                                                         4c61796572
                                                    ]
                                                    /isOpen 0
                                                    /isOn 1
                                                    /hasDialog 0
                                                    /parameterCount 3
                                                    /parameter-1 {
                                                         /key 1836411236
                                                         /showInPalette -1
                                                         /type (integer)
                                                         /value 7
                                                    }
                                                    /parameter-2 {
                                                         /key 1937008996
                                                         /showInPalette -1
                                                         /type (integer)
                                                         /value 23
                                                    }
                                                    /parameter-3 {
                                                         /key 1851878757
                                                         /showInPalette -1
                                                         /type (ustring)
                                                         /value [
                                                              11
                                                              48696465204f7468657273
                                                         ]
                                                    }
                                               }
                                          }

                                           

                                          What is ai_plugin_Layer? What steps did you take to build this action?

                                           

                                          When I inspect the action in the actions palette, it doesn't seem like there's much there to dissect.

                                           

                                          Oh, and one thing I got wrong earlier …

                                           

                                          The first for loop in your script records original layer visibility, but it also sets all layers to visible; I noticed the action doesn't work without this temporary step.

                                           

                                          Thanks again for your help!

                                           

                                          I was totally able to get your code to work with a BridgeTalk script I'm working on!

                                           

                                          Thank you!!!!

                                          • 18. Re: Select only highlighted layers?
                                            Qwertyfly... Level 4

                                            the script starts by making a list of what layers are visible and which are not.

                                            while doing that it sets all layers to visible.

                                            the action is then run which runs the Hide Others command.

                                            now the script can tell you which ones are visible. these are the ones you had selected.

                                            last of all it sets the original visibility of each layer using the list it created at the start.


                                            the action was recorded in the usual way. then saved.

                                            open that saved aia file with a text editor to get the action string.

                                            it does need to be modified.

                                            but all that take is to add ' to the start of each line.

                                            and ', to the end of all lines, except the last just gets the '.

                                            in essence, all we do is recreate the manually created aia file using JS.


                                            sorry about the long wait for a reply, I've been off grid for a few weeks.

                                            let me know if you want more info.

                                            • 19. Re: Select only highlighted layers?
                                              mickyhulse Level 1

                                              Thanks a billion for the reply Qwertyfly..., I greatly appreciate the help. And no worries on reply time. There's no pressure from me for replies … I totally understand how we can all get too busy to reply right away.

                                               

                                              Thanks so much for the clarification on the code. I was able to successfully implement it in this plugin:

                                               

                                              mhulse/illy-anim-redux - JavaScript - GitHub

                                               

                                              I'm pretty satisfied with the end result. Thanks a bunch for your help!

                                               

                                              the action is then run which runs the Hide Others command.

                                              ...

                                              the action was recorded in the usual way. then saved.

                                               

                                              That's interesting! Thanks so much for the details!

                                               

                                              I hate to keep bugging you, but where would I find the "Hide Others" command? I see something like that under the Illustrator CC menu, but I thought that was an application-wide thing on OS X?

                                               

                                              Thanks again (to everyone) and Qwertyfly... for the help!!! I greatly appreciate the assist.

                                              • 20. Re: Select only highlighted layers?
                                                Qwertyfly... Level 4

                                                select some layers, then in the flyout menu of the layers panel you will find the hide others...

                                                Capture.JPG

                                                • 21. Re: Select only highlighted layers?
                                                  mickyhulse Level 1

                                                  Ahhhhh! I don't think I would have ever looked there. Hehehe! Thanks Qwertyfly...!