Skip to main content
Inspiring
April 14, 2011
Answered

[JS IDCS5] geometricBounds from XML to inline Frames

  • April 14, 2011
  • 1 reply
  • 1331 views

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

This topic has been closed for replies.
Correct answer John Hawkinson

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

1 reply

John Hawkinson
Inspiring
April 14, 2011

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.

stoereeeAuthor
Inspiring
April 15, 2011

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?

John Hawkinson
John HawkinsonCorrect answer
Inspiring
April 15, 2011

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