6 Replies Latest reply on Apr 15, 2011 2:39 AM by stoereee

    [JS IDCS5] geometricBounds from XML to inline Frames

    stoereee Level 1

      The following script is working on  InDesign Desktop. It's reading the imported XML to apply the attributes  values to the inline graphics as scale, geometricBounds and offset:

       

      // #include "glue code.jsx"

      main(myDocument);

      function main(){

      var myRuleSet = new Array (
      new findObjAttribute("//image")
      );
      with(myDocument){
      var elements = xmlElements;
      __processRuleSet(elements.item(0), myRuleSet);
      }

      function findObjAttribute(XPATH){
      this.name = "findObjAttribute";
      this.xpath = XPATH;
      this.apply = function(myElement, myRuleProcessor)
      {

      var myHeigth=myElement.xmlAttributes.itemByName("height").value;
      var myWidth=myElement.xmlAttributes.itemByName("width").value;
      var myScale=parseInt(myElement.xmlAttributes.itemByName("scale").value);
      var myXoffset=myElement.xmlAttributes.itemByName("xoffset").value;
      var myYoffset=myElement.xmlAttributes.itemByName("yoffset").value;

      with(myHeigth, myWidth, myScale, myYoffset, myXoffset){

      var myRectangle = myElement.xmlContent.parent;

      try {

      myRectangle.graphics[0].horizontalScale = myScale;
      myRectangle.graphics[0].verticalScale = myScale;
      myRectangle.graphics[0].move(undefined, [myYoffset, myXoffset]);
      myRectangle.geometricBounds = [0, 0, myHeigth, myWidth];

           } catch(e){};
      }
      return true;
      }
      }
      }

       

      When  running it on InDesign Server, the geometricBouns only applies to the  first graphic in the document. The scales and the offsets are applied to  all the graphics.

       

      Here is an example of the XML:

       

      <Root>
      <Paragraph>
      <image href="image1.jpg" scale="110" xoffset="-10mm" yoffset="10mm" width="87mm" height="60mm"></image>
      </Paragraph>
      <Paragraph>
      <image href="image2.jpg" scale="90" xoffset="20mm" yoffset="0mm" width="87mm" height="50mm"></image>
      </Paragraph>
      <Paragraph>
      <image href="image3.jpg" scale="100" xoffset="-15mm" yoffset="5mm" width="87mm" height="80mm"></image>
      </Paragraph>
      </Root>

       

      Why are the geometricBounds values only applying to the first graphic with InDesign Server?

       

      Regards, Sjoerd

        • 1. Re: [JS IDCS5] geometricBounds from XML to inline Frames
          John Hawkinson Level 5

          This construction:

          try {

          myRectangle.graphics[0].horizontalScale = myScale;
          myRectangle.graphics[0].verticalScale = myScale;
          myRectangle.graphics[0].move(undefined, [myYoffset, myXoffset]);
          myRectangle.geometricBounds = [0, 0, myHeigth, myWidth];

               } catch(e){};
          }

          is quite dangerous; suppose there is an error from setting myRectangle.geometricBounds? You will never see it and you will never know what it is.

          While we sometimes write "try { ... } catch(e) { };" in throwaway scripts, you should never use it either in production scripts or if you're trying to debug something and wonder why it is failing.

           

          It seems pretty likely that there's an error being thrown there -- you should probably see what it is.

           

          A reasonable debugging substitute would be "try { ... } catch(e) { $.writeln("Caught "+e.message+" at line "+e.line); }"

           

          In production code, it would be wiser to catch only the specific exception you really care about:

           

          "try { ... } catch(e) { if (e!==TypeError) { throw e; } ... }"

           

          etc. Replace TypeError with the actual exception being thrown, of course.

          1 person found this helpful
          • 2. Re: [JS IDCS5] geometricBounds from XML to inline Frames
            stoereee Level 1

            This error is showing up two times:

             

            [script] Caught Invalid object for this request at line 345 [3500] [18383]

             

            Line 345 is:

             

            myRectangle.geometricBounds = [0, 0, myHeight, myWidth];
            

             

            The document has got three inline graphics and only at the first the geometricBounds are applied. For the next two it's generating the above error. Why?

            • 3. Re: [JS IDCS5] geometricBounds from XML to inline Frames
              John Hawkinson Level 5

              Well, you'll need to inspect the properties of the graphics that it doesn't work on. Doubtless there's something there...

              • 4. Re: [JS IDCS5] geometricBounds from XML to inline Frames
                John Hawkinson Level 5

                Also, the problem isn't that you're confusing myHeigth and myHeight, is it? The latter is the correct spelling...

                • 5. Re: [JS IDCS5] geometricBounds from XML to inline Frames
                  stoereee Level 1
                  Also, the problem isn't that you're confusing myHeigth and myHeight, is it? The latter is the correct spelling...

                   

                  The script spelled right, so that's not the problem.

                   

                  Well, you'll need to inspect the properties of the graphics that it doesn't work on. Doubtless there's something there...


                  I copied the first graphic and paste it two times and removed the others. Now it is working! There has to be something with the last two graphics indeed. Though it's weird, because all the images were imported with XML by href. But now I know where I have to find the problem, thank you.

                  • 6. Re: [JS IDCS5] geometricBounds from XML to inline Frames
                    stoereee Level 1

                    There's was something else in the script that stopped this.