26 Replies Latest reply on May 28, 2013 7:48 AM by Laubender

    Viewing or printing a list of hyperlinks contained in a document

    gwhPoster Level 1

      Hi everyone,

      I have a document containing hundreds of hyperlinks. Is it possible to automatically compile a list of those hyperlinks so that I can view and/or print them as I need to check the syntax and it would be easier if they were viewable all in one place.

       

      Appreciate any advice.

        • 1. Re: Viewing or printing a list of hyperlinks contained in a document
          Peter Kahrel Adobe Community Professional & MVP

          What do you want that list to show? The names of the hyperlinks? Could be as simple as this:

           

          hlinks = app.documents[0].hyperlinks.everyItem().name.join('\r');
          frame= app.documents.add().textFrames.add ({geometricBounds: [0,0,'50mm','50mm']});
          frame.contents = hlinks;
          

          Peter

          • 2. Re: Viewing or printing a list of hyperlinks contained in a document
            gwhPoster Level 1

            Thanks so much for the reply,

             

             

            It would be good to show the source hyperlink text and then the url next to it. Is that possible to do?

             

            If yes, what do I do with the code that you supplied? Do I put I paste it into a plain text file and append a .jsx extension and then acces it via the scripts panel?

            • 3. Re: Viewing or printing a list of hyperlinks contained in a document
              nukleas Level 1

              Hello,

               

              If you would like, you can use this script:

               

              https://github.com/nukleas/indesign-incopy-snippets/blob/master/extractHyperlinks.js

               

              It will extract the current document's hyperlinks and save them to a tab delimited text file you can open up in word/excel/etc

               

              Here's the raw code:

               

               

               

              function extractHyperlinks(doc) {
                  "use strict";
                  var i,
                    linkedText, // The source text
                    linkedURL, // The URL destination
                    extracted_hyperlinks = [], // The array of hyperlinks
                    myHyperlinks = doc.hyperlinks, // The hyperlinks in the given document
                    extracted_hyperlink; // Individual extracted hyperlink
                  for (i = 0; i < myHyperlinks.length; i += 1) {
               // If you don't do the following check to make sure the objects exist and are not null, the script will break when there isnt a source or destination available.
                      if (myHyperlinks.item(i).source.sourceText && myHyperlinks.item(i).destination) {
                          linkedText = myHyperlinks.item(i).source.sourceText.contents || ""; // extract linked text
                          linkedURL = myHyperlinks.item(i).destination.destinationURL || ""; // extract destination URL
                      }
                      // Merge information in hyperlink object
               extracted_hyperlink = {
               text: linkedText,
               URL: linkedURL
               };
                      extracted_hyperlinks.push(extracted_hyperlink); // Push to hyperlink array
                  }
                  return extracted_hyperlinks;
              }
              
              
              function writeHyperlinksToFile(hyperlinks) {
                  "use strict";
                  var myFilePath = File.openDialog("Choose where to save your Hyperlinks"),
                      outputFile = new File(myFilePath),
                      i;
                  outputFile = new File(outputFile);
                  outputFile.open('w');
                  for (i = 0; i < hyperlinks.length; i += 1) {
                      outputFile.writeln(hyperlinks[i].text + "\t" + hyperlinks[i].URL); // tab-delimited text, URL
                  }
                  outputFile.close();
              }
              function main() {
                  "use strict";
                  var extracted_hyperlinks = extractHyperlinks(app.documents[0]);
                  writeHyperlinksToFile(extracted_hyperlinks);
              }
              main();
              
              
              • 4. Re: Viewing or printing a list of hyperlinks contained in a document
                gwhPoster Level 1

                Wow - that looks great. Will it save the hyperlink source text as well as ther url?

                 

                Thanks so much.

                • 5. Re: Viewing or printing a list of hyperlinks contained in a document
                  nukleas Level 1

                  Indeed! Let's say you have, say, "Adobe" linked as "http://www.adobe.com."

                   

                  It will create a text file that will look something like this:

                   

                  Adobe     http://www.adobe.com

                   

                  Since it's tab-delimited, you should even be able to just toss it into Excel and make a two-column table with it!

                   

                  Hope this helps!

                  • 7. Re: Viewing or printing a list of hyperlinks contained in a document
                    Peter Kahrel Adobe Community Professional & MVP

                    Nice one, Nukleas. What's the "use strict" for, though?

                    • 8. Re: Viewing or printing a list of hyperlinks contained in a document
                      Vamitul Level 4

                      peter:

                      https://developer.mozilla.org/en/docs/JavaScript/Reference/Functions_and_function_scope/St rict_mode

                       

                      i'm quite sure it's useless in extendscript.

                       

                      Later edit:

                      after a quick test, i can cofirm it's useless:

                       

                      "use strict";
                      var obj={x:12};
                      with (obj){  //with should be disallowed when using strict mode, however with extendscript it's business as usual.
                          x=16;}
                      alert(obj.x);
                      

                       

                      Message was edited by: Vamitul

                      • 9. Re: Viewing or printing a list of hyperlinks contained in a document
                        Peter Kahrel Adobe Community Professional & MVP

                        You're right, Vamitul: the examples in the link you gave should throw errors but don't.

                         

                        -- as you tried too, I now see!

                         

                        Message was edited by: Peter Kahrel

                        • 10. Re: Viewing or printing a list of hyperlinks contained in a document
                          nukleas Level 1

                          I am not too surprised if it is useless, I keep them in whatever I write in JavaScript as a "good habit", but please feel free to take it out!

                          • 11. Re: Viewing or printing a list of hyperlinks contained in a document
                            Laubender Adobe Community Professional & MVP

                            @Nukleas – did you test your script on documents that contain a tabel of contents?
                            It will break on line 13:

                             

                            linkedURL = myHyperlinks.item(i).destination.destinationURL || ""; // extract destination URL
                            

                             

                            Error message is: Objekt unterstützt Eigenschaft oder Methode destinationURL nicht.
                            Translation: Object does not support property or method destinationURL

                             

                            That is because the Table of Contents feature in InDesign has no "destinationURL", but a target that refers to pages.

                             

                            You could wrap that line in a try/catch. Is working for me.

                             

                            Also: what I cannot get to work is the save dialog from your script.
                            The "Open" button is greyed out:

                             

                            SaveHyperlinkInfo.png

                             

                            Instead of "File.openDialog()", you could use "File.saveDialog()". That is working for me…

                             

                            I'm on Mac OSX 10.6.8 with InDesign CS5.5 (German).

                             

                            Uwe

                            • 12. Re: Viewing or printing a list of hyperlinks contained in a document
                              Vamitul Level 4

                              your "good habit" might get you in a lot of trouble in the browser world, as strict mode implementation differs substantialy from each browser (especially mobile ones)

                              • 13. Re: Viewing or printing a list of hyperlinks contained in a document
                                nukleas Level 1

                                Ah, that's a good call! I am testing on Windows so it views a bit differently for me.

                                 

                                I have changed the dialog to save dialog, for the destination I thought it might be better to try to catch any destination, this is a roundabout way:

                                 

                                 

                                            for (destination_type in myHyperlinks.item(i).destination) {
                                                if (myHyperlinks.item(i).destination.hasOwnProperty(destination_type) && destination_type.toString().match('destination.*')) {
                                                    linkedURL = myHyperlinks.item(i).destination[destination_type] || "";
                                                    break;
                                                }
                                            }
                                
                                

                                 

                                I'm sure there's a better form by checking what type each destination is and calling the object accordingly, but it's late and I might fall asleep at any second!

                                 

                                Thank you for your suggestions!

                                • 14. Re: Viewing or printing a list of hyperlinks contained in a document
                                  gwhPoster Level 1

                                  I've just gotten around to testing the script out and have come across the error relating to a table of contents on line 13.

                                   

                                  ScreenShot.jpg

                                   

                                  I tried deleting the table of contents to see if the script would run but I'm still getting the error.

                                   

                                  The only change I made to the script was I changed this line from:

                                   

                                  var myFilePath = File.openDialog("Choose where to save your Hyperlinks"),

                                   

                                  to

                                   

                                  var myFilePath = File.saveDialog("Choose where to save your Hyperlinks"),

                                   

                                  As I'm not a coder, could someone tell me how to revise the script in order to overcome this error?

                                   

                                  Thanks again

                                  • 15. Re: Viewing or printing a list of hyperlinks contained in a document
                                    nukleas Level 1

                                    Hiya,

                                     

                                    I've updated the script on github: https://github.com/nukleas/indesign-incopy-snippets/blob/master/extractHyperlinks.js

                                     

                                    The fix ought to do it, here's the raw code (still haven't nixed the "use strict";s, but we're assuming they're harmless in this case:

                                     

                                     

                                    function extractHyperlinks(doc) {
                                        "use strict";
                                        var i,
                                     linkedText,
                                     linkedURL,
                                            extracted_hyperlinks = [],
                                            myHyperlinks = doc.hyperlinks,
                                            extracted_hyperlink,
                                            destination_type;
                                        for (i = 0; i < myHyperlinks.length; i += 1) {
                                     // If you don't do the following check to make sure the objects exist and are not null, the script will break when there isnt a source or destination available.
                                            if (myHyperlinks.item(i).source.sourceText && myHyperlinks.item(i).destination) {
                                                linkedText = myHyperlinks.item(i).source.sourceText.contents || ""; //extract linked text
                                                for (destination_type in myHyperlinks.item(i).destination) {
                                                    if (myHyperlinks.item(i).destination.hasOwnProperty(destination_type) && destination_type.toString().match('destination.*')) {
                                                        linkedURL = myHyperlinks.item(i).destination[destination_type] || "";
                                                        break;
                                                    }
                                                }
                                            }
                                     extracted_hyperlink = {
                                     text: linkedText,
                                     URL: linkedURL
                                     };
                                            extracted_hyperlinks.push(extracted_hyperlink);
                                        }
                                        return extracted_hyperlinks;
                                    }
                                    
                                    
                                    function writeHyperlinksToFile(hyperlinks) {
                                        "use strict";
                                        var myFilePath = File.saveDialog("Choose where to save your Hyperlinks"),
                                            outputFile = new File(myFilePath),
                                            i;
                                        outputFile = new File(outputFile);
                                        outputFile.open('w');
                                        for (i = 0; i < hyperlinks.length; i += 1) {
                                            outputFile.writeln(hyperlinks[i].text + "\t" + hyperlinks[i].URL);
                                        }
                                        outputFile.close();
                                    }
                                    function main() {
                                        "use strict";
                                        var extracted_hyperlinks = extractHyperlinks(app.documents[0]);
                                        writeHyperlinksToFile(extracted_hyperlinks);
                                    }
                                    main();
                                    
                                    
                                    • 16. Re: Viewing or printing a list of hyperlinks contained in a document
                                      Laubender Adobe Community Professional & MVP

                                      @gwhPoster – a table is just one out of a few other things the script can break.

                                       

                                      Try the following and change line 13 (of the original script posted in answer #3) to:

                                       

                                      try{
                                      linkedURL = myHyperlinks.item(i).destination.destinationURL || ""; // extract destination URL
                                      }catch(e){};
                                      


                                      On error the script will silently go on. However, all entries in a table of contents will be in the text file together with the page numbers. Some other entries related to Hyperlinks also.

                                       

                                      Uwe

                                      • 17. Re: Viewing or printing a list of hyperlinks contained in a document
                                        Laubender Adobe Community Professional & MVP

                                        @nukleas – our posts just crossed…

                                        Did not think you are still awake.

                                         

                                        @gwhPoster – first try the new script by nukleas.

                                         

                                        Uwe

                                        • 18. Re: Viewing or printing a list of hyperlinks contained in a document
                                          gwhPoster Level 1

                                          Thanks to you both - the new full script works great.

                                           

                                          I noticed that it includes cross-references in the results which are not needed but they can easily be deleted if I open up the file in excel and deletete the unwanted rows.

                                           

                                          Since a specific hyperlink can occur multiple times in the one chapter, after I had gone through and checked the syntax, I had planned to do a find/change by searching for the hyperlink source text but is it possible to change to the actual hyperlink url when I do the find/change or would I just be changing to the source text and not the actual url?

                                          • 19. Re: Viewing or printing a list of hyperlinks contained in a document
                                            gwhPoster Level 1

                                            I just found the following script which looks like it will do a find/change for hyperlinks:

                                             

                                            http://in-tools.com/downloads/indesign/scripts/ReplaceHyperlinkUrlValues.jsx

                                             

                                            Hope it helps someone-else also.

                                            • 20. Re: Viewing or printing a list of hyperlinks contained in a document
                                              Trevorׅ Adobe Community Professional

                                              A different approach

                                              Makes a new document with a table of the open documents URL hyperlinks.

                                              The new document can be exported to a pdf and the hyperlinks tested.

                                               

                                              Example of produced new document table.

                                              _Untitled-207 @ 75%_2013-05-22_19-21-45.png

                                               

                                              // Makes table of  open documents URL hyplerlinks in a new document by Trevor
                                              // http://forums.adobe.com/thread/1216355?tstart=0
                                              app.panels.itemByName("$ID/Hyperlinks").visible = 0;
                                              app.doScript("main()", ScriptLanguage.JAVASCRIPT, undefined, UndoModes.FAST_ENTIRE_SCRIPT, "Made Hyperlinks table");
                                              app.panels.itemByName("$ID/Hyperlinks").visible = 1;
                                              function main()
                                                  {
                                                      var doc = app.activeDocument,
                                                             myHyperlinks =doc.hyperlinks.everyItem().getElements(),
                                                             l = myHyperlinks.length, 
                                                             d = setupNewDoc(), newDoc = d.document, tf = d.textFrame, ps = tf.parentStory,
                                                             h, URL, hd, ns, hd;
                                                      myHyperlinks.reverse();
                                                      while (l--)
                                                          {
                                                              h = myHyperlinks[l];
                                                              URL = (h.destination == '[object HyperlinkURLDestination]') ? h.destination.destinationURL : "HYPERLINK TYPE IS " + h.destination;
                                                              ps.insertionPoints[0].contents = "URL: " + URL + "\r";
                                                              hyperSource = (h.source.sourceText) ? h.source.sourceText.contents : "--- NO SOURCE ---"
                                                              if (h.destination == '[object HyperlinkURLDestination]')
                                                                  {
                                                                      hs = ps.characters.itemByRange (5, 5 + URL.length-1);
                                                                      hs.appliedCharacterStyle = "Hyperlink Blue";
                                                                      hd = newDoc.hyperlinkURLDestinations.add({destinationURL: hs.contents.toString()});
                                                                      hys = newDoc.hyperlinkTextSources.add(hs);
                                                                      nh = newDoc.hyperlinks.add(hys, hd, {name: h.name});
                                                                  }
                                                              ps.insertionPoints[0].contents =  h.name + "\t" + hyperSource + "\t";
                                                          }
                                                        ps.insertionPoints[0].contents = "NAME\tSOURCE\tURL\r";
                                                        ps.texts[0].length && ps.texts[0].convertToTable ();
                                                        addPages(newDoc, tf, tf.geometricBounds);
                                              
                                                  function setupNewDoc ()
                                                      {
                                                          var  nd = app.documents.add({documentPreferences:{pagesPerDocument:  1, facingPages: 0}}),
                                                                  m = nd.pages[0].marginPreferences,
                                                                  tb = nd.pages[0].bounds;
                                                                  tb[0] += m.top; tb[1]+=m.left; tb[2]-=m.bottom; tb[3]-=m.right;            
                                                          var  tf = nd.pages[0].textFrames.add({geometricBounds:tb}),
                                                                 Blue = nd.colors.add ({space: ColorSpace.RGB, colorValue:[0, 0, 255], name:"Hyperlink Blue"}),
                                                                 cs = nd.characterStyles.add ({name: "Hyperlink Blue", underline : 1, fillColor: Blue});
                                                          return {
                                                              document : nd,
                                                              textFrame : tf,
                                                              };
                                                       }
                                              
                                              
                                              function addPages(theDoc, ptf /* Textframe */ ,gb /*geometricBounds*/)
                                                  {
                                                      if (!ptf.overflows) return;
                                                      np = theDoc.pages.add();
                                                      ptf = np.textFrames.add({geometricBounds:gb, previousTextFrame: ptf});
                                                      if (ptf.overflows) addPages(theDoc, ptf, gb);
                                                  }
                                              }
                                              

                                               

                                              Regards

                                               

                                              Trevor

                                               

                                              P.s. I'm still waiting for someone to test my mac saveas dialog

                                              • 21. Re: Viewing or printing a list of hyperlinks contained in a document
                                                gwhPoster Level 1

                                                Sorry to come back to this post. The script is still working as intended but I'm having a bit of a problem. Within my hyperlinks panel there are upwards of 12 to 15 hyperlinks that are a light grey colour. When I click on one of these links and press the "go to source of selected hyperlink" arrow at the bottom of the panel, nothing happens. I'm assuming that these links used to have source text but the text must have been deleted in an earlier version.

                                                 

                                                When I run the script though, it's including these "orphaned" hyperlinks as part of the results, ie listing both the source text which as I said isn't in the document, and also the destination url.

                                                 

                                                So this is quite confusing. Firstly, if the source text isn't in the document why is the url still appearing in the hyperlinks panel. Secondly, even if it's in the hyperlinks panel, how is the script able to list the hyperlink source text if I can't even get to it in the document!

                                                 

                                                Very baffling.

                                                 

                                                Just wondered if anyone knew what was going on.

                                                 

                                                Appreciate any further help.

                                                • 22. Re: Viewing or printing a list of hyperlinks contained in a document
                                                  Laubender Adobe Community Professional & MVP

                                                  @gwhPoster – as stated here:

                                                   

                                                  http://forums.adobe.com/message/5356216#5356216

                                                   

                                                  there is the possibility, that a script is just deleting the destination of a hyperlink, leaving the hyperlink intact. It's property destination is then set to "null".

                                                   

                                                  To get rid of ALL hyperlinks with destination set to "null", you could run the following snippet:

                                                   

                                                  var d=app.documents[0];
                                                  
                                                  alert("Hyperlink length BEFORE: "+d.hyperlinks.length);
                                                  
                                                  for(var n=d.hyperlinks.length-1;n>=0;n--){
                                                      if(d.hyperlinks[n].destination === null){
                                                          d.hyperlinks[n].remove();
                                                          };
                                                      };
                                                  
                                                  alert("Hyperlink length AFTER: "+d.hyperlinks.length);
                                                  

                                                   

                                                  You'll get an alert before and after the loop about the count of the hyperlinks in your document…

                                                   

                                                  Uwe

                                                  • 23. Re: Viewing or printing a list of hyperlinks contained in a document
                                                    gwhPoster Level 1

                                                    Thanks for the reply Uwe,

                                                     

                                                    You mentioned about the possibility of script deleting the destination of a hyperlink. Are you saying that I must have run a script at some point and that this script is what caused these "orphaned" hyperlinks?

                                                     

                                                    It's not the destination that is missing though, it's the source text, ie. the text within the document that you click in order to go to the destination so I'm still not sure what's happening.

                                                    • 24. Re: Viewing or printing a list of hyperlinks contained in a document
                                                      Laubender Adobe Community Professional & MVP

                                                      @gwPoster – no, I did not say such a specific thing.
                                                      And, if you are not sure what has happened, how could I instead ;-)

                                                       

                                                      Without inspecting the InDesign file(s) that are affected, one can only suggest a few things.
                                                      And it's possible that the real problems behind are never hit…

                                                      Uwe

                                                      • 25. Re: Viewing or printing a list of hyperlinks contained in a document
                                                        gwhPoster Level 1

                                                        Ok fair enough. So will the code snippet that you supplied remove the "orphaned" links from the panel? The code seems to target hyperlinks without a destination whereas my links have a destination, just not a source.

                                                         

                                                        If yes, do I save the code as a .jsx file and use the scripts panel to launch it? Sorry, my coding is very limited.

                                                        • 26. Re: Viewing or printing a list of hyperlinks contained in a document
                                                          Laubender Adobe Community Professional & MVP

                                                          @gwhPoster – the snippet removes hyperlinks without destinations.
                                                          So you cannot use it for your destinations without a source.

                                                           

                                                          You could try  (CAUTION. I did NOT test that yet !! ) :

                                                           

                                                          source === null

                                                           

                                                          instead of

                                                           

                                                          destination === null

                                                           

                                                          in my snippet. Test that on a duplicate of your InDesign file…

                                                           

                                                          Uwe