6 Replies Latest reply on Jul 6, 2017 3:24 PM by Silly-V

    How can I loop through name objects?

    lan.mine

      I was wondering if there is a way to loop through specific name objects (thru-cut) one at a time? I like to apply action commands to the objects indivdually. Is this possible?

        • 1. Re: How can I loop through name objects?
          lan.mine Level 1

          This is what I have so far.

           

          #target illustrator
          
          function test(){
          
          
          
            function getPartialNamedItems(nameStr){
              var doc = app.activeDocument, thisItem, arr = [];
              for(var i=0; i<doc.pageItems.length; i++){
                thisItem = doc.pageItems[i];
                if(thisItem.name.match(nameStr)){
                  arr.push(thisItem);
                }
              };
              return arr;
            }
          
            var allItems = getPartialNamedItems ("thru-cut");
          
          app.doScript ('Artboards', 'Test');  
          
            //alert(allItems.length);
          };
          
          test();
          
          • 2. Re: How can I loop through name objects?
            Silly-V Adobe Community Professional

            now loop through your found items and perform an app.doScript on each of them after setting their .selected to true to select the items, and app.activeDocument.selection = null to deselect everything.

            • 3. Re: How can I loop through name objects?
              lan.mine Level 1

              Thanks for your help Silly-V. I realize that the method I was using was incorrect. I won't be able to create the artboards using an action and that portion will have to be scripted. Here is what I have so far. No errors, but it isn't creating new artboards to the "thru-cut" objects like I need it to. Any chance you can help me with this? Thanks.

               

              //get the total number of thru-cut in active document
              function CutThruItems(nameStr){
                  var doc = app.activeDocument, thisItem, arr = []
                  for(var i=0; i<doc.pageItems.length; i++){
                      thisItem = doc.pageItems[i];
                      if(thisItem.name.match(nameStr)){
                          arr.push(thisItem);
                          }
                      }
                  return arr;
                  }
              
              var allCuts = CutThruItems ("thru-cut");
              
              alert(allCuts.length);
              
              
               //looping on layers to create one artboard per layer      
                      for ( var i = 0 ; i < allCuts.length; i++)    
                      {      
                          var abIndex = i;  
                          
                          allCuts=true;
                          var sel = doc.selection
              
                          //Create the artboard around selected art    
                          doc.fitArtboardToSelectedArt(abIndex);    
              
                          artboards[abIndex].name = currentLayer.name;        
                              
                          //reset selection for next iteration    
                          doc.selection = null;      
                      }      
              
              • 4. Re: How can I loop through name objects?
                Silly-V Adobe Community Professional

                You have to do something like var newBoard = doc.artboards.add([0, 100, 200, -100]);

                • 5. Re: How can I loop through name objects?
                  lan.mine Level 1

                  Very close. I have the artboard showing but it is now its not resizing to the artwork.

                   

                   

                     

                  //////////////////////////////////////////////get the total number of thru-cut in active document

                  function CutThruItems(nameStr){

                      var doc = app.activeDocument, thisItem, arr = []

                      for(var i=0; i<doc.pageItems.length; i++){

                          thisItem = doc.pageItems[i];

                          if(thisItem.name.match(nameStr)){

                              arr.push(thisItem);

                              }

                          }

                      return arr;

                      }

                   

                  var allCuts = CutThruItems ("thru-cut");

                  alert(allCuts.length);

                   

                  //////////////////////////////////////////////Checkpoint of visible thru-cut starts here

                   

                          for ( var i = 0 ; i < CutThruItems("thru-cut").length; i++){

                             

                          var currentItem = CutThruItems("thru-cut");

                   

                   

                          //We don't want to deal with hidden layers

                          if(currentItem.visible == false) continue;

                   

                          //Unlock the layer if needed

                          currentItem.locked = false;

                   

                          //Select ALL in the layer

                          currentItem.hasSelectedArtwork = true;

                             

                  //////////////////////////////////////////////Artboard creation starts here

                         

                        if(i>0){ 

                          var newAb = artboards.add([0,100,200,-100]);   

                        } 

                        var thisLayer = CutThruItems("thru-cut")[i]; 

                        thisLayer.hasSelectedArtwork = true; 

                        doc.fitArtboardToSelectedArt(i); 

                        doc.selection = null; 

                      } 

                      //alert("Finished:\nRenaming of Artboards to match Layer names is complete"); 

                  • 6. Re: How can I loop through name objects?
                    Silly-V Adobe Community Professional

                    You don't have the artboards being used as a property of a document object, are they still working for you?
                    Well I think your problem is that you specify your 'thru-cut' art as layers, while they are not layers. Layers have hasSelectedArtwork property, while art - or the items inside of layers - have the .selected property. In your case you'd change your code to: thisLayer.selected = true;