• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Insert images in cells or change/replace text with image

New Here ,
May 03, 2017 May 03, 2017

Copy link to clipboard

Copied

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.

TOPICS
Scripting

Views

2.5K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 03, 2017 May 03, 2017

Copy link to clipboard

Copied

Hi Tina,

what is your version of InDesign?


Something fundamentally changed with table cells in InDesign CC 2015.

Regards,
Uwe

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 03, 2017 May 03, 2017

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 03, 2017 May 03, 2017

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 03, 2017 May 03, 2017

Copy link to clipboard

Copied

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.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.contents.replace(/@/g, "");

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

  if (file.exists) {

  f.remove();

  f.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();

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 04, 2017 May 04, 2017

Copy link to clipboard

Copied

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.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.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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 04, 2017 May 04, 2017

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 04, 2017 May 04, 2017

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 04, 2017 May 04, 2017

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 04, 2017 May 04, 2017

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 04, 2017 May 04, 2017

Copy link to clipboard

Copied

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.parent.constructor.name == "Cell")

{

    var cell = f.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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 04, 2017 May 04, 2017

Copy link to clipboard

Copied

LATEST

Works like a charm, fantastic!

Uwe, thank you very much.

Regards,

Tina

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 03, 2017 May 03, 2017

Copy link to clipboard

Copied

Place images

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
May 03, 2017 May 03, 2017

Copy link to clipboard

Copied

Search the forum " place inline graphics" Thanks

Thanks,
Prabu
Design smarter, faster, and bolder with InDesign scripting.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
May 03, 2017 May 03, 2017

Copy link to clipboard

Copied

Hi,

Try this...

  1. imagePath = "~/Documents"; 
  2. if (app.selection.length == 1 && (app.selection[0] instanceof Column || app.selection[0] instanceof Cell)) 
  3.      app.findGrepPreferences = null; 
  4.      app.findGrepPreferences.findWhat = "^\\u+$"; 
  5.      list = app.selection[0].findGrep(true); 
  6.      err = []; 
  7.      for (i=0; i<list.length; i++) 
  8.      { 
  9.           try { 
  10.                list.insertionPoints[-1].place (File(imagePath+"/"+list.contents+".jpg")); 
  11.           } catch(_) 
  12.           { 
  13.                err.push ('No image found for "'+list.contents+'"'); 
  14.           } 
  15.      } 
  16.      if (err.length) 
  17.           alert (err.join("\r")); 

Thanks

Thanks,
Prabu
Design smarter, faster, and bolder with InDesign scripting.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 04, 2017 May 04, 2017

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines