2 Replies Latest reply on Aug 30, 2018 1:37 AM by [Jongware]

    How do i convert long urls to short text links to save space?

    mattt57341061

      I've got a file that I'm working on and in that file there is a table with a one column that is all urls. These urls take up too much space so i wanted to change them to something short like "Click here for link" I know how to do it on a small scale with Edit Hyperlink option, but I've got about 300 links i need to do this to and i'm trying to save myself some work.

       

      So essebtially what i want to do is change... https://www.thislinkiswaytolongandtakesuptomuchspace.com to something like Click here for link but i need to do that on a large scale.

       

      Thanks

        • 1. Re: How do i convert long urls to short text links to save space?
          yadavs92328 Level 2

          You can try this script and modify according to your need,

          var myDoc = app.documents[0];
          //you can store your urls which are present in document using grep search or text search
          var myLink = ["www.firstURL.com", "www.secondURL.com"];
          // And store how link should show in text as per your need
          var showText = ["click here for link", "click here for link"];
          for(var i = 0; i < myLink.length; i++){
              app.findTextPreferences = NothingEnum.nothing;
              app.changeTextPreferences = NothingEnum.nothing;
              app.findTextPreferences.findWhat = myLink[i];
              app.changeTextPreferences.changeTo = showText[i]
              myDoc.changeText();
              app.findTextPreferences = NothingEnum.nothing;
              app.changeTextPreferences = NothingEnum.nothing;
              app.findTextPreferences.findWhat = showText[i];
              var found = myDoc.findText();
              var counter = 0;
              var done = false;
              do{
                  try{
                      var myHyperlinkSource = app.activeDocument.hyperlinkTextSources.add(found[counter]);
                      done = true;
                      }
                  catch(e){
                      counter++;
                      done = false;
                      }
                  }
              while(done==false)
              var myHyperlinkURLDestination = app.activeDocument.hyperlinkURLDestinations.add(""+myLink[i]);
              var myHyperlink = app.activeDocument.hyperlinks.add(myHyperlinkSource, myHyperlinkURLDestination, {name: (found[counter].contents+"_"+counter)});
              }
          
          • 2. Re: How do i convert long urls to short text links to save space?
            [Jongware] Most Valuable Participant

            According to the Object Model, the actual text that is the hyperlink "button" is pointed to by a HyperlinkTextSource property sourceText. So this is all it takes:

             

            for (i=0; i<app.activeDocument.hyperlinkTextSources.length; i++)
            {
             app.activeDocument.hyperlinkTextSources[i].sourceText.contents = "Click for link";
            }
            

             

            And in case you don't have a character style applied, you'd better do that right away:

             

            charstyle = app.activeDocument.characterStyles.item("link");
            for (i=0; i<app.activeDocument.hyperlinkTextSources.length; i++)
            {
            app.activeDocument.hyperlinkTextSources[i].sourceText.contents = "Click for link";
            app.activeDocument.hyperlinkTextSources[i].sourceText.appliedCharacterStyle = charstyle;
            }
            

             

            (Adjust the character style name "link" to your personal taste.)