11 Replies Latest reply on Jun 19, 2017 9:11 AM by pixxxel schubser

    Incrementing textFrame creation inside a function

    wolfeEdition

      When I run my script it does exactly like I want... It adds a text frame with the "grid location" for the selected items. My only issue is that it isn't incrementing the text frames. I believe line 42 is the issue....because every time it runs my function it resets the xCoor, yCoor, and inc back to zero. Anyone have an idea of how i can make the text frames not stack on top of each other??

       

      #target illustrator
      function findGrid() {  
      var d = activeDocument,  
          ps = d.layers['42 Pg Grid Location'].pathItems,  
          l = ps.length,  
          o = 'ABCDEFGHIJKLMNOPQRST...',  
          W, H,  
          rows = columns = 1,  
          origin = d.rulerOrigin,  
          newOrigin = new Array(2),  
          t, b, center, gx, gy,  
          i = 0;  
        
      for (; i < l; i++) {  
          if (ps[i].width == 0) {  
              columns++;  
              !newOrigin[1] && newOrigin[1] = origin[1] + ps[i].geometricBounds[3];  
              !H && H = ps[i].height;  
          }  
          if (ps[i].height == 0) {  
              rows++;  
              !newOrigin[0] && newOrigin[0] = origin[0] + ps[i].geometricBounds[2];  
              !W && W = ps[i].width;  
          }  
      }  
        
      d.rulerOrigin = newOrigin;  
      W = W / columns;  
      H = H / rows;
      t = app.selection[z];  
      b = t.geometricBounds;  
      center = [b[0] + (b[2] - b[0]) / 2, b[1] + (b[3] - b[1]) / 2];  
      gx = Math.abs(center[0]) / W;  
      gy = Math.abs(center[1]) / H;  
        
      d.rulerOrigin = origin;  
      //alert('' + o[Math.ceil(gy - 1)] + Math.ceil(gx));
      xCoor = 0;
      yCoor = 0;
      inc = 0;
      createGridText = d.textFrames.add();
      createGridText.position = [xCoor, yCoor = (inc -= 20)];
      createGridText.contents = '' + o[Math.ceil(gy - 1)] + "-" + Math.ceil(gx);
      //alert(createGridText.contents);
      }
      
      
      for (var z = 0; z < selection.length; z++){
      findGrid();
      }
      

       

      Here is what the output looks like....

      Capture.JPG

      Here is my desired output....

      Capture2.JPG

      Any help would be appreciated!!

        • 1. Re: Incrementing textFrame creation inside a function
          williamadowling Level 4

          Change lines 40-42 from this:

          inc = 0; 

          createGridText = d.textFrames.add(); 

          createGridText.position = [xCoor, yCoor = (inc -= 20)];

           

           

          to this:

           

          inc = 20; 

          createGridText = d.textFrames.add(); 

          createGridText.position = [xCoor, (yCoor -= inc) ];

           

          • 2. Re: Incrementing textFrame creation inside a function
            Silly-V Adobe Community Professional

            You are never really doing the (inc -= 20) to create an offset from previous item because your inc = 0; is always defined in the function which is repeated in loop. So your inc is always at -20 for all the items. To decrement your inc, define it outside the function - I'd put it right above the loop which calls the function.

            1 person found this helpful
            • 3. Re: Incrementing textFrame creation inside a function
              wolfeEdition Level 1

              williamadowling  wrote

               

              Change lines 40-42 from this:

              inc = 0; 

              createGridText = d.textFrames.add(); 

              createGridText.position = [xCoor, yCoor = (inc -= 20)];

               

               

              to this:

               

              inc = 20; 

              createGridText = d.textFrames.add(); 

              createGridText.position = [xCoor, (yCoor -= inc) ];

               

              This still keeps everything stacked on top of each other.

              • 4. Re: Incrementing textFrame creation inside a function
                wolfeEdition Level 1

                Perfect! Moved inc =0; outside the function just like you recommended and it works perfectly!

                Here is the final code...

                #target illustrator
                function findGrid() {  
                var d = activeDocument,  
                    ps = d.layers['42 Pg Grid Location'].pathItems,  
                    l = ps.length,  
                    o = 'ABCDEFGHIJKLMNOPQRST...',  
                    W, H,  
                    rows = columns = 1,  
                    origin = d.rulerOrigin,  
                    newOrigin = new Array(2),  
                    t, b, center, gx, gy,  
                    i = 0;  
                  
                for (; i < l; i++) {  
                    if (ps[i].width == 0) {  
                        columns++;  
                        !newOrigin[1] && newOrigin[1] = origin[1] + ps[i].geometricBounds[3];  
                        !H && H = ps[i].height;  
                    }  
                    if (ps[i].height == 0) {  
                        rows++;  
                        !newOrigin[0] && newOrigin[0] = origin[0] + ps[i].geometricBounds[2];  
                        !W && W = ps[i].width;  
                    }  
                }  
                  
                d.rulerOrigin = newOrigin;  
                W = W / columns;  
                H = H / rows;
                t = app.selection[z];  
                b = t.geometricBounds;  
                center = [b[0] + (b[2] - b[0]) / 2, b[1] + (b[3] - b[1]) / 2];  
                gx = Math.abs(center[0]) / W;  
                gy = Math.abs(center[1]) / H;  
                  
                d.rulerOrigin = origin;  
                //alert('' + o[Math.ceil(gy - 1)] + Math.ceil(gx));
                xCoor = 0;
                yCoor = 0;
                createGridText = d.textFrames.add();
                createGridText.position = [xCoor, yCoor = (inc -= 20)];
                createGridText.contents = '' + o[Math.ceil(gy - 1)] + "-" + Math.ceil(gx);
                //alert(createGridText.contents);
                }
                inc = 0;
                for (var z = 0; z < selection.length; z++){
                findGrid();
                }
                
                • 5. Re: Incrementing textFrame creation inside a function
                  pixxxel schubser Level 5

                  Hi wolfeEdition,

                  that's strange. Your code does not work.

                   

                  Did you running the code from ESTK or directly from Illustrator? Is this all - or only a snippet from a larger script?

                   

                  Pasting the code in ESTK and select few paths which are located at layer named 42 Pg Grid Location and running your snippet gives  the first error in line #26 d.rulerOrigin = newOrigin;   --> Point value expected

                   

                  After changing line #10 in /*newOrigin = new Array(2),*/ newOrigin = new Array(0,0),
                  the script works. but all contents of every new text frames is: undefined-NaN

                  • 7. Re: Incrementing textFrame creation inside a function
                    wolfeEdition Level 1

                    I am running the script from ESTK. Sorry I pasted in a working version....this is my final code....

                    #target illustrator
                    function findGrid() {
                        var d = activeDocument,
                            ps = d.layers['42 Pg Grid Location'].pathItems,
                            l = ps.length,
                            o = 'ABCDEFGHIJKLMNOPQRST...',
                            W, H,
                            rows = columns = 1,
                            origin = d.rulerOrigin,
                            newOrigin = new Array(2),
                            t, b, center, gx, gy,
                            i = 0;
                    
                    
                        for (; i < l; i++) {
                            if (ps[i].width == 0) {
                                columns++;
                                !newOrigin[1] && newOrigin[1] = origin[1] + ps[i].geometricBounds[3];
                                !H && H = ps[i].height;
                            }
                            if (ps[i].height == 0) {
                                rows++;
                                !newOrigin[0] && newOrigin[0] = origin[0] + ps[i].geometricBounds[2];
                                !W && W = ps[i].width;
                            }
                        }
                    
                    
                        d.rulerOrigin = newOrigin;
                        W = W / columns;
                        H = H / rows;
                        t = app.selection[z];
                        b = t.geometricBounds;
                        center = [b[0] + (b[2] - b[0]) / 2, b[1] + (b[3] - b[1]) / 2];
                        gx = Math.abs(center[0]) / W;
                        gy = Math.abs(center[1]) / H;
                    
                    
                        d.rulerOrigin = origin;
                        xCoor = -50;
                        yCoor = 0;
                        createGridText = d.textFrames.add();
                        createGridText.position = [xCoor, yCoor = (inc -= 20)];
                        createGridText.contents = '' + o[Math.ceil(gy - 1)] + "-" + Math.ceil(gx);
                    } 
                    
                      inc = 0;
                      incTwo = 0;
                      d = activeDocument
                    for (var z = 0; z < selection.length; z++) {
                        xC = -350;
                        yC = 0;
                        createComponentText = d.textFrames.add();
                        createComponentText.position = [xC, yC = (incTwo -= 20)];
                        createComponentText.contents = selection[z].contents;
                        findGrid();
                    }
                    
                    • 8. Re: Incrementing textFrame creation inside a function
                      wolfeEdition Level 1

                      YES! moluapple I am building off of that code. I have 2 different accounts.... one is for work and one is for my personal stuff. I accidentally posted this under the wrong profile. Thanks so much for your help on the other post!

                      • 9. Re: Incrementing textFrame creation inside a function
                        wolfeEdition Level 1

                        Is there an easy way to get all of this to happen on a new document instead of the current active document? Meaning it gathers all the information from the current active document and then puts it all on a new document.

                        • 10. Re: Incrementing textFrame creation inside a function
                          Silly-V Adobe Community Professional

                          You mean save as a copy, then process?

                          • 11. Re: Incrementing textFrame creation inside a function
                            pixxxel schubser Level 5

                            wolfeEdition  schrieb

                             

                            I am running the script from ESTK. Sorry I pasted in a working version....this is my final code....

                             

                            Okay. I see. This snippet required the: Find Text Grid Location.pdf

                            In other documents the script cannot work.