Highlighted

Script to replace images with their names

Community Beginner ,
Jul 01, 2020

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?

Adobe Community Professional
Correct answer by brianp311 | Adobe Community Professional

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();
       }
   }
}
TOPICS
Import and export, Scripting

Views

167

Likes

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

Script to replace images with their names

Community Beginner ,
Jul 01, 2020

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?

Adobe Community Professional
Correct answer by brianp311 | Adobe Community Professional

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();
       }
   }
}
TOPICS
Import and export, Scripting

Views

168

Likes

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
Jul 01, 2020 0
Engaged ,
Jul 01, 2020

Copy link to clipboard

Copied

Hello debraleag,

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

 

Regards,

Mike

Likes

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
Reply
Loading...
Jul 01, 2020 0
Community Beginner ,
Jul 01, 2020

Copy link to clipboard

Copied

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

Likes

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
Reply
Loading...
Jul 01, 2020 0
Adobe Community Professional ,
Jul 01, 2020

Copy link to clipboard

Copied

Likes

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
Reply
Loading...
Jul 01, 2020 0
Community Beginner ,
Jul 02, 2020

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.

Likes

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
Reply
Loading...
Jul 02, 2020 0
Adobe Community Professional ,
Jul 02, 2020

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.

Likes

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
Reply
Loading...
Jul 02, 2020 0
Community Beginner ,
Jul 02, 2020

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];

 

Likes

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
Reply
Loading...
Jul 02, 2020 0
Adobe Community Professional ,
Jul 02, 2020

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();
       }
   }
}

Likes

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
Reply
Loading...
Jul 02, 2020 0
Community Beginner ,
Jul 02, 2020

Copy link to clipboard

Copied

Brilliant! Thank you so much. 

Likes

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
Reply
Loading...
Jul 02, 2020 0
Adobe Community Professional ,
Jul 02, 2020

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. 

Likes

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
Reply
Loading...
Jul 02, 2020 0
rob day LATEST
Adobe Community Professional ,
Jul 02, 2020

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();
            }
        }
    } 
}

 

 

Likes

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
Reply
Loading...
Jul 02, 2020 0