18 Replies Latest reply on Jun 12, 2017 1:25 PM by Aristarco Palacios

    How to sort objects alphabetically (inside of a layer)?

    zawij

      How to sort objects alphabetically (inside of a layer) on layer list?

      I have already tried scripts for Layer sorting. They work indeed for the layers, but not the objects themselves packed inside.

        • 1. Re: How to sort objects alphabetically (inside of a layer)?
          CarlosCanto Adobe Community Professional & MVP

          post your script, someone will help you tweak it to work with the objects instead of layers,

          • 2. Re: How to sort objects alphabetically (inside of a layer)?
            zawij Level 1

            It is not my script, but it works with layers. I found it googling some time ago.

             

            #target "Illustrator" 

              

            /*

                 sort layers (with sublayer)

            */ 

              

            var doc = app.documents[0]; 

            var lay = doc.layers; 

            var array_primary = []; 

            for (var i=0, iL=lay.length; i < iL ; i++) { 

                 if (lay[i].layers.length === 0) { 

                      array_primary.push( lay[i].name ); 

                 } 

                 else { 

                      array_primary.push( lay[i].name ); 

                      var array_secondary = []; 

                      for (var j=0, jL=lay[i].layers.length; j < jL ; j++) { 

                       array_secondary.push(lay[i].layers[j].name); 

                      }; 

                      var result2 = array_secondary.sort( function (a,b) { return a > b }); 

                      // sort sublayers 

                      sort_layer (lay[i], result2); 

                 } 

            }; 

            var result1 = array_primary.sort( function(a,b) { return a > b } ); 

              

            // sort layers 

            sort_layer (doc, result1); 

              

            function sort_layer (obj, array) { 

                 for (var ri=0, riL=array.length; ri < riL ; ri++) { 

                      obj.layers.getByName(array[ri]).zOrder( ZOrderMethod.SENDTOBACK ); 

                 }; 

            • 3. Re: How to sort objects alphabetically (inside of a layer)?
              captainfodder

              Likewise, I'm interested to see if this can be resolved. The script I've been attempting to use is this: Click Here

               

              Basically what I'm trying to achieve is a layer group by state, then by provider, then by city, then the dot representing the city.

              Example:

              • Wyoming
                • Charter
                  • Cable
                    • Gillette, WY
                    • Albin, WY
                    • Additional Cities +

               

              That above script so far will sort everything except the final (deepest) layer, the one with the object (my dot).

              • 4. Re: How to sort objects alphabetically (inside of a layer)?
                williamadowling Level 4

                do you know how many "layers" in the hierarchy there will be? or is that an unknown value? for example.. the "Town, WY" always be under "cable" which will always be under "charter" etc? or is it possible that there could be more or less levels within the hierarchy?

                • 5. Re: How to sort objects alphabetically (inside of a layer)?
                  captainfodder Level 1

                  williamadowling wrote:

                   

                  do you know how many "layers" in the hierarchy there will be? or is that an unknown value? for example.. the "Town, WY" always be under "cable" which will always be under "charter" etc? or is it possible that there could be more or less levels within the hierarchy?

                  Thanks for the reply,

                   

                  Unfortunately the hierarchy is different. I'll give you a couple examples:

                  • Wyoming
                    • ACT
                      • Fiber
                        • Newcastle, WY
                        • Sundance, WY
                        • Gillette, WY
                        • Casper, WY
                        • (more beyond this, 18 cities total)
                    • Charter
                      • Cable
                        • Jackson, WY
                        • Riverton, WY
                        • Gillette, WY
                        • Casper, WY
                        • (more beyond this, 24 cities total)
                    • CenturyLink
                      • DSL
                        • Gillette, WY
                        • Casper, WY
                        • (more beyond this, 34 cities total)
                      • T1
                        • Gillette, WY
                        • Casper, WY
                        • (more beyond this, well over 100 cities total)
                      • Fiber
                        • Gillette, WY
                        • (more beyond this, 50+ cities)

                   

                  I hope this helps out. If there is some way to sort each of these objects underneath their layers with one script, and it needs to be modified to account for the amount of layers I can do that, I just don't know how to write the script.

                  • 6. Re: How to sort objects alphabetically (inside of a layer)?
                    williamadowling Level 4

                    great. the name of the layers in the hierarchy don't matter so much. i just needed to know if they were going to go any deeper. is it at all possible that there could be a separate layer inside of, for example, cable? or is that as deep as it could go?

                     

                    i'll assume that it can't.. because i don't know why one town/city would act as a group, based on this model. let me put together the basic nested loop structure. i don't actually know the syntax for alphabetizing, but i'll indicate where that code should go within your loops.

                     

                    **EDIT**

                     

                    the city/town part. are those groups or paths? do you have a screenshot of the actual layers panel expanded such as your example you typed out? that would help.

                    • 7. Re: How to sort objects alphabetically (inside of a layer)?
                      captainfodder Level 1

                      Awesome, The cities are the deepest layer/object there is. There isn't anything below the cities, and the cities them selves are just a circle with a solid gold fill (object). To answer your question if there were any more layers beneath the "service type," for example, cable, the answer is no. In the examples I gave above, that is the exact structure/hierarchy, except some companies (CenturyLink) offer multiple service types, so they have more than one layer there, but the cities contained in each layer is still the bottom level.

                       

                      I've attached a screen shot of what it looks like, because unfortunately I do not know what the "dots" actually are.

                      screencap.png

                      • 8. Re: How to sort objects alphabetically (inside of a layer)?
                        williamadowling Level 4

                        perfect. thanks. almost finished. = )

                        • 9. Re: Re: How to sort objects alphabetically (inside of a layer)?
                          williamadowling Level 4

                          So this is your looping structure. i'm not saying it's the BEST way to do it.. i'm sure there's some advanced way that is much cleaner than i've made this. But this will dig you all the way into your citys/towns objects.

                           

                           

                          function alphabetize(){
                            if (app.documents.length > 0){
                            var docRef = app.activeDocument;
                            var state = docRef.layers;
                          
                            for (a = 0; a < state.length; a++){ // loop through your states
                            var  provider = state[a].groupItems;
                            if (provider.length > 0){
                          
                            for (b = 0; b < provider.length; b++){ // loop through providers
                            var product = provider[b].groupItems;
                            if (product.length > 0){
                          
                            for (c = 0; c < product.length; c++){ // loop through products
                            var town = product[c].pathItems;
                            if (town.length > 0){
                          
                            for (d = 0; d < town.length; d ++){ // loop through towns
                          
                            //alphatbetization code goes here
                          
                            } // end for loop D
                            } // end if exists town
                            } // end for loop C
                            } // end if exists product
                            } // end for loop B
                            } // end if exists top groups
                            } // end for loop A
                            } // end if document exist
                            else{
                            alert ("No document Open");
                            } // end else no document open
                            } // end function
                          alphabetize();
                          
                          
                          • 10. Re: How to sort objects alphabetically (inside of a layer)?
                            captainfodder Level 1

                            Do I need to have anything selected for this script to work? When I'm running it I'm not seeing anything changing.

                             

                            *** EDIT ***

                             

                            So this is the alphabetization code I'm trying to use, but my guess is it's incorrect:

                            sort_layer (docRef, result1);
                            
                            function sort_layer (obj, array) {
                              for (var ri=0, riL=array.length; ri < riL ; ri++) {
                              obj.layers.getByName(array[ri]).zOrder( ZOrderMethod.SENDTOBACK );
                              };
                            }
                            
                            • 11. Re: How to sort objects alphabetically (inside of a layer)?
                              williamadowling Level 4

                              this script doesn't actually DO anything. it's just getting you inside the layers to the level you want. like i said i don't know how to actually alphabetize... i can think of a way, but i KNOW it's not the right way because it's really clunky and verbose. and if i'm honest, i can't really see what's doing the alphabetization in the script that you posted. i'll see what i can find out. i'm all about learning how to do this too.

                               

                              further information. this script doesn't actually act upon objects. only modifies their indexes in the layers panel. therefore, no selection is necessary and having any/some/all objects selected will not affect the outcome..

                               

                              let me see what i can do about alphabetizing

                              • 12. Re: Re: How to sort objects alphabetically (inside of a layer)?
                                captainfodder Level 1

                                I see... Well I apologize I'm not more help as I barely understand this script.

                                 

                                I've found another array for alphabetizing, would this help in any manner?

                                var layers = activeDocument.layers;
                                var layersArray = [];
                                var len = layers.length;
                                
                                
                                
                                // store all layers in an array
                                for (var i = 0; i < len; i++) {
                                layersArray.push(layers[i]);
                                }
                                
                                // sort layer top to bottom
                                layersArray.sort();
                                
                                for (i = 0; i < len; i++) {
                                layersArray[i].move(layers[i], ElementPlacement.PLACEBEFORE);
                                }
                                
                                • 13. Re: How to sort objects alphabetically (inside of a layer)?
                                  williamadowling Level 4

                                  i figured it out actually. much easier than i imagined. i'm finishing up now.

                                  • 14. Re: Re: How to sort objects alphabetically (inside of a layer)?
                                    williamadowling Level 4

                                    hmm.. i'm perplexed.. here's what i got.. it is indeed alphabetizing the objects.. but it is NOT changing the position of the layers.. for some reason.

                                     

                                    i think it's because it's outside the "d" for loop, but idk.. anybody out there that's smarter than I care to help out??

                                     

                                    function alphabetize(){ 
                                      if (app.documents.length > 0){ 
                                      var docRef = app.activeDocument; 
                                      var state = docRef.layers; 
                                    
                                      for (a = 0; a < state.length; a++){ // loop through your states 
                                      var  provider = state[a].groupItems; 
                                      if (provider.length > 0){ 
                                    
                                      for (b = 0; b < provider.length; b++){ // loop through providers 
                                      var product = provider[b].groupItems; 
                                      if (product.length > 0){ 
                                    
                                      for (c = 0; c < product.length; c++){ // loop through products 
                                      var town = product[c].pathItems; 
                                      if (town.length > 0){ 
                                      var townList = [];
                                    
                                      for (d = 0; d < town.length; d ++){ // loop through towns 
                                      townList.push(town[d].name);
                                      } // end for loop D 
                                      townList.sort();
                                      for (e = 0; e > townList.length; e++){ // re-order towns by name
                                      town[e].zOrder(ZOrderMethod.SENDTOBACK);
                                      alert(towns);
                                      } // end for loop E
                                      } // end if exists town 
                                      } // end for loop C 
                                      } // end if exists product 
                                      } // end for loop B 
                                      } // end if exists providers 
                                      } // end for loop A 
                                      } // end if document exist 
                                      else{ 
                                      alert ("No document Open"); 
                                      } // end else no document open 
                                      } // end function 
                                    alphabetize(); 
                                    
                                    • 15. Re: Re: Re: How to sort objects alphabetically (inside of a layer)?
                                      pixxxel schubser Level 5

                                      captainfodder schrieb:

                                       

                                      … I've found another array for alphabetizing, would this help in any manner? …

                                      Hi captainfodder,

                                      the method sort() is not the best for all things. But it's the easiest way for simple sorting.

                                       

                                      And if only path items in the layers exists, you can rebuild this snippet e.g. like that:

                                      var layers = activeDocument.layers;
                                      var objArray = [];
                                      // this is now for the topmost layer only 
                                      // add a loop through all layers
                                      
                                      var layPI = layers[0].pathItems;
                                      var len = layPI.length;  
                                      
                                      // store all layer items in an array 
                                      for (var i = 0; i < len; i++) {
                                          objArray.push(layPI[i]);
                                          }
                                      
                                      // sort layer top to bottom
                                      objArray.sort();
                                      
                                      for (i = 0; i < len; i++) {
                                          objArray[i].move(layPI[i], ElementPlacement.PLACEBEFORE);
                                          }  
                                      

                                       

                                      Have fun

                                       

                                      • 16. Re: Re: Re: How to sort objects alphabetically (inside of a layer)?
                                        captainfodder Level 1

                                        I showed this script to a co-worker and he provided me with some insight.

                                        "it looks to me like when sorting townList you end up losing your indexed association with the 'town' array. So you must store the index of the town along with the name of the town; then sort by the name while still keeping a hold of what index it is in `town`."


                                        He was able to give me a script back that accomplished what he recommended, but it still didn't quite work.

                                         

                                        We compared his script, that was based off yours, and then compared and stole things from the original script that was sorting the layers themselves (see original code above) but not the path items.

                                         

                                        We then put everything together and got it working with this!

                                         

                                        #target "Illustrator"
                                        
                                        
                                        function alphabetize()
                                        {
                                             if (app.documents.length == 0)
                                             {
                                                  alert("No document Open");
                                                  return;
                                             }
                                        
                                             var docRef = app.documents[0];
                                             var state = docRef.layers;
                                        
                                        
                                             for (a = 0; a < state.length; a++)
                                             {
                                                  var provider = state[a].layers;
                                                  for (b = 0; b < provider.length; b++)
                                                  { 
                                                       var product = provider[b].layers;
                                                       for (c = 0; c < product.length; c++)
                                                       {   
                                                            var town = product[c].pathItems;
                                                            if (town.length > 0)
                                                            {
                                                                 var townList = [];
                                        
                                                                 for (d = 0; d < town.length; d++)
                                                                 {  
                                                                      townList.push(town[d].name);
                                                                 }
                                                                 townList.sort();
                                                                 sort_layer(product[c], townList);
                                        
                                                            }
                                                       }
                                                  }
                                             } 
                                        }
                                        function sort_layer (obj, array) {
                                             for (var ri=0, riL=array.length; ri < riL ; ri++) {
                                             obj.pathItems.getByName(array[ri]).zOrder( ZOrderMethod.SENDTOBACK );
                                             };
                                        }
                                        alphabetize();
                                        
                                        
                                        
                                        • 17. Re: How to sort objects alphabetically (inside of a layer)?
                                          Jung K Kwon Level 1
                                          #target "Illustrator"
                                          
                                          
                                          var doc            = app.activeDocument;
                                          
                                          
                                          var Top_all_layers  = doc.layers;
                                          
                                          
                                          Arrange_Layers(doc, Top_all_layers) ;
                                          
                                          
                                          redraw();
                                          
                                          
                                          
                                          
                                          function Arrange_Layers(where, T_Layers_OBJ) {
                                          
                                          
                                              var temp_L_names = new Array();
                                             
                                              for ( var i = 0 ; i < T_Layers_OBJ.length ; i++ ) {
                                                  temp_L_names.push(T_Layers_OBJ[i].name);
                                              }
                                          
                                          
                                              sort_layer(where, temp_L_names.sort(naturalCompare)) ;;
                                          
                                          
                                              for ( var i = 0 ; i < T_Layers_OBJ.length ; i++ ) {
                                                  if ( T_Layers_OBJ[i].layers.length != 0 ) {
                                                      Arrange_Layers(T_Layers_OBJ[i], T_Layers_OBJ[i].layers) ;
                                                  }
                                              }
                                          
                                          
                                          return ;
                                          
                                          
                                          }
                                          
                                          
                                          function sort_layer(where, Layers_Array) {
                                             
                                              var temp_P_names  = Array() ;
                                              var Sorted_temp_P = Array() ;
                                             
                                              for (var k=0 ; k < Layers_Array.length ; k++) {
                                                  var temp_layer = where.layers.getByName(Layers_Array[k]) ;//
                                                  var status_Lock = temp_layer.locked;
                                                  var status_Preview = temp_layer.preview;
                                                  var status_visible = temp_layer.visible;
                                                 
                                                  temp_layer.locked  = false;
                                                  temp_layer.preview  = true;
                                                  temp_layer.visible  = true;
                                          
                                          
                                                  temp_layer.zOrder( ZOrderMethod.SENDTOBACK );
                                                 
                                                  temp_layer.locked  = status_Lock;
                                                  temp_layer.preview  = status_Preview;
                                                  temp_layer.visible  = status_visible;
                                              }
                                          
                                             
                                              for (var k=0 ; k < Layers_Array.length ; k++) {
                                                 
                                                  var temp_layer = where.layers.getByName(Layers_Array[k]) ;//
                                                  var temp_items = temp_layer.pageItems ;
                                                 
                                                  for (var l=0 ; l < temp_items.length ; l++) {
                                                      temp_P_names.push(temp_items[l].name) ;
                                                     
                                                  }
                                             
                                                  Sorted_temp_P = temp_P_names.sort(naturalCompare) ;
                                                 
                                                  try {
                                                      for (var l=0 ; l < Sorted_temp_P.length; l++){
                                                          $.writeln(Sorted_temp_P[l],"--", temp_items[l]) ;
                                                          var temp_OBJ = temp_layer.pageItems.getByName(Sorted_temp_P[l]) ;           
                                                          temp_OBJ.zOrder(ZOrderMethod.SENDTOBACK);
                                                          //
                                                      } //
                                                  }
                                                  catch (e)
                                                  {
                                                      //alert(e); 
                                                  }
                                                 
                                          
                                              }
                                             
                                          //
                                          }
                                          //
                                          //
                                          function naturalCompare(a, b) {
                                              var ax = [], bx = [];
                                          
                                          
                                              a.replace(/(\d+)|(\D+)/g, function(_, $1, $2) { ax.push([$1 || Infinity, $2 || ""]) });
                                              b.replace(/(\d+)|(\D+)/g, function(_, $1, $2) { bx.push([$1 || Infinity, $2 || ""]) });
                                             
                                              while(ax.length && bx.length) {
                                                  var an = ax.shift();
                                                  var bn = bx.shift();
                                                  var nn = (an[0] - bn[0]) || an[1].localeCompare(bn[1]);
                                                  if(nn) return nn;
                                              }
                                          
                                          
                                              return ax.length - bx.length;
                                          }
                                          
                                          1 person found this helpful
                                          • 18. Re: Re: Re: How to sort objects alphabetically (inside of a layer)?
                                            Aristarco Palacios Level 1

                                            Works great for me!

                                            Script debugging window keeps appearing though. Is it because of the catch(e)? Or did I name it wrong and it is a .jsx and not a .js?

                                            Thanks again!!

                                             

                                            EDIT: Never mind, I found it!