9 Replies Latest reply on Jan 7, 2014 9:54 PM by Arvindkmar

    Script to search for text and remove hyperlink and characterstyle that is applied (return to normal)

    Soteriologist Level 1

      I need help with my script to search for text and remove hyperlink and characterstyle that is applied (return the text to normal).

       

      I can't get it to do either the way I want.  I want the user to be able to type into a dialog the text they want to search for, have it search for that text IGNORING case (case-insensitive) and then for all the occurences that it finds it, remove the character styles that are applied to it (set it to [None]) and remove the hyperlinks.

       

      My code seems to set the character style to [None], when I click on the text and look at the Character Styles Window, BUT all the old styles (font color, underline) are still applied to it and not stripped away as I want.

       

      Also, I can't seem to figure out how to remove all the old links.

       

      Here's my code:

       

      var myDocument = app.activeDocument;

      var noneStyle = myDocument.characterStyles.item("[None]");

      var myFindVal = "";

      var myErrorText = "";

      var myErrorCount = 0;

      var myDialog = app.dialogs.add({name: "Search for the user submitted text and remove hyperlinks from found items.", canCancel: true});

      with(myDialog) {

          with(dialogColumns.add()) {

              with(borderPanels.add()) {

                  staticTexts.add({staticLabel: "Search and remove hyperlinks for:"});

                  var myTextBox = textEditboxes.add({minWidth:100});

              }

          }

      }

      if (myDialog.show() == true) {

          myFindVal = myTextBox.editContents;

          doSearchAndReplace();

          app.findTextPreferences = NothingEnum.nothing;

          app.changeTextPreferences = NothingEnum.nothing;

          if (myErrorCount == 0) {

              alert("Done! Part hyperlinks have been removed for: '" + myFindVal + "'!");

          }

          else {

              alert("Last Error Message: '" + myErrorText + "' & Error Count: " + myErrorCount);

          }

      }

      myDialog.destroy();

      function doSearchAndReplace() {

          app.findTextPreferences.findWhat = myFindVal;

          app.findChangeTextOptions.caseSensitive = false;

          app.findChangeTextOptions.includeFootnotes = false;

          app.findChangeTextOptions.includeHiddenLayers = false;

          app.findChangeTextOptions.includeLockedLayersForFind = false;

          app.findChangeTextOptions.includeLockedStoriesForFind = false;

          app.findChangeTextOptions.includeMasterPages = false;

          app.findChangeTextOptions.wholeWord = false;

          var myFoundItems = myDocument.findText();

          for (i = 0; i < myFoundItems.length; i++) {

              myRemoveHyperlinkAndStyle(myFoundItems[i]);

          }

      }

      function myRemoveHyperlinkAndStyle(myFoundItem) {

          try {

              myDocument.hyperlinkTextDestinations.item(myFoundItem).remove();

              myDocument.hyperlinkTextSources.item(myFoundItem).remove();

              myDocument.hyperlinkURLDestinations.item(myFoundItem).remove();

             myFoundItem.applyCharacterStyle(noneStyle);

          }

          catch(myError) {

              myErrorCount++;

              myErrorText = myError;

          }

      }