10 Replies Latest reply on Sep 30, 2013 2:02 PM by CarlosCanto

    get artboard of selected item

    hilukasz Level 1

      I have script where I select items on multiple artboard but I can't seem to get get active art board of the selected item. Is this possible?

       

      each artboard has a text item and I would like them in the same position on each of their own artboards that they are already one. right now it adjusts them all to one artboard. here is what I have so far

       

      doc = app.activeDocument;
      for(i = 0; i < selection.length; i++){
          var firstItemPosition = doc.selection[0].position;
          doc.selection[i].position = firstItemPosition;
          var activeArtboardIndex = doc.artboards.getActiveArtboardIndex();
          alert(activeArtboardIndex);
       }
      
        • 1. Re: get artboard of selected item
          Gustavo Del Vechio Level 3

          Hi hilukasz

           

          Just to better understand your question:

           

          You have various artboards in your document. You have one text inside each artboard and you would like each text are in the same position (each in in it´s own artboard).

           

          Is this measurement based on the artboards bounds? For example, you would need each text to be in center or near the top of the their artboards?

           

          Or is this measurement based to one specific text? For example, if the text in artboard 1 is x=10 and y=20, you want all the other text align this way on its own artboard.

           

          Gustavo.

          • 2. Re: get artboard of selected item
            hilukasz Level 1

            yeah, sorry should have been more clear. I am just aligning them based on the position of the first selected items index. I am starting simple then will work from there. Rest is easy, hardest part is figuring out how to get the artboard of each selected item.

             

            I printed the object out for the selection and it seems to have no aweness of its active artboard, so I am not very hopeful:

             

            keys: 
            
            story : [Story]
            contents : asdfsadf
            textRange : [TextRange]
            textSelection : [TextRange]
            rowCount : 1
            columnCount : 1
            rowGutter : 0
            columnGutter : 0
            flowLinksHorizontally : true
            spacing : 0
            opticalAlignment : false
            kind : TextType.POINTTEXT
            contentVariable : undefined
            orientation : TextOrientation.HORIZONTAL
            
            • 3. Re: get artboard of selected item
              Muppet Mark Level 5

              In AI an item can be on no artboard, one artboard or multi-artboards ( nothing to stop overlapping )

              • 4. Re: get artboard of selected item
                Gustavo Del Vechio Level 3

                Besides Muppet´s mention, there´s no method or property (at least in manual) that returns the pageItems covered by an specific artboard. You can read the pageItems in a specific layer, group, but not for one artboard.

                 

                I was trying some tests....

                 

                We know, manually, if you select an element, Illustrator automatically switches the active artboard. But this does not happen via script. If you select an item, even try to copy and paste or whatever isolates it, Illustrator does not change the artboard. Looks the unique way to change the active artboard is really the method setActiveArtboardIndex();

                 

                I tried inserting the line "app.CoordinateSystem = CoordinateSystem.ARTBOARDCOORDINATESYSTEM;" to see if it makes any difference, but no sucess.

                 

                Honestlly do not know how to know which artboard an specific item is located. Perhaps anything mathematical that comparates position of each artboard with one specific item. But this would be a big problem if artboards overlap or the item takes more than one artboard.

                 

                Honestlly I do not know hehehe

                • 5. Re: get artboard of selected item
                  hilukasz Level 1

                  sure, but how do you get what artboard it is on? I think Gustavo might be closest to answer with having to calculate the boundries and see if its within them, but that seems a bit slow and might bog the script down if I have to do a lot of artboards/alignment.

                  • 6. Re: get artboard of selected item
                    Gustavo Del Vechio Level 3

                    It would be necessary to test it in order to know the time it takes to anaylises the artboards and contents to align, but I think the operation would not be so slow.

                     

                    Try to make a loop that reads each artboard´s position and compare with the position of the pageItem. Align by the artboard that matches or more approaches the position of the item.

                     

                    This is just an idea. I did not make any test to know if it would work hehe.

                     

                    Best Regards

                    Gustavo.

                    • 7. Re: get artboard of selected item
                      CarlosCanto Adobe Community Professional & MVP

                      ...or with a "brute force" approach

                       

                      // return each selected item's artboard index
                      // carlos canto 09/28/2013
                      // http://forums.adobe.com/message/5721205?tstart=0#5721205
                      
                      
                      var idoc = app.activeDocument;
                      app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;
                      var selec = idoc.selection;
                      
                      
                      var sel = [];
                      for(i = 0; i < selec.length; i++)
                          sel.push(selec[i]);
                      
                      
                      for(j = 0; j < sel.length; j++) {
                          idoc.selection = null;
                          alert('selection ' + j + objectArboardIindex (sel[j]));
                      }
                      
                      
                      for(j = 0; j < sel.length; j++) 
                          sel[j].selected = true;
                      
                      
                      app.userInteractionLevel = UserInteractionLevel.DISPLAYALERTS; 
                      
                      
                      function objectArboardIindex (obj) {
                          var idoc = app.activeDocument;
                          for(i=0; i<idoc.artboards.length; i++) {
                              idoc.artboards.setActiveArtboardIndex (i); // activate each artboard
                              idoc.selectObjectsOnActiveArtboard(); // select all in artboard
                              if (obj.selected) return ' is in Artboard ' + idoc.artboards.getActiveArtboardIndex();
                              idoc.selection = null;
                          }
                          return ' is outside artboard(s)';
                      }
                      
                      • 8. Re: get artboard of selected item
                        hilukasz Level 1

                        ah this works. Just curious, how come you disable display alerts ont he second line? I commented this out just to see what it would do, and it doesn't seem to make a difference.

                         

                        by the way, very clever way to check for artboard, never would have thought to use it this way!

                        • 9. Re: get artboard of selected item
                          hilukasz Level 1

                          hmm it seems it doesn't work the way that I thought it might. It still always positions items based on first artboard even when you set: 

                           

                          idoc.artboards.setActiveArtboardIndex(i)
                          

                           

                          I tried to implement it like this, any idea what I might be doing wrong?

                           

                          // return each selected item's artboard index
                          // carlos canto 09/28/2013
                          // http://forums.adobe.com/message/5721205?tstart=0#5721205
                          
                          var idoc = app.activeDocument;
                          app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;
                          var selec = idoc.selection;
                          var firstItemPosition = selec[0].position;
                          
                          
                          var sel = [];
                          for(i = 0; i < selec.length; i++)
                              sel.push(selec[i]);
                          
                          for(j = 0; j < sel.length; j++) {
                              idoc.selection = null;
                              alert('selection ' + j + objectArboardIindex (sel[j]));
                          }
                          
                          
                          for(j = 0; j < sel.length; j++) 
                              sel[j].selected = true;
                          
                          
                          app.userInteractionLevel = UserInteractionLevel.DISPLAYALERTS; 
                          
                          
                          function objectArboardIindex (obj) {
                              var idoc = app.activeDocument;
                              for(i=0; i<idoc.artboards.length; i++) {
                                  idoc.artboards.setActiveArtboardIndex(i); // activate each artboard
                                  idoc.selectObjectsOnActiveArtboard(); // select all in artboard
                                  if (obj.selected) {
                                      obj.position = firstItemPosition;
                                      return ' is in Artboard ' + idoc.artboards.getActiveArtboardIndex();
                                  }
                                  idoc.selection = null;
                              }
                              return ' is outside artboard(s)';
                          }
                          
                          • 10. Re: get artboard of selected item
                            CarlosCanto Adobe Community Professional & MVP

                            thanks

                            hilukasz wrote:

                             

                            ah this works. Just curious, how come you disable display alerts ont he second line? I commented this out just to see what it would do, and it doesn't seem to make a difference.

                            I just added it to see if it sped up the script, that line is used to suppress application alerts, it is not really helping in this case.

                             

                            I tried to implement it like this, any idea what I might be doing wrong?

                            change the coordinate system to Artboards (default is Document) before you access the position property

                             

                            app.coordinateSystem = CoordinateSystem.ARTBOARDCOORDINATESYSTEM;
                            var firstItemPosition = selec[0].position;