Skip to main content
Participating Frequently
October 8, 2014
Answered

create hyperlink for all images

  • October 8, 2014
  • 1 reply
  • 1002 views

Hello everyone,

I am new to scripting, but I am assuming my current task needs some scripting.

I have a catalog laid out in InDesign.  It is about 250 pages and each page has about 10 image so there are about 2500 images.  The way I have them laid out is each image is placed inside a frame.  Now, I need to hyperlink each image to an external web site.  For example, for an image whose name is (12345.jpg), the hyperlink will be "https://www.testabc.com/productdetails.aspx?itemnum=12345".  Similarly if the next item is 67890, then the hyperlink would need to be "https://www.testabc.com/productdetails.aspx?itemnum=67890".  All images are linked to a folder on the computer.

Can you please suggest how best to write and run this script.  Any and all help would be greatly appreciated.

Thank you

This topic has been closed for replies.
Correct answer cchimi

Hello folks,

The above code works great, but for some reason it does not process all the images in the document.  After creating a link for the first 15/20 images, it stops.  I do not get any error messages either.  I created alerts in the java script trying to figure out a pattern but so far no luck. Any suggestion on why the script might not be processing all the images in the document or how best I can debug and fix the problem.

Any help or suggested changes to the code would be greatly appreciated.

Thank you,

Phani


I can't test the script at the moment, but looking at it, I do notice that the entire for loop is within a try-catch. That means that if it encounters an error, it stops processing any more graphics. There may be a good reason for this, but I think you could just move the try-catch into the for loop, so it can catch any individual linking error without stopping completely. Something like this:

var myDoc = app.activeDocument; 

 

var  myGr = app.activeDocument.allGraphics; 

 

alert(myGr.length) 

 

    for(i=0; i<myGr.length; i++) 

    {

      try{

        var myGrName = String(myGr.itemLink.name); 

         

        var mySplitName = myGrName.split(".")[0]; 

         

        app.select(myGr.parent) 

         

        myHyperlinkPageItemSource =  app.activeDocument.hyperlinkPageItemSources.add(app.selection[0]) 

         

        var myHyperlinkURLDestination = app.activeDocument.hyperlinkURLDestinations.add("https://www.testabc.com/productdetails.aspx?itemnum=" + String(mySplitName)));

         

        var myHyperlink = app.activeDocument.hyperlinks.add(myHyperlinkPageItemSource, myHyperlinkURLDestination, {name: "https://www.testabc.com/productdetails.aspx?itemnum=" + String(mySplitName)})   

         

        } catch(e){}

    } 

 

alert("Process Completed")

The forum's syntax highlighting is giving me a hard time, but I would probably throw an "alert(myGrName)" into those catch brackets so you can begin to try to figure out where it's erroring out (if that is indeed the problem).

Another thought is that, if there's a chance of multiple images with the same number, you might want to have your hyperlinkURLDestination check for an already existing destination before adding a new one.

1 reply

BEGINNER_X
Legend
October 9, 2014

Hi Phani,

Below is your code,

var myDoc = app.activeDocument;

var  myGr = app.activeDocument.allGraphics;

alert(myGr.length)

try{

    for(i=0; i<myGr.length; i++)

    {

        var myGrName = String(myGr.itemLink.name);

       

        var mySplitName = myGrName.split(".")[0];

       

        app.select(myGr.parent)

       

        myHyperlinkPageItemSource =  app.activeDocument.hyperlinkPageItemSources.add(app.selection[0])

       

        var myHyperlinkURLDestination = app.activeDocument.hyperlinkURLDestinations.add(String(myGr.itemLink.name));

       

        var myHyperlink = app.activeDocument.hyperlinks.add(myHyperlinkPageItemSource, myHyperlinkURLDestination, {name: "https://www.testabc.com/productdetails.aspx?itemnum=" + String(mySplitName)}) 

       

        }

    }

catch(e){}

alert("Process Completed")

Regards

Siraj

PhaniAuthor
Participating Frequently
October 9, 2014

Hello Siraj,

Thanks for the code. 

When I run it, it creates the links as I can see them in the hyperlinks panel.  However when I export to pdf (with the hyperlinks check box turned on), the links are created in the pdf but the link URL changes.  The new URL is pointing to the location of the PDF\image.jpg.  If I copy the PDF to a differnt location, the links change accordingly.  Very strange.

Also, I noticed that the links being created were for a "shared destination" and not for "URL".

I am pretty sure I am missing something small, but after spending 4 hours and with relatively little scripting knowledge, I am lost.

Any help or suggested changes to the above code would be greatly appreciated.

Thanks & Regards

Inspiring
October 9, 2014

I think you just need to change this line

    var myHyperlinkURLDestination = app.activeDocument.hyperlinkURLDestinations.add(String(myGr.itemLink.name)); 

to

    var myHyperlinkURLDestination = app.activeDocument.hyperlinkURLDestinations.add("https://www.testabc.com/productdetails.aspx?itemnum=" + String(mySplitName))); 

assuming that generates the correct URL (I haven't tried it). The above script is just not actually creating a url link there.