Skip to main content
Participating Frequently
July 1, 2020
Answered

Script to replace images with their names

  • July 1, 2020
  • 3 replies
  • 3017 views

I'm exporting text from an InDesign file to rtf, for an author who only works in Word to use when writing the next edition of a book. 

I would like the text export to contain the filename of inline images instead of (or in addition to?) the image preview that currently exports. 

I can find lots of "replace text with an image" scripts but not the other way around. Does anyone know of such a thing?

This topic has been closed for replies.
Correct answer brian_p_dts

Maybe something like this? 

var links = app.documents[0].links.everyItem().getElements();
for (i = links.length-1; i >= 0; i--) {
   try {
       var target = links[i].parent.parent.parent;
       var ip = target.insertionPoints[0];
       ip.contents = '<link>' + links[i].name + '</link>';
       links[i].parent.parent.remove();
   } catch(e) {
       if (target.constructor.name == "Group") {
           target = links[i].parent.parent.parent.parent;
           ip = target.insertionPoints[0];
           ip.contents = '<link>' + links[i].name + '</link>';
           links[i].parent.parent.parent.remove();
       }
   }
}

3 replies

Peter Kahrel
Community Expert
Community Expert
July 2, 2020

Here's a simple script to replace inline images with their names:

 

 

links = app.documents[0].links.everyItem().getElements();
for (i = links.length-1; i >= 0; i--) {
   ip = links[i].parent.parent.parent.insertionPoints[0];
   ip.contents = '<link>' + links[i].name + '</link>';
   links[i].parent.parent.remove();
}

 

It deals only with straightfoward inlines. The codes <link> and </link> you can change to anything that catches your fancy.

P.

debraleagAuthor
Participating Frequently
July 2, 2020

Thank you, Peter! It mostly worked until arriving at a less-than-straightforward anchored, grouped image. Then it threw off this error and declined to go any further. Any suggestions for a tweak? 

JavaScript Error!

Error Number: 55
Error String: Object does not support the property or method 'insertionPoints'

Engine: main
File: /Applications/Adobe InDesign 2020/Scripts/Scripts Panel/Samples/JavaScript/Image-to-Name.jsx
Line: 7
Source:    ip = links[i].parent.parent.parent.insertionPoints[0];

 

brian_p_dts
Community Expert
brian_p_dtsCommunity ExpertCorrect answer
Community Expert
July 2, 2020

Maybe something like this? 

var links = app.documents[0].links.everyItem().getElements();
for (i = links.length-1; i >= 0; i--) {
   try {
       var target = links[i].parent.parent.parent;
       var ip = target.insertionPoints[0];
       ip.contents = '<link>' + links[i].name + '</link>';
       links[i].parent.parent.remove();
   } catch(e) {
       if (target.constructor.name == "Group") {
           target = links[i].parent.parent.parent.parent;
           ip = target.insertionPoints[0];
           ip.contents = '<link>' + links[i].name + '</link>';
           links[i].parent.parent.parent.remove();
       }
   }
}
debraleagAuthor
Participating Frequently
July 2, 2020

Thank you, Derek. I have done some testing with that plugin but unfortunately it doesn't export the image names.

Legend
July 2, 2020

Hello debraleag,

Can you post the code you're currently using for the export?

 

Regards,

Mike

debraleagAuthor
Participating Frequently
July 2, 2020

Hi Mike -- Not using any code, just straight-up selecting the text and choosing Export... as Rich Text Format.