Skip to main content
Known Participant
August 3, 2015
Answered

how to add caption for all images in indesign document using indesign javscript

  • August 3, 2015
  • 7 replies
  • 1328 views

i want to add caption for all the images in indesign document..

this is my code .. im getting error like ,

invalid parameter.

capset ();

function capset()

{

var doc = app.activeDocument;

var myPicture = doc.allGraphics;

var gb,i, myCaption;

while(pic = myPicture.pop() )

{  

gb= pic.geometricBounds;

// add a frame to to picture's parent, which is a Page

myCaption = pic.parent.textFrames.add ();

// set position and size of the caption

myCaption.geometricBounds = [gb[2], gb[1], gb[2]+3, gb[3]];

// add placeholder contents

myCaption.contents = "Caption";

pic.parent.groups.add ([pic, myCaption]);

}

}

This topic has been closed for replies.
Correct answer Jump_Over

Hi,

Runing a code with your doc you need to notice possible occurrences (existing groups, anchored items, locked items, etc)

This one supposes to exclude grouped and anchored:

capset ();

function capset()

{

var

  mDoc = app.activeDocument,

  // anchored or grouped items excluded

  myGraphics = mDoc.splineItems.everyItem().getElements(),

  picContainer, gb, myCaption;

while(picContainer = myGraphics.pop() )

{

  // exclude empty frames

  if (!picContainer.graphics.length) continue;

  gb= picContainer.geometricBounds;

  // add a frame to to picture's parent, which is a Page

  myCaption = picContainer.parent.textFrames.add ();

  // set position and size of the caption

  myCaption.geometricBounds = [gb[2], gb[1], gb[2]+3, gb[3]];

  // add placeholder contents

  myCaption.contents = "Caption";

  mDoc.groups.add ([picContainer, myCaption]);

  }

}

Jarek

7 replies

mani4Author
Known Participant
August 3, 2015

thanks for your great support .

I Changed that line and now its working fine.

mani4Author
Known Participant
August 3, 2015

thank u so much jarek.. your code is working fine..

now i'm getting one problem in this ,

i have document like book with left and right page..

when i run this script, it adds captions to all the images , but problem is ,the image which is in right-hand side page having caption in left-hand side page .

Because of the geometricBounds same for left and side page ...

for left-hand side images ,it is working fine.

so, caption has to add separately for each page images ..

Jump_Over
Legend
August 3, 2015

Hi,

line 16:

myCaption = picContainer.parentPage.textFrames.add ();


Jarek

mani4Author
Known Participant
August 3, 2015

yeah , i tried this now,,

same error only showing

Jump_Over
Jump_OverCorrect answer
Legend
August 3, 2015

Hi,

Runing a code with your doc you need to notice possible occurrences (existing groups, anchored items, locked items, etc)

This one supposes to exclude grouped and anchored:

capset ();

function capset()

{

var

  mDoc = app.activeDocument,

  // anchored or grouped items excluded

  myGraphics = mDoc.splineItems.everyItem().getElements(),

  picContainer, gb, myCaption;

while(picContainer = myGraphics.pop() )

{

  // exclude empty frames

  if (!picContainer.graphics.length) continue;

  gb= picContainer.geometricBounds;

  // add a frame to to picture's parent, which is a Page

  myCaption = picContainer.parent.textFrames.add ();

  // set position and size of the caption

  myCaption.geometricBounds = [gb[2], gb[1], gb[2]+3, gb[3]];

  // add placeholder contents

  myCaption.contents = "Caption";

  mDoc.groups.add ([picContainer, myCaption]);

  }

}

Jarek

mani4Author
Known Participant
August 3, 2015

this line only showing error ..

pic.parent.groups.add ([pic, myCaption]);

Jump_Over
Legend
August 3, 2015

Hi,

It should be:

doc.groups.add([pic.parent, myCaption]);

rather...

Jarek

mani4Author
Known Participant
August 3, 2015

one more thing is ,

it working for single image ...using selection option

Jump_Over
Legend
August 3, 2015

Hi,

Paste which line returns this error, pls

Jarek

mani4Author
Known Participant
August 3, 2015

i released the anchor of all images and i tired using pic.parent

eventhough im getting the same error messege..like

"Invalid parameter"

Jump_Over
Legend
August 3, 2015

Hi,

3 remarks:

- var pic is a graphic (pageItem.contents) so can't be element of group - use its parent instead of

- you cant add textFrame to graphic container by the same reason ("picture's parent, which is a Page" not always true)

- if pic.parent is anchored - adding textFrame and setting its gBounds wont work

so

- target your code to pic.parent

- check if it is anchored before adding caption

Jarek

Community Expert
August 3, 2015

Just a remark, Jarek:
You can add a text frame to a graphic frame by scripting with method add()
You cannot do it in the GUI.

But there is a practical problem using this with a graphic frame:

The text frame is only visible inside the bounds of the graphic frame, because it is nested into the graphic frame.

If you want the caption stay in the visible area of the placed image, no problem.

If you want the caption below, above, left or right of the visible area, there is a problem using this method.

Uwe

Jump_Over
Legend
August 3, 2015

Hi Uwe,

Thanks for this remark, you're right indeed

Jarek