15 Replies Latest reply on May 4, 2017 11:26 PM by tinaz3753070

    Insert images in cells or change/replace text with image

    tinaz3753070

      I would like to automatically insert images into cells.

      I have a file with multiple tables. Almost all tables have a cell or two where an image should be put. Now I insert them manually as inline text (and add cell style, which lines /fits image to cell frame).

      Idea is to put text into cells. E.g. first cell has text 1x, second cell has text 2x etc. Then rename all images (from one folder) to 1x-blahblah, 2x-blahblah ...

      I'd like to somehow merge these specific text cells with specific images.

        • 1. Re: Insert images in cells or change/replace text with image
          Laubender Adobe Community Professional & MVP

          Hi Tina,

          what is your version of InDesign?


          Something fundamentally changed with table cells in InDesign CC 2015.

           

          Regards,
          Uwe

          • 2. Re: Insert images in cells or change/replace text with image
            tinaz3753070 Level 1

            Hello!

            I'm using CS4. Yes, I know... graphic cell in CC looks much easier. But can it be done automatically? To somehow define specific cells and then add images to them with one click (script)?

            Thanks for fast reply.

            Regards,

            Tina

            • 3. Re: Insert images in cells or change/replace text with image
              tinaz3753070 Level 1

              Place images

              This script looks promising. If only I could change placement option- so the image is inserted in cell instead of text frame...

              • 4. Re: Insert images in cells or change/replace text with image
                Ananth@desgin Level 3

                Search the forum " place inline graphics" Thanks

                • 5. Re: Insert images in cells or change/replace text with image
                  Laubender Adobe Community Professional & MVP

                  tinaz3753070  wrote

                  …graphic cell in CC looks much easier. …

                  It's not.

                  It's full of bugs if you seek for a solution to implement a text frame plus an image into this kind of cell with InDesign CC 2015 or CC 2017. I tried several things.

                   

                  One important bug is:

                  If you manage to include a text frame within a graphic cell InDesign will crash if you duplicate or copy/paste a table.

                   

                  I know of two methods where you can include a text frame by means of the UI.

                  There are some more with scripting. I will not expand on this. The result can crash InDesign.

                   

                  Regards,
                  Uwe

                  1 person found this helpful
                  • 6. Re: Insert images in cells or change/replace text with image
                    tinaz3753070 Level 1

                    Just found a script from Kasyan's Personal Site - Home Page

                    Works great- puts (replaces) text with images in a text frame. However I'd like it to work in a cell. Here I get error in line

                      f[i].insertionPoints[0].place(file)

                     

                     

                    main();

                     

                    function main() {

                      var name, f, file, text,

                      arr = [];

                     

                      if(app.documents.length != 0) {

                      var doc = app.activeDocument;

                      var folder = Folder.selectDialog("Choose a folder with images");

                     

                      if (folder != null) {

                      app.findObjectPreferences = app.changeGrepPreferences  = NothingEnum.NOTHING;

                      app.findGrepPreferences.findWhat = "@.+?@";

                      f = doc.findGrep(true);

                     

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

                      name = f[i].contents.replace(/@/g, "");

                      file = new File(folder.fsName + "/" + name);

                     

                      if (file.exists) {

                      f[i].remove();

                      f[i].insertionPoints[0].place(file);

                      }

                      else {

                      arr.push("File doesn't exist '" + name + "'");

                      }

                      }

                     

                      app.findObjectPreferences = app.changeGrepPreferences  = NothingEnum.NOTHING;

                     

                      arr.push("------------------------------------------");

                      text = arr.join("\r");

                      writeToFile(text);

                      }

                      }

                      else{

                      alert("Please open a document and try again.");

                      }

                    }

                     

                    function writeToFile(text) {

                      var file = new File("~/Desktop/Place inline images.txt");

                      if (file.exists) {

                      file.open("e");

                      file.seek(0, 2);

                      }

                      else {

                      file.open("w");

                      }

                      file.write(text + "\r");

                      file.close();

                    }

                    • 7. Re: Insert images in cells or change/replace text with image
                      Ananth@desgin Level 3

                      Hi,

                       

                      Try this...

                       

                      imagePath = "~/Documents";  
                      if (app.selection.length == 1 && (app.selection[0] instanceof Column || app.selection[0] instanceof Cell))  
                      {  
                           app.findGrepPreferences = null;  
                           app.findGrepPreferences.findWhat = "^\\u+$";  
                           list = app.selection[0].findGrep(true);  
                           err = [];  
                           for (i=0; i<list.length; i++)  
                           {  
                                try {  
                                     list[i].insertionPoints[-1].place (File(imagePath+"/"+list[i].contents+".jpg"));  
                                } catch(_)  
                                {  
                                     err.push ('No image found for "'+list[i].contents+'"');  
                                }  
                           }  
                           if (err.length)  
                                alert (err.join("\r"));  
                      }  
                      
                      

                      Thanks

                      1 person found this helpful
                      • 8. Re: Insert images in cells or change/replace text with image
                        tinaz3753070 Level 1

                        Hi,

                        thanks for your work!

                        I can't get it working; guess I'm doing something wrong.

                        I tried original script on a different pc, and I get error. Only if I put @image.jpg@ between some text, then it works.

                        • 9. Re: Insert images in cells or change/replace text with image
                          Laubender Adobe Community Professional & MVP

                          Hi Tina,

                          see also into this thread here where some pitfalls of placing images to text cells are discussed:

                          Re: Place image into table cell based on text value

                           

                          So my suggestions would be:

                           

                          If f[i].parent is a Cell object:

                           

                          1. Add a rectangle to the first insertion point of the cell.

                          2. Place the image to the rectangle.

                           

                          Warning:

                          Depending on the size of the rectangle and the kind of its anchoring the cell could overflow.

                           

                          Alternatively:

                          Consider a rectangle on the page where the image is placed to, resized to fit your needs and then will be anchored to the first insertion point of the cell with AnchorPosition.ANCHORED.

                           

                          For AnchorPosition.INLINE_POSITION you may consider several other things:

                          Pay attention to the insets of the cell.

                          Maybe set them to value 0.

                          You may do this after setting the cell height to a fixed value:

                          f[i].parent.autogrow = false

                           

                          Pay attention to the stroke weights of the cells.

                          Calculate the net height and net width of the cell if you want to anchor inline or aboveline.
                          Make sure the cell does not overflow after adding the rectangle.

                           

                          Regards,
                          Uwe

                          • 10. Re: Insert images in cells or change/replace text with image
                            Laubender Adobe Community Professional & MVP

                            Also important for a more generalized kind of script:

                            Take also into account:

                             

                            Cell

                            1. rotation angle ( 0, 90, 180, or 270 )

                            2. writing direction ( horizontal or vertical )

                            3. Text (where exactly is your first insertion point positioned?) :

                            align to baseline ( true or false )

                            baseline shift

                            character rotation

                            character direction

                            justification

                            right indent

                            left indent

                            first line indent

                            paragraph direction

                             

                            Table:

                            3. table direction ( right-to-left or left-to-right )

                             

                            Regards,
                            Uwe

                            • 11. Re: Insert images in cells or change/replace text with image
                              tinaz3753070 Level 1

                              Hi Uwe,

                              When I manually insert images they never overflow- they are cropped (I use couple of predefined dimensions). And cell has a style (zero inset), which precisely fits image:

                              Untitled-1.jpg

                              Frankly I'm not a programmer, that's why I have so much trouble modifying a script...

                              Cheers,

                              Tina

                              • 12. Re: Insert images in cells or change/replace text with image
                                Laubender Adobe Community Professional & MVP

                                Frankly I'm not a programmer, that's why I have so much trouble modifying a script...

                                 

                                Hi Tina,

                                good to know that it will work with surrounding text.
                                Do you mean surrounding text in a text cell or will it only work with text in a text frame?

                                 

                                Also good that you provided a screenshot.

                                Are you using an object style for the images where the anchoring is predefined?


                                Maybe you could discribe your "manual" way how you place the image and position it to the cell in steps.
                                That would help…

                                 

                                And also:
                                Provide an InDesign document with a working example where you placed some images to table cells.
                                Upload the document to a download service like Dropbox and provide the link.

                                 

                                Regards,
                                Uwe

                                 

                                PS:

                                You posted your question in the InDesign Scripting forum where scripting problems are discussed.
                                So do not assume that a non-scripting person will understand all suggestions and code examples.

                                If you are lucky one kind scripter will look into your problem deeply and provides a full script.

                                 

                                Text cells are complex beasts.

                                So you were right as you assumed that working with graphic cells in CC 2015 or CC 2017 would be less problematic.

                                • 13. Re: Insert images in cells or change/replace text with image
                                  tinaz3753070 Level 1

                                  Hi,

                                  Script works with surrounding text both in a cell and in a text frame.

                                   

                                  Ok, the manual way, it's very simple:

                                  I place image in a cell (type tool - as inline text), then apply cell style (insets:0, Align: Bottom, Offset: Fixed, Clip contents to cell)

                                  I don't use object style. Anchoring would add another step.

                                   

                                  Link to example:

                                  https://we.tl/ZdDIoMjLnK

                                   

                                  Regards,

                                  Tina

                                  • 14. Re: Insert images in cells or change/replace text with image
                                    Laubender Adobe Community Professional & MVP

                                    Hi Tina,

                                    that was really helpful. Thanks.

                                     

                                    Detect the line in Kasyan's script that says:

                                     

                                    if (file.exists) {
                                    

                                     

                                    Make room for a bit more code and add the following after that line:

                                     

                                    if(f[i].parent.constructor.name == "Cell")
                                    {
                                        var cell = f[i].parent;
                                        cell.autoGrow = false;
                                        cell.texts[0].contents = "";
                                        cell.insertionPoints[0].place(file);
                                    
                                        continue;
                                    
                                    };
                                    

                                     

                                    That should do the trick if you provide a bit more surrounding text to e.g.:

                                    @x2.tif@ .

                                     

                                    Cheers,
                                    Uwe

                                    1 person found this helpful
                                    • 15. Re: Insert images in cells or change/replace text with image
                                      tinaz3753070 Level 1

                                      Works like a charm, fantastic!

                                      Uwe, thank you very much.

                                       

                                      Regards,

                                      Tina