5 Replies Latest reply on Sep 11, 2016 9:19 AM by pixxxel schubser

    “Fit All in Window” using JavaScript

    akinuri Level 1

      I've recently started scripting in Illustrator and having a little problem. When creating a document through File tab, (File->New, Print, Letter) the created document is positioned at (0,0) and the view is focused on the document.

       

      But if I create a document using JS, for example app.documents.add();, the created document isn't positioned at (0,0), but at (0,-792). I reposition the artboard and move it to (0,0) with app.activeDocument.artboards[0].artboardRect = [0, 0, 612, -792];. This moves the artboard below, out of the focused view. I need to scroll down to get the artboard into view.

       

      Is there a way to focus the view on the artboard using JS? Maybe call the "Fit All in Window" command?

       

      Using Adobe Illustrator CS6 64-bit on Windows 8.1 Pro

        • 1. Re: “Fit All in Window” using JavaScript
          Silly-V Adobe Community Professional

          It's great that you have CS6, because the following command app.executeMenuCommand('fitin') - I remember this Carlos answer from a long time ago!

          1 person found this helpful
          • 2. Re: “Fit All in Window” using JavaScript
            akinuri Level 1

            I'm glad there's a built-in way to do this. I was already half way to creating a JS version of "Fit All in Window". Though I'm gonna finish it anyway.

             

            "fitin" seems to perform "Fit Artboard in Window". What I needed was "fitall" which performs "Fit All in Window".

             

            I've looked into the scripting PDFs and I see no mention of app.executeMenuCommand(). Why is that? I'd like to know more about it, and possibly get a list of commands that I can use.

             

            Also, why is it great that I'm using CS6? Is it because app.executeMenuCommand() isn't available for earlier versions? If app.executeMenuCommand() available for all versions, writing a JS version of it seems a bit redundant.

            • 3. Re: “Fit All in Window” using JavaScript
              pixxxel schubser Level 5

              Hi akinuri,

              try this

              app.executeMenuCommand ('fitall');
              

               

              app.executeMenuCommand will only works in versions CS6+

              And here is a little "Forum documentation" --> [JS] CS6+   executeMenuCommand

               

              Have fun

               

              • 4. Re: “Fit All in Window” using JavaScript
                akinuri Level 1

                I've created a JS version of the "Fit All in Window" anyway Perhaps, someone will find it useful. It works in CS6 64-bit. Didn't test other versions.

                 

                "Fit All in Window" command for Illustrator in JavaScript [Gist]

                 

                function calcRect(rectArray) {
                    var rect = {
                        rect    : rectArray.join(", "),
                        topLeft    : {
                            x : rectArray[0],
                            y : rectArray[1],
                        },
                        bottomRight    : {
                            x : rectArray[2],
                            y : rectArray[3],
                        },
                    };
                    rect.width  = Math.abs(rect.bottomRight.x - rect.topLeft.x);
                    rect.height = Math.abs(rect.bottomRight.y - rect.topLeft.y);
                    rect.center = [
                        rect.topLeft.x + (rect.width / 2),
                        rect.topLeft.y - (rect.height / 2)
                    ];
                    rect.aspectRatio = rect.width / rect.height;
                    return rect;
                }
                
                
                function calcViewRect(rectArray, zoom) {
                    var rect = {
                        rect    : rectArray.join(", "),
                        topLeft    : {
                            x : rectArray[0],
                            y : rectArray[1],
                        },
                        bottomRight    : {
                            x : rectArray[2],
                            y : rectArray[3],
                        },
                    };
                    rect.width        = Math.abs(rect.bottomRight.x - rect.topLeft.x);
                    rect.height       = Math.abs(rect.bottomRight.y - rect.topLeft.y);
                    rect.zoom         = zoom || 1;
                    rect.actualWidth  = rect.width * zoom;
                    rect.actualHeight = rect.height * zoom;
                    rect.aspectRatio  = rect.width / rect.height;
                    return rect;
                }
                
                
                function getDocumentBounds(artboards) {
                    var rect = {
                        topLeft : {
                            x : null,
                            y : null,
                        },
                        bottomRight : {
                            x : null,
                            y : null,
                        },
                    };
                    
                    for (var i = 0; i < artboards.length; i++) {
                        var artboardRect = calcRect(artboards[i].artboardRect);
                        
                        if (rect.topLeft.x == null) {
                            rect.topLeft.x = artboardRect.topLeft.x;
                        } else {
                            if (artboardRect.topLeft.x < rect.topLeft.x) {
                                rect.topLeft.x = artboardRect.topLeft.x;
                            }
                        }
                        
                        if (rect.topLeft.y == null) {
                            rect.topLeft.y = artboardRect.topLeft.y;
                        } else {
                            if (artboardRect.topLeft.y > rect.topLeft.y) {
                                rect.topLeft.y = artboardRect.topLeft.y;
                            }
                        }
                                
                        if (rect.bottomRight.x == null) {
                            rect.bottomRight.x = artboardRect.bottomRight.x;
                        } else {
                            if (artboardRect.bottomRight.x > rect.bottomRight.x) {
                                rect.bottomRight.x = artboardRect.bottomRight.x;
                            }
                        }
                        
                        if (rect.bottomRight.y == null) {
                            rect.bottomRight.y = artboardRect.bottomRight.y;
                        } else {
                            if (artboardRect.bottomRight.y < rect.bottomRight.y) {
                                rect.bottomRight.y = artboardRect.bottomRight.y;
                            }
                        }
                    }
                    rect.rect = [rect.topLeft.x, rect.topLeft.y, rect.bottomRight.x, rect.bottomRight.y];
                    return rect;
                }
                
                
                function calcZoom(viewRect, documentRect, margin) {
                    if (documentRect.aspectRatio > viewRect.aspectRatio) {
                        return parseFloat(((viewRect.actualWidth - (2 * margin)) / documentRect.width).toFixed(2));
                    } else {
                        return parseFloat(((viewRect.actualHeight - (2 * margin)) / documentRect.height).toFixed(2));
                    }
                }
                
                
                function fitAll(document) {
                    var view         = document.views[0];
                    
                    var viewRect     = calcViewRect(view.bounds, view.zoom);
                    var documentRect = calcRect(getDocumentBounds(document.artboards).rect);
                
                
                    view.centerPoint = documentRect.center;
                    view.zoom        = calcZoom(viewRect, documentRect, 20);
                }
                
                
                // fitAll(app.activeDocument);
                
                1 person found this helpful
                • 5. Re: “Fit All in Window” using JavaScript
                  pixxxel schubser Level 5

                  I did not tested your code yet. (Because of I don't need zoom functions)

                  But it seems to be a good job.

                   

                   

                  And perhaps you can do something interesting with this snippet:

                  var myView=app.activeDocument.views[0];
                  myView.centerPoint = [(app.activeDocument.width/2),(app.activeDocument.height/-2)];
                  myView.zoom = 1.4;   //entspricht 140%
                  myView.screenMode.FULLSCREEN;
                  

                   

                  Have fun (and always fun with coding)