10 Replies Latest reply on Sep 19, 2018 2:44 AM by JustOneBren

    Zoom to the selection

    JustOneBren Level 1

      Hi Scripters,


      I have been looking for a way to javascript the zoom to fit the selected objects into the window, I have looked through the zoom styles but cannot find one that does this. I have tried the "Fit Selection in Window" menu item in InDesign but this zooms out a lot further than the selection so is no good for what I need.


      Does anyone have a one liner solution or can point me in the direction of a script that does this that I can edit to fit my needs?


      Many Thanks in advance, Bren

        • 1. Re: Zoom to the selection
          Pickory Level 4

          Fiddle with the zoomPercentage.


            app.activeDocument.layoutWindows[0].zoomPercentage = app.activeDocument.layoutWindows[0].zoomPercentage;

          • 2. Re: Zoom to the selection
            JustOneBren Level 1

            Thanks for the reply,

            Unfortunately this does a fixed percentage zoom and doesn't take into account the size of my current selection which changes every time the script is run.


            • 3. Re: Zoom to the selection
              winterm Level 5

              Well, I could imagine something working better than this, but didn't find yet…


              1 person found this helpful
              • 4. Re: Zoom to the selection
                JustOneBren Level 1


                It seems to zoom right in on the back-most object and de-select all the others but it gives me something to work from, I will have a go at altering this and come back with any results.

                Thank you

                • 5. Re: Zoom to the selection
                  Michel [FRIdNGE] Level 4



                  Some news! …



                  Michel, for FRIdNGE

                  • 6. Re: Zoom to the selection
                    JustOneBren Level 1

                    Hi Michel,


                    I have figured out how to do this for my needs but unfortunately I was not able to come up with a script that would work for the masses.

                    I work on catalogues that the finished page is 269mm high, I expand the page to 1269mm, add a second page then add product and wanted to zoom to fit the product.

                    Below is how I have achieved this but it's not a script that will be that useful to most, unfortunetely I couldn't get anything from wintern's link to work as I required.



                        // Zoom view to fit all on spread

                            pageHeight = app.activeDocument.documentPreferences.properties.pageHeight // Find current page height and set up variable


                            app.menuActions.itemByName("$ID/Group").invoke(); // Run Menu Command "Group"


                            var selecCopy = app.selection[0].geometricBounds; // Read group size

                                  selecWidth = selecCopy[3] - selecCopy[1];

                                  selecHeight = selecCopy[2] - selecCopy[0];


                            app.activeDocument.align(app.activeDocument.selection, AlignOptions.VERTICAL_CENTERS, AlignDistributeBounds.PAGE_BOUNDS); // Centre selection vertically so zoom works


                            if (selecHeight <= 269) {selecHeight = 269} // If selection height is smaller than the finished page size, make the page the zoom size 269mm


                            app.activeDocument.documentPreferences.properties = {pageHeight: selecHeight} // Alter page height


                            app.layoutWindows[0].zoom(ZoomOptions.FIT_SPREAD) // Zoom: Fit to spread


                            app.menuActions.itemByName("$ID/Ungroup").invoke(); // Run Menu Command


                            if (selecHeight != 269) {app.activeDocument.documentPreferences.properties = {pageHeight: pageHeight}} // Unless page is 269mm high, revert to previous height

                    • 7. Re: Zoom to the selection
                      Michel [FRIdNGE] Level 4



                      If well understood, you just want to max-zoom on the items selection! …


                      Capture d’écran 2018-09-18 à 22.22.04.png


                      So … several or just one item!


                      Capture d’écran 2018-09-18 à 22.22.21.png


                      Capture d’écran 2018-09-18 à 22.22.40.png


                      Capture d’écran 2018-09-18 à 22.27.29.png


                      Capture d’écran 2018-09-18 à 22.27.47.png




                      Michel, for FRIdNGE

                      • 8. Re: Zoom to the selection
                        Laubender Adobe Community Professional & MVP

                        Hi Michel,

                        since you did not post any code, I can only assume that you somehow are working with property bounds of layoutWindow.

                        But this will not give you the right numbers in a case like that:




                        layoutWindow bounds are discussed e.g. here:

                        window.bounds gives weird results


                        Oh, and this one could be interesting as well:

                        Moving and Sizing the activeDocument Window?


                        So I fear that layoutWindow.zoom( ZoomOptions.FIT_PAGE ) is the only choice to get a good-enough value for layoutWindow.zoomPercentage. Of course that would leave a small gap between the layoutWindow's inner border and the selected objects. Just like the little gap you see between the page edges and the layoutWindow in the screenshot above.


                        I'm testing the following algorithm and it seems to work good-enough for me:


                        1. Calculate the visible width and height of your selected object(s)

                        2. Add a page to the document temporarily, set all margins of that page to zero.

                        3. Change the size of that page to width and height of your selection

                        4. Make the spread of the added and resized page the active one

                        5. Do doc.layoutWindows[0].zoom( ZoomOptions.FIT_PAGE )

                        6. Read out value of doc.layoutWindows[0].zoomPercentage

                        7. Make the spread of your selection the active spread


                        In case you lost your selection in the process of 1 to 7:

                        8. Do doc.layoutWindows[0].zoom( ZoomOptions.FIT_SPREAD )

                        9. Select your objects

                        10. Do doc.layoutWindows[0].zoomPercentage = value where value is the one from step 6.


                        11. Remove the added page from step 2 and perhaps a group from step 1.



                        If you managed to fit the selection without using bounds of layoutWindow then I suggest you post some code.



                        • 9. Re: Zoom to the selection
                          Laubender Adobe Community Professional & MVP

                          A note on the value of property zoomPercentage:


                          It seems that there is a restriction to the maximum value you can do by scripting that differs from the maximum value you can do with the UI. For my screen on my machine this maximum value is 3000 %. With the UI one could zoom in to 4000 %. Value 3000 correspond to the value I see if I create a document with a tiny page size like 1 mm x 1 mm and do keyboard shortcut Ctrl 0 ( Windows ) or Cmd 0 ( Mac ) to fit the page to the layout window.


                          And of course there is a minimum value for page size that is 0.353 mm x 0.353 mm and a maximum value of 5486.4 mm x 5486.4 mm.



                          • 10. Re: Zoom to the selection
                            JustOneBren Level 1

                            Hi Michel,

                            Looks like that's what I was originally looking for, mine works fine for me but it would be handy if you would post the code for yours, may help others looking for the same thing.

                            Thanks, Bren