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

Script to replace images with their names

Explorer ,
Jul 01, 2020 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?

TOPICS
Import and export , Scripting

Views

1.9K

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

correct answers 1 Correct answer

Community Expert , Jul 02, 2020 Jul 02, 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];
   
...

Votes

Translate

Translate
Advisor ,
Jul 01, 2020 Jul 01, 2020

Copy link to clipboard

Copied

Hello debraleag,

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

 

Regards,

Mike

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
Explorer ,
Jul 01, 2020 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.

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

Copy link to clipboard

Copied

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
Explorer ,
Jul 02, 2020 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.

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

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
Explorer ,
Jul 02, 2020 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];

 

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 ,
Jul 02, 2020 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();
       }
   }
}

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
Explorer ,
Jul 02, 2020 Jul 02, 2020

Copy link to clipboard

Copied

Brilliant! Thank you so much. 

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

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 ,
Jul 02, 2020 Jul 02, 2020

Copy link to clipboard

Copied

LATEST

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

 

 

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