4 Replies Latest reply on Jul 18, 2011 6:14 AM by tiekenz

    Simple Graphics Frame Resize Script

    tiekenz

      Hi, I have an ex-Pagemaker publication that I am looking at in Indesign, and I need to resize and move the three frames on each page.  Rather than doing all 125 pages manually I'm sure it's easy to script, but vbs and javascript scripting is completely new to me (I'm on Windows) and the samples I have looked at all look overly complex for what I want.

       

       

       

      At the most basic level, if someone could give me an example script showing how to set new Y, W and H coordinates (eg 27mm, 107mm and 107mm respectively) for a selected graphics frame and then fit the content proportionally, it would speed up my day immensely :)

       

       

       

      If I could get more automation that would be even better of course :) There's basically two graphics frames and one text frame on each page.

      The graphics frame with an initial Y value of 32.877 mm needs to be given new Y, W and H values of  27mm, 107mm and 107mm;

      The text frame with an initial Y value of 129.783 mm needs to be given a new Y value of 136mm, and the second graphics frame, with an initial Y value of 145.287mm needs to be given  new Y, W and H values of 146mm, 100mm and 19mm.

       

       

      I normally script a macro in an external macro program such as AutoHotKey or Tasker, but for some reason I can't get them to work reliably with InDesign, so am trying to learn the "correct" way.

       

      Thanks,

      Tieke

        • 1. Re: Simple Graphics Frame Resize Script
          John Hawkinson Level 5

          Hi, Tieke.

           

          This is all pretty simple stuff. Have you checked out the documentation?

           

          As it looks like you surmise, the best way to script this doesn't involve using the selection. But we can start that way,

          if you like. I'll abstract it out so it's easy to replace the selection (g1, in this example) with something else.

           

          At the most basic level, if someone could give me an example script showing how to set new Y, W and H coordinates (eg 27mm, 107mm and 107mm respectively) for a selected graphics frame and then fit the content proportionally,.
          var
            g1=app.selection[0],
            bounds, savedUnits, width, height;
          
          savedUnits = app.scriptPreferences.measurementUnit;
          try {
            app.scriptPreferences.measurementUnit = MeasurementUnits.MILLIMETERS;
            bounds = g1.geometricBounds; // y1, x1, y2, x2
            // Set y/width/height to 27/107/107 mm
            bounds[0] = 27; // y1
            bounds[2] = 27+107; // y2
            bounds[3] = bounds[1]+107; // x2
            g1.geometricBounds = bounds;
          } finally {
            app.scriptPreference.measurementUnit = savedUnits;
          }
          g1.fit(FitOptions.PROPORTIONALLY);
          

           

          That's a bit trickier than it may need to be because the script wants to account for the case where the document's measurement units aren't in millimeters, and then sets them back at the end. If your document is in millimeters already, then the savedUnits/try/finally stuff is superfluous. that's untested, by the way.

           

          If I could get more automation that would be even better of course There's basically two graphics frames and one text frame on each page.

          The graphics frame with an initial Y value of 32.877 mm needs to be given new Y, W and H values of  27mm, 107mm and 107mm;

          The text frame with an initial Y value of 129.783 mm needs to be given a new Y value of 136mm, and the second graphics frame, with an initial Y value of 145.287mm needs to be given  new Y, W and H values of 146mm, 100mm and 19mm.

           

          Well, usually the best approach doesn't involve searching out frames by their coordinates...it's better if there is a script label applied to the frame, or something else. But life isn't always like that. You could do something like this:

           

          var
            d=app.activeDocument,
            i, j,
            margin=1.0, // one millimeter
            page, frame, g1;
          for (i=0; i<d.pages.length; i++) {
            page = d.pages[i];
            for (j=0; j<page.rectangles.length; j++) {
              frame=page.rectangles[j];
              if (Math.abs(frame.geometricBounds[0] - 32.877) < margin) {
                g1 = frame;
              }
            }
            // write to the ESTK's console:
            $.writeln("Found frame g1 at ["+g1.geometricBounds.join(",")+"] (y1/x1/y2/x2)");
            // if you used alert("foo") it will pop up on the screen, often very annoying
            // ok, do whatever you wanted to with g1
          }
          

           

          Also untested. Merging the two is left as an exercise to the reader.

          note that it's importnat to not try to test the y1 coordinate of the frame exactly, because internal units are stored in points and there is always the potential for round-off errors and it's always a bad idea to test two floating point numbers for equality. So decide how close they should be, subtract them and take the absolute value and see if it is within your margin (in this case 1.0 millimeters).

           

          I didn't bother with the unit-saving the second time, but you should.

          1 person found this helpful
          • 2. Re: Simple Graphics Frame Resize Script
            tiekenz Level 1

            Thanks for that - definitely set me on the right course - obviously I shouldn't start looking at a new scripting language after midnight .

            Had problems with the automated script version in that it would pop up with error 21 (undefined is not an object) when it got to the $.writeln line, but am going to take it slowly and figure out a method (and apply script labels as part of the script so any future changes can be simpler).

             

            thanks,

            Tieke

            • 3. Re: Simple Graphics Frame Resize Script
              John Hawkinson Level 5

              Well, I did say it was untested, but I would think that error would

              arise if g1 was undefined, that is, it never found a frame matching

              your first graphic frame.

               

              The thing to do is run it from the ESTK and when there is an error,

              check the values of variables, either in the Data Browser or by

              typing expressions into the JavaScript Console.

              • 4. Re: Simple Graphics Frame Resize Script
                tiekenz Level 1

                Thanks for your help.