Copy link to clipboard
Copied
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?
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];
...
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Hi Mike -- Not using any code, just straight-up selecting the text and choosing Export... as Rich Text Format.
Copy link to clipboard
Copied
Would this be of use to you?
Copy link to clipboard
Copied
Thank you, Derek. I have done some testing with that plugin but unfortunately it doesn't export the image names.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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];
Copy link to clipboard
Copied
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();
}
}
}
Copy link to clipboard
Copied
Brilliant! Thank you so much.
Copy link to clipboard
Copied
My pleasure, though Peter did the heavy lifting 🙂 . For additional error handling, you could add an "else" statement to the catch block that alerted the link name so that you could identify which ones don't successfully convert.
Copy link to clipboard
Copied
Hi Brian, it would be possible to have any number of nested groups, which might be a problem with the 4 parent target?
Another approach would be to check all page items with a while loop to drill down through the groups. Something like this:
var pi = app.documents[0].allPageItems;
for(i = 0; i < pi.length; i++){
//gets all document images
if (pi[i].constructor.name == "Image") {
var par = pi[i].parent;
//checks the parents all the way to the Application object
while (par.constructor.name != "Application") {
par = par.parent;
if (par.constructor.name == "Story") {
par.insertionPoints[0].contents = '<link>' + pi[i].itemLink.name + '</link>';
pi[i].parent.remove();
}
}
}
}