9 Replies Latest reply on Jan 13, 2014 4:26 AM by KevinBird1966

    Place image into table cell based on text value

    KevinBird1966 Level 1

      Hi,

       

      I'm looking at converting plain text into InDesign Tagged Text. This tagged text stream will have multiple tables, and each table will have a cell whose text value is the full path to the image that needs to be placed in that cell (eg. file:///JohnHobbs.eps).

       

      Is there a script around that will import the image referenced by the string value of the cell and fit it horizontally/proportionally?

       

      Regards.

       

      --

      Kevin

        • 1. Re: Place image into table cell based on text value
          Learner Learner Level 1

          Hi Kevin,

           

          Try this...

           

          var doc = app.activeDocument;

              cells = doc.stories.everyItem().tables.everyItem().cells.everyItem().getElements();

          for (var i=0; i<cells.length; i++){

                  cell = cells[i].contents;

                  cells[i].contents = "";

                  try{

                      cells[i].insertionPoints.item(0).place(File(cell));

                  }

                  catch(e){}

                  }

          • 2. Re: Place image into table cell based on text value
            KevinBird1966 Level 1

            Hi,

             

            Many thanks for your reply.

             

            I’ve made some small modifications to the script to match my needs.

             

            I have one remaining question.

             

            The cell increases in depth to accommodate the graphic. I know I can fix the cell depth and the graphic will crop, but can I place a graphic at a certain percentage size? This would save me having to re-size all my graphics in an external program first.

             

            Kind regards.

             

            --

            Kevin

            • 3. Re: Place image into table cell based on text value
              Learner Learner Level 1

              Hi Kevin,

               

              Here it is,

               

              var doc = app.activeDocument;

                  cells = doc.stories.everyItem().tables.everyItem().cells.everyItem().getElements();

              for (var i=0; i<cells.length; i++){

                      cell = cells[i].contents;

                      cells[i].contents = "";

                      try{

                          var image = cells[i].insertionPoints.item(0).place(File(cell));

                          image[i].absoluteHorizontalScale = 50;

                          image[i].absoluteVerticalScale = 50

                          image[i].fit(FitOptions.FRAME_TO_CONTENT);

                      }

                      catch(e){}

                      }

              • 4. Re: Place image into table cell based on text value
                KevinBird1966 Level 1

                Hi,

                 

                Thanks for the reply.

                 

                I tried implementing your code, but the results weren't as expected.

                 

                I changed...

                 

                image[i].absoluteHorizontalScale = 50;

                 

                to

                 

                image.absoluteHorizontalScale = 50;

                 

                ...as I'm not sure 'image' is an array, but that doesn't change anything. The file gets placed as an anchored image, but the size doesn't change (regadless of scale settings).

                 

                As I missing something obvious?

                 

                Regards.

                 

                --

                Kevin

                 

                 

                 


                • 5. Re: Place image into table cell based on text value
                  Jump_Over Level 5

                  Hi,

                   

                  Image is an array in above code. Image[0] is currently placed one.

                  However notice that above code removes content from each cell regardless it is a path...

                  Notice as well, that ID is placing an image into a rectangle which is placed into cell's contents, in fact. So there are two "fitting" efforts here.

                   

                  You could try something like that:

                  var 
                       doc = app.activeDocument, destCell, currFile, currRec, currImage, i, 
                       cells = doc.stories.everyItem().tables.everyItem().cells.everyItem().getElements();
                  
                  for (i=0; i<cells.length; i++){
                            destCell = cells[i];
                            currFile = File(destCell.contents);
                            if (currFile.exists) {
                                      destCell.contents = "";
                                      currRec = recSize(destCell);
                                      currImage = currRec.place(currFile)[0];
                                      currImage.fit(FitOptions.FILL_PROPORTIONALLY);
                                      currImage.fit(FitOptions.FRAME_TO_CONTENT);
                                      }
                            }
                  
                  function recSize(cell) {
                            cell.texts[0].properties = {
                                      keepBaseline: false,
                                      justification: Justification.CENTER_ALIGN
                                      } ;        //**
                  
                            var 
                            mW = cell.width,
                            mH = cell.height,
                            mRec = cell.insertionPoints[0].rectangles.add(),
                            mRecGB = mRec.geometricBounds,
                            mRecFinGB = [
                                      mRecGB[0], 
                                      mRecGB[1], 
                                      mRecGB[0]  + mH - (cell.bottomInset + cell.topInset), 
                                      mRecGB[1]  + mW - (cell.rightInset + cell.leftInset)];
                  
                            mRec.appliedObjectStyle = doc.objectStyles.item(0);     //***
                            mRec.geometricBounds = mRecFinGB;
                            cell.properties = {
                                      autoGrow: true,
                                      verticalJustification: VerticalJustification.CENTER_ALIGN
                                      };         //**
                            return mRec;
                            }
                  

                  ** some custom properties are applied

                  ***objectStyle "None" is applied to each rectangle (image container).

                   

                  Jarek

                  • 6. Re: Place image into table cell based on text value
                    Laubender Adobe Community Professional & MVP

                    @Jarek – two main issues I encountered with your code:

                     

                    1. Since placing images in cells could set a table to overflow, I'd loop from back to forth.

                     

                    2. Testing if a file exists is not enough, if the contents of the cell is empty and is used as file path name (or as a part of a file path name)!

                    An error will be thrown: "File already is open and cannot be placed".

                    This is misleading. I don't know, if this will happen on Windows OS, but I came accross this issue when testing on Mac OSX 10.6.8.

                     

                    To solve #1 is easy.

                     

                    For issue #2 I tried to close the file with currFile.close(), but with an empty path this did not work.

                    So I did not test with currFile.exists, but placed the file elsewhere in the document, and if succesfully, removed the placed one right after. All this is wrapped with a try/catch => continue:

                     

                    try{
                    
                        var myTest = doc.pages[0].place(currFile)[0].getElements()[0].parent.remove();
                    
                        }catch(e){continue};
                    

                     

                    My test scenario was the following:

                     

                    One table with image names in the cells. Some cells empty, some cells with different contents, some image names repeated.
                    All images to place are in the same folder as the InDesign file.

                     

                    Before:

                     

                    BEFORE-Placing.png

                     

                     

                    After:

                     

                    AFTER-Placing.png

                     

                     

                    Here your slightly changed code I did this test successfully in the given scenario.
                    Without the changes I had the two issues above.

                     

                    var 
                         doc = app.activeDocument, destCell, currFile, currRec, currImage, i, 
                         cells = doc.stories.everyItem().tables.everyItem().cells.everyItem().getElements();
                    
                    //CHANGE 1: Loop from back to forth:
                    for (i=cells.length-1; i>=0; i--){
                        destCell = cells[i];
                        
                        //CHANGE 2:
                        //Just for my specific testing
                        //(All files are in the same folder as the InDesign file, 
                        //so I could use the doc's filePath together with the cell's contents)
                    
                        currFile = File(doc.filePath+"/"+destCell.contents);
                        
                        //CHANGE 3:
                        //Instead of testing the file with currFile.exists, I did a try/catch scenario with a
                        //random place action on page one of the document immediately removing the image together
                        //with its container.
                        
                        //If this action is NOT successful the code below will not be carried out on the current file
                        //The loop will continue to the next possible file.
                        try{
                    
                            var myTest = doc.pages[0].place(currFile)[0].getElements()[0].parent.remove();
                    
                            }catch(e){continue};
                    
                        destCell.contents = "";
                        currRec = recSize(destCell);
                        currImage = currRec.place(currFile)[0];
                        currImage.fit(FitOptions.FILL_PROPORTIONALLY);
                        currImage.fit(FitOptions.FRAME_TO_CONTENT);
                    
                    }
                    
                    function recSize(cell) {
                              cell.texts[0].properties = {
                                        keepBaseline: false,
                                        justification: Justification.CENTER_ALIGN
                                        } ;        //**
                    
                              var 
                              mW = cell.width,
                              mH = cell.height,
                              mRec = cell.insertionPoints[0].rectangles.add(),
                              mRecGB = mRec.geometricBounds,
                              mRecFinGB = [
                                        mRecGB[0], 
                                        mRecGB[1], 
                                        mRecGB[0]  + mH - (cell.bottomInset + cell.topInset), 
                                        mRecGB[1]  + mW - (cell.rightInset + cell.leftInset)];
                    
                              mRec.appliedObjectStyle = doc.objectStyles.item(0);     //***
                              mRec.geometricBounds = mRecFinGB;
                              cell.properties = {
                                        autoGrow: true,
                                        verticalJustification: VerticalJustification.CENTER_ALIGN
                                        };         //**
                              return mRec;
                              }
                    

                     

                    I still can see one possible issue:

                     

                    On Mac OSX when using file or folder names with special characters (e.g. Umlauts like ä,ö,ü etc.pp.) this script could fail for these files.
                    This is due to the double-byte representation of special characters in file names you cannot typeset alike in InDesign.

                     

                    It does not help to copy/paste the names from the OS to InDesign.
                    This problem is discussed elsewhere in this forum.

                     

                    Uwe

                    • 7. Re: Place image into table cell based on text value
                      Jump_Over Level 5

                      Hi,

                      @ Uwe, You are right as usually

                       

                      Just showed the way but traps passed over in silence...

                       

                      Jarek

                      • 8. Re: Place image into table cell based on text value
                        Laubender Adobe Community Professional & MVP

                        @Jarek – not as "usually" ;-)

                         

                        There might be other issues, if the cells could not be expanded deep enough or if you place PDFs (the PDF boxes are not set and you want to place by crop box etc.).

                         

                        A lot of work is to be done to make that bullet proof…

                         

                        Uwe

                        • 9. Re: Place image into table cell based on text value
                          KevinBird1966 Level 1

                          Hi Jarek & Uwe,

                           

                          Many thanks for replying to my original post, and explaining the potential problems/pitfalls - it is appreciated.

                           

                          I can see that I still have a lot to learn!

                           

                          Regards.

                           

                          --

                          Kevin