5 Replies Latest reply on Jan 7, 2014 10:53 AM by Soteriologist

    Find and replace text in hyperlinks

    Soteriologist

      I want to find and replace text inside all of the hyperlinks inside my documents.  What property of the hyperlink elements is the hyperlink text stored in?

       

      I know the text that the hyperlink is applied is stored in: myHyp[len].source.sourceText.contents

       

      But where is the hyperlink itself stored?

       

      Say I have a bunch of hyperlinks that look like: "https://www.website.com/subsite1/page1.html?id=123"

      I want to find and replace "subsite1" with "subsite2" so all the links change to "https://www.website.com/subsite2/page1.html?id=123"

       

      Thank in advance for all of the help.

        • 1. Re: Find and replace text in hyperlinks
          Jump_Over Level 5

          Hi,

           

          edit myHyp[len].destination.destinationURL

           

          In your case:

          // ...............
          var
               myDest = myHyp.destination.destinationURL, 
               myDest = myDest.replace("subsite1","subsite2");
          
          myHyp.destination.destinationURL = myDest;
          // ...............
          

           

          Jarek

          • 2. Re: Find and replace text in hyperlinks
            Soteriologist Level 1

            You're amazing as always my friend!

             

            Here's my final code for anyone who's interested:

             

             

            main();
            
            function main() {
                var myDialog = app.dialogs.add({name: "Find and replace the user submitted text in all hyperlinks found in this document.", canCancel: true});
                with(myDialog) {
                    with(dialogColumns.add()) {
                        with(borderPanels.add()) {
                            staticTexts.add({staticLabel: "Find:"});
                            var myFindBox = textEditboxes.add({minWidth:200});
                            staticTexts.add({staticLabel: "& Replace with:"});
                            var myReplaceBox = textEditboxes.add({minWidth:200});
                        }
                    }
                }
            
                if (myDialog.show() == true) {
                    var myDocument = app.activeDocument,
                            myFindVal = myFindBox.editContents,
                            myReplaceVal = myReplaceBox.editContents,
                            noneStyle = myDocument.characterStyles.item(0),
                            myHyp = myDocument.hyperlinks.everyItem().getElements(),
                            len = myHyp.length,
                            myCount = 0;
            
                    while (len-->0)
                        if (myHyp[len].destination.hasOwnProperty("destinationURL") && myHyp[len].destination.destinationURL.length > 0) {
                            var myDest = myHyp[len].destination.destinationURL;
                            myDest = myDest.replace(myFindVal,myReplaceVal);
                            myHyp[len].destination.destinationURL = myDest;
                            myCount++;
                        }
            
                    alert("Done! Found '" + myFindVal + "' in this document's hyperlinks, and replaced with '" + myReplaceVal + "', " + myCount + " times.");
                }
                myDialog.destroy();
            }
            
            • 3. Re: Find and replace text in hyperlinks
              Soteriologist Level 1

              I'm getting a JavaScript Error! sometimes when I run my code.

               

              Error Number: 21

              Error String: null is not an object

               

              Source: if (myHype[len].destination.hasOwnProperty("destinationURL")

               

               

               

               

              What's the best way for me to check if the hyperlinks have destinationURLs that aren't null and are longer than 0?  I only want to run my code on valid hyperlinks of course.

               

              Thank you in advance for all of your help.

              • 4. Re: Find and replace text in hyperlinks
                Jump_Over Level 5

                Hi,

                 

                I would add to the "if" statement:

                if (
                     myHyp[len].destination &&
                     myHyp[len].destination.hasOwnProperty("destinationURL") &&
                     myHyp[len].destination.destinationURL.length > 0 
                     ) {
                //.....
                }   
                

                 

                Jarek

                1 person found this helpful
                • 5. Re: Find and replace text in hyperlinks
                  Soteriologist Level 1

                  Again, you're awesome mate!

                   

                  Thank you so much, that did the trick!

                   

                  Here's my final code:

                   

                   

                  main();
                  
                  function main() {
                      var myDialog = app.dialogs.add({name: "Find and replace the user submitted text in all hyperlinks found in this document.", canCancel: true});
                      with(myDialog) {
                          with(dialogColumns.add()) {
                              with(borderPanels.add()) {
                                  staticTexts.add({staticLabel: "Find:"});
                                  var myFindBox = textEditboxes.add({minWidth:200});
                                  staticTexts.add({staticLabel: "& Replace with:"});
                                  var myReplaceBox = textEditboxes.add({minWidth:200});
                              }
                          }
                      }
                  
                      if (myDialog.show() == true) {
                          var myDocument = app.activeDocument,
                                  myFindVal = myFindBox.editContents,
                                  myReplaceVal = myReplaceBox.editContents,
                                  noneStyle = myDocument.characterStyles.item(0),
                                  myHyp = myDocument.hyperlinks.everyItem().getElements(),
                                  len = myHyp.length,
                                  myCount = 0;
                  
                          while (len-->0)
                              if (
                                  myHyp[len].destination &&
                                  myHyp[len].destination.hasOwnProperty("destinationURL") &&
                                  myHyp[len].destination.destinationURL.length > 0 
                              ) {
                                  var myDest = myHyp[len].destination.destinationURL;
                                  myDest = myDest.replace(myFindVal,myReplaceVal);
                                  myHyp[len].destination.destinationURL = myDest;
                                  myCount++;
                              }
                  
                          alert("Done! Found '" + myFindVal + "' in this document's hyperlinks, and replaced with '" + myReplaceVal + "', " + myCount + " times.");
                      }
                      myDialog.destroy();
                  }