5 Replies Latest reply on Jan 26, 2009 12:18 AM by (Peter_Bornhall)

    Changing script from CS2 to CS4

      Hi everyone!

      Having a bit of a problem (not to mention working against a deadline) with an old CS2 script created for a client. ESTK throws up an "Undefined is not an object" error.

      Below is the code that seems to be the culprit. ESTK stops on the line after the .place(), assuming here that it's because the .place() doesn't return a proper object or something like that (I hate when they change scripting around too much).

      Basically, what it does is placing an image into the first paragraph of a table cell. It then tries to set the dimensions to a "maximum" size and then fits the image proportionally to those dimensions.

      > tmpBild = "B0000843.jpg"; //temporary for testing
      newTable.rows[newTable.bodyRowCount-1].cells[4].contents = "\r";
      tmpPara = newTable.rows[newTable.bodyRowCount-1].cells[4].paragraphs.item(0);
      tmpPara.justification = Justification.centerAlign;
      tmpImg = tmpPara.place( PICBASE + tmpBild );
      tmpImg.parent.visibleBounds = ["0mm","0mm","38.236mm","38.236mm"];
      tmpImg.parent.geometricBounds = ["0mm","0mm","38.236mm","38.236mm"];
      tmpImg.fit( FitOptions.proportionally );
      tmpImg.parent.fit( FitOptions.frameToContent );
      tmpImg.parent.strokeColor = "Paper";
      tmpImg.parent.strokeAlignment = StrokeAlignment.centerAlignment;
      tmpImg.parent.strokeWeight = "5pt";
      tmpImg.parent.strokeType = app.strokeStyles.item("Thick - Thin");

      Can anybody help convert this so it works for InDesign CS4? In the meantime, I will try to take care of the rest of the script.

      Btw, I have/had a scripting
      b reference
      in PDF format for InDesign CS2, is there anywhere that I can download that for CS4, I know it was very helpful back then.
        • 1. Re: Changing script from CS2 to CS4
          Level 1
          I have worked a bit more on this, and have new things to ask :)

          I think I have solved the actual image placing, but I get errors when things run outside the text box it seems.

          The script creates a new empty page, and on that page it creates a textbox containing a table. The table is then filled automatically from outside data, and when necessary it is supposed to place an image into a table cell.

          This works on the first page, UNTIL you get to a cell that is outside of the textbox, i.e. the table can't be fitted inside the textbox on the single page.

          I also tried this with simple rectangles that is the correct size from the start, but get the same error. This is the code used:

          > newTable.rows[newTable.bodyRowCount-1].cells[4].contents = "\r";

          > var myPara = newTable.rows[newTable.bodyRowCount-1].cells[4].paragraphs.item(0);

          > var myRect = myPara.rectangles.add({geometricBounds:["0mm","0mm","38.236mm","38.236mm"]});;

          ...which fails on the third row (var myRect) when the cell is outside of the textbox with "Invalid object for this request" in ESTK.

          What gives here? Is there something I'm doing wrong here, or is there some workaround to use? One possibility would be to somehow check if the textbox is overflowing, and if it is, create a new page and continue the textbox there.

          As before, any help here is appreciated. Basically, I need to be able to place an image proportionally into a table cell, keeping it within a set of maximum dimensions.
          • 2. Re: Changing script from CS2 to CS4
            Hi Peter,

            Have you had any more success with this? I can not get you code to work at all even if the bounds of the rectangle fit in the Cell?

            If anyone else knows how to simply place an image in a cell then an example script would be greatly appreciated!!
            • 3. Re: Changing script from CS2 to CS4
              Level 1
              What I ended up doing was create roughly enough pages in the document and create linked text frames on all pages, thus letting the table created in the first text frame flow as long as the document has enough pages. In the end processing, I then remove all the pages but the first one, effectively getting one page with one text frame containing all table contents.

              This was the only way I could find to get around this problem. No matter what I did, placing content into a table cell that was overflowing a text frame just didn't work.

              Here's the part where I create the pages (variable myMaxPages contains your estimated maximum number of pages). Please note in app.documents.add() that it is set to false to NOT open a window (speeds it up very slightly if you have a huge number of pages). You can remove the false and the myDocument.windows.add() statement if you don't want this or for debug purposes. The formatting mechanism on these forums seem broken, but the following code should be ok/readable once properly formatted.

              >myDocument = app.documents.add( false );
              with(myDocument) {
              for( i = 0; i < myMaxPages; i++ ) {
              myPage = pages.item(i);
              with(myPage) {
              if( i > 0 ) { myPrevFrame = myTextFrame; }
              myTextFrame = textFrames.add();
              with( myTextFrame ) {
              if( i == 0 ) {
              myTable = tables.add();
              with( myTable ) {
              bodyRowCount = 1;
              columnCount = 5;
              } else {
              if( i > 0 ) { previousTextFrame = myPrevFrame; }
              if( i < myMaxPages - 1 ) {

              While fiddling with it, I also updated my image placement code a bit:

              > var myCell = myTable.rows[myTable.bodyRowCount-1].cells[4];
              myCell.contents = "\r";
              myCell.paragraphs.item(0).justification = Justification.centerAlign;
              var myRect = myCell.insertionPoints[0].textFrames.add();
              myRect.contentType = ContentType.unassigned;
              myRect.visibleBounds = ["0mm","0mm","38.236mm","38.236mm"];
              myRect.geometricBounds = ["0mm","0mm","38.236mm","38.236mm"];
              var myFile = File(PICBASE + tmpBild);
              if( myFile.exists ) {
              var myImage = myRect.place( myFile );
              myRect.fit( FitOptions.proportionally );
              myRect.fit( FitOptions.frameToContent );

              Maybe this helps someone, maybe it doesn't. Can't say that I'm happy with the behaviour of getting errors and being unable to place content in table cells that is overflowing out of a text frame, but I needed to solve it, and this is how I did it.

              Of course, if anyone else has a better solution to placing images into table cells that may be in the overflowing part of a text frame, go ahead and share with the rest of us, please.
              • 4. Re: Changing script from CS2 to CS4
                Hi Peter,

                I had exactly the same issue as you, but I implemented a workaround by making the cell larger before doing the place(). In my situation, I know that the image that will be placed in the cell will have a height < 500 pixels. Once placed, I reset the cell height to it's original value or to the rectangle's height (if the rectangle would not fit in the original cell).

                • 5. Re: Changing script from CS2 to CS4
                  Level 1
                  Hi Chris,

                  Did that work even on a table cell that was overflowing out of the text box? I never actually tried that approach (I think), but if it works it's a good solution. As it ended up, I did another workaround by creating like 20 extra pages in the document and created linked textboxes across those 20 pages so the table had somewhere to overflow into. That worked well, and it's currently the solution I'm using. I'm now at the end of the project (for this year) though, so I won't change anything.

                  Besides, for my project, I have no idea on how big the images to be placed are. They can be any size that a digital camera can produce, so dimensions of 4000x3000 pixels aren't impossible :)

                  For the curious; the project/script is for a plant catalogue. It reads the data (including the filenames of the images) from a text file (tab-separated thingy). Or rather, the text file is actually placed into an InDesign document as a table, and the script reads from that table and outputs to a new table complete with images in a new document. The programming itself is easy mode, it's the harnessing of InDesign that takes some time :D