4 Replies Latest reply on Apr 9, 2012 7:35 PM by maxr2012

    script to calculate the hypotenuse of a page and place a picture box

    maxr2012

      Hi,

      I'm wondering if someone can help me with a script.

      It needs to get the size of an open document, calculate the hypotenuse (diagonal), divide this by 10 and place a square picture box on the page at that size in the top left corner.

      I found this javascript to calculate the hypotenuse but don't know how to use it or integrate it.

       

      function hypotenuse(a, b) {

         function square(x) { return x*x; }

        

         return Math.sqrt(square(a) + square(b));

      }

      function secondFunction(){

         var result;

         result = hypotenuse(1,2);

         alert( result );

      }

       

      Yes I'm a total newbie and would appreciate anyones help.

      Thanks,

      maxrus2012

        • 1. Re: script to calculate the hypotenuse of a page and place a picture box
          Marc Autret Level 4

          Hi maxrus,

           

          It sounds so simple that no one has found it necessary to answer you! Indeed, we can easily retrieve the height and the width of a page, then calculate the diagonal, then create a square based on that length.

           

          Anyway, let's try to make such routine work in any context, supporting any custom measurement units, rulers settings, rotated spreads and/or even scaled/skewed pages! To do so we need to avoid usual methods based on "geometric bounds". Interesting challenge!

           

          Here is my attempt:

           

          // Create a square based on the active page's diagonal length (10%)
          // ===========================
          
          function measureDiagonal(/*Page*/page)
          // -------------------------------------
          // Ret. the page's diagonal in pts (relative to the page CS)
          {
              var CS_INNER = CoordinateSpaces.innerCoordinates;
              
              var wh = page.resolve(AnchorPoint.bottomRightAnchor, CS_INNER)[0],
                  w = wh[0],
                  h = wh[1];
          
              return Math.sqrt(w*w + h*h); // Pythagorean theorem
          }
          
          function createTopLeftCornerRectangle(/*Page*/page, /*num[2]*/wh)
          // -------------------------------------
          // wh: width and height of the rectangle in pts (relative to the page CS)
          {
              // Some const shortcuts
              // ---
              var    CS_SPREAD = CoordinateSpaces.spreadCoordinates,
                  CS_INNER = CoordinateSpaces.innerCoordinates,
                  RM_REPLACE = ResizeMethods.replacingCurrentDimensionsWith,
                  AP_TOP_LEFT = AnchorPoint.topLeftAnchor;
          
              var spread = page.parent,
                  // Create a rectangle (in the spread CS--the page CS is not relevant yet)
                  // ---
                  rec = spread.rectangles.add({fillColor:'Black'}),
                  // Page transformation values (relative to the spread)
                  // ---
                  pageMxValues = page.transformValuesOf(CS_SPREAD)[0].matrixValues;
                  
          
              // Normalize the rectangle in the spread
              // (the size does not matter here)
              // ---
              rec.reframe(CS_SPREAD, [[0,0],[10,10]]);
              
              // Apply the page transfo to the rectangle
              // so that its inner space fits the page space
              // ---
              rec.transform(CS_SPREAD, [[0,0], CS_SPREAD], pageMxValues);
              
              // Finally, resize the rec
              // ---
              rec.resize(CS_INNER, AP_TOP_LEFT, RM_REPLACE, wh.concat(CS_INNER));
              
              return rec;
          }
          
          var    FACTOR = .1, // 10%
              win = app.layoutWindows.length && app.activeWindow,
              page = win && (win instanceof LayoutWindow) && win.activePage,
              size = page && page.isValid && FACTOR*measureDiagonal(page);
              
          
          size && createTopLeftCornerRectangle(page, [size,size]);
          

           

          Not sure this is exactly what you were looking after.

          Anyway...

           

          @+

          Marc

          • 2. Re: script to calculate the hypotenuse of a page and place a picture box
            John Hawkinson Level 5

            Anyway, let's try to make such routine work in any context, supporting any custom measurement units, rulers settings, rotated spreads and/or even scaled/skewed pages! To do so we need to avoid usual methods based on "geometric bounds". Interesting challenge!

            I think you might have scared him. Someone should demonstrate the easy way (I'll get to it...tomorrow?).

            • 3. Re: script to calculate the hypotenuse of a page and place a picture box
              maxr2012 Level 1

              Thanks Marc,

              This works great, it may be simple but I am still trying to get my head around it. I appreciate your quick response.

              Cheers,

              Max

              • 4. Re: script to calculate the hypotenuse of a page and place a picture box
                maxr2012 Level 1

                Hi John,

                I would love to see your spin on this as well if you have the time.

                Thanks,

                Max