4 Replies Latest reply on Jan 23, 2013 3:37 PM by ScriptingRules

    Center Artboard (CTRL+0)

    ScriptingRules

      There have been several discussions centered around moving artwork to the center of the artboard, but I havent found a discussion that shows how one scripts the centering of an artboard.

       

      I regularly save the documents after CTRL+0, but I am processing via script and need the same functionality.

       

      Any Ideas?

       

       

      function centerArtboard()
      {        
              var doc = app.activeDocument;

              ...

      }

        • 1. Re: Center Artboard (CTRL+0)
          CarlosCanto Adobe Community Professional & MVP

          Ctrl+0 is for Fit Artboard in Window, that will vary depending on your monitor size, you can check your own, it could be 110% or 104% or something else.

           

          to zoom 100% and center

           

          1. find out the center of the artboard (use artboardRect)

          2. move the view to the center of the artboard (use activeView.centerPoint = center of artboard)

          3. zoom to 100% (use activeView.zoom = 1;)

          • 2. Re: Center Artboard (CTRL+0)
            ScriptingRules Level 1

            CarlosCanto

             

            Thanks for your help.  I'm not sure how to deal with the monitor size and resolution, but I mapped the zoom levels on part heights from 1 to 18 inches (our basic parts) and plotted that in excel and came out with an equation for to calculate the zoom.

             

            I had a lot of trouble centering the document because the origin was never in the same place.  After setting that, this works fine.

             

            #target illustrator

            centerDocument()

            function centerDocument()
            {
                 var docRef = app.activeDocument
                 docRef.pageOrigin = [0,0];
                 docRef.rulerOrigin = [0,0];
                 var width=artboardRef[0].artboardRect[2]-artboardRef[0].artboardRect[0];
                 var height=artboardRef[0].artboardRect[1]-artboardRef[0].artboardRect[3];
                 docRef.activeView.centerPoint = [width/2,height/2]
                 var power = Math.pow((height/72),(-.962))
                 var zoom = docRef.activeView.zoom = 13.611*power
            }

            • 3. Re: Center Artboard (CTRL+0)
              CarlosCanto Adobe Community Professional & MVP

              that's cool, but that might work for your screen, for mine if I have a landscape document, hitting Ctrl+0 gives me 147% zoom, with your script I get 177%, if I have a document in portrait hitting Ctrl+0 gives me 110% zoom, with your script I get 134%.

               

              what makes it tricky is the monitor resolution, I'm not sure if we can get that in any way. You can get the monitor size using

               

              var scr = $.screens[0];
              

               

              but that's about as far as I have gone.

               

               

              by the way, I added this to your script it was undefined

              var artboardRef = docRef.artboards;
              
              • 4. Re: Center Artboard (CTRL+0)
                ScriptingRules Level 1

                CarlosCanto

                 

                Thanks for the correction above, I got carried away when I was cleaning up my mess.

                 

                I added an IF statement to allow for wider vs taller artboards.  It could be made to add the screen size if someone so desired.

                 

                #target illustrator

                centerDocument()

                function centerDocument()
                {
                     var docRef = app.activeDocument
                     var artboardRef = docRef.artboards;
                     docRef.pageOrigin = [0,0];
                     docRef.rulerOrigin = [0,0];
                     var width=artboardRef[0].artboardRect[2]-artboardRef[0].artboardRect[0];
                     var height=artboardRef[0].artboardRect[1]-artboardRef[0].artboardRect[3];
                     docRef.activeView.centerPoint = [width/2,height/2]
                     if(width > height)
                     {
                        var power = Math.pow((width/72),(-.962))
                        var zoom = docRef.activeView.zoom = 20*power
                     }else{
                        var power = Math.pow((height/72),(-.962))
                        var zoom = docRef.activeView.zoom = 13*power
                        }
                    
                }