Search code examples
phpadobeadobe-indesignextendscriptindesign-server

Adobe InDesign CS5 Server JavaScript: Get rectangle by XML tag


Current set-up:

Adobe InDesign Server CS5 scripted through ExtendScript via PHP SOAP

The problem:

I'm currently placing an image file into a rectangle using the following code:

 frame     =   doc.rectangles[0];
 imgList   =   frame.place(new File(img));

This works fine; the img file is placed into the rectangle as expected. However, this only refers to the first rectangle in the document: if I have two rectangles in the document, the image is placed into the last created rectangle.

What I'd ideally like to be able to refer to the rectangle by its XML tag - something like:

frame     =   doc.getRectangleByTag('Pic'); // <Pic> being the name of the XML tag
imgList   =   frame.place(new File(img));

Does anyone have any advice as to how this can be achieved? I realise this is rudimentary question, but am finding no joy after several hours of searching.

Many thanks


Solution

  • There isn't a 'getRectangleByTag' method that I know of. However, if you know the 'id' or 'name' property of the rectangle which you are looking for, you can loop through the rectangles in the document and finding it like this:

    var rectangles = doc.rectangles;
    var rectID; //the ID you're looking for
    var myRectangle;
    
    for(var i = 0; i < rectangles.length; i++){
       if(rectangles[i].id == rectID){
          myRectangle = rectangles[i];
       }
    }
    
    myRectangle.place(new File(img));
    

    Hope this helps!