9 Replies Latest reply on Oct 29, 2014 8:30 AM by Phani

    create hyperlink for all images

    Phani

      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

        • 1. Re: create hyperlink for all images
          BEGINNER_X Level 3

          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[i].itemLink.name);
                  
                  var mySplitName = myGrName.split(".")[0];
                  
                  app.select(myGr[i].parent)
                  
                  myHyperlinkPageItemSource =  app.activeDocument.hyperlinkPageItemSources.add(app.selection[0])
                  
                  var myHyperlinkURLDestination = app.activeDocument.hyperlinkURLDestinations.add(String(myGr[i].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

          • 2. Re: create hyperlink for all images
            Phani Level 1

            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

            • 3. Re: create hyperlink for all images
              cchimi Level 2

              I think you just need to change this line

               

                  var myHyperlinkURLDestination = app.activeDocument.hyperlinkURLDestinations.add(String(myGr[i].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.

              • 4. Re: create hyperlink for all images
                Phani Level 1

                This is exactly what was needed.  Now it runs like a charm.

                 

                Thank you Siraj and cchimi for your help.

                 

                Phani

                • 5. Re: create hyperlink for all images
                  Phani Level 1

                  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

                  • 6. Re: Re: create hyperlink for all images
                    cchimi Level 2

                    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[i].itemLink.name);  
                              
                            var mySplitName = myGrName.split(".")[0];  
                              
                            app.select(myGr[i].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.

                    • 7. Re: create hyperlink for all images
                      Phani Level 1

                      Thanks again for your help.  Yes, now all the images are processed.  Is there anyway I can trap the errors to see which images failed.  Thanks again for your help.

                      • 8. Re: create hyperlink for all images
                        cchimi Level 2

                        I had to edit my answer a bunch of times because I was having trouble with the syntax highlighter, so make sure you're looking at the final version (not the one you got in the email notification).

                         

                        I think putting an alert in the catch should give you a good starting point. Where it says

                         

                            }catch(e){}

                         

                        replace that with

                         

                            }catch(e){alert(myGrName);}

                         

                        My guess is that the issue has to do with multiple images with the same number ID, but it'll be clearer once you know which images aren't passing muster.

                         

                        If you're using the ESTK, you can also use

                         

                            }catch(e){$.writeln(myGrName);}

                         

                        which will print the problem names to the ESTK console rather than bugging you with a message box for each one.

                        • 9. Re: create hyperlink for all images
                          Phani Level 1

                          Works like a charm.  Thanks cchmi, appreciate your help.  You saved me from a lot of headache.