6 Replies Latest reply on Aug 16, 2010 8:58 AM by grefel

    Replacing occurrences of a word by links

    BertrandRaphael

      Hello,

       

      I'm trying to write a Javascript script that would replace every occurrences of a given word in an Indesign CS3 document by a hyperlink pointing to a given URL.

       

      I'm very new to Indesign scripting, and I don't know where to start. I've tried searching this forum, but couldn't find the answer to my problem.

      If anyone can help...

       

      Thanks,

      Bertrand

        • 1. Re: Replacing occurrences of a word by links
          grefel Level 2

          You could use my findAndDo Script (blog is in german): http://www.indesignblog.com/?p=7

           

          for a hyperlink you need to change it:

           

          app.changeGrepPreferences = NothingEnum.nothing;
          app.findGrepPreferences = NothingEnum.nothing;
          app.findGrepPreferences.findWhat = "SEARCH";
          var erg = app.activeDocument.findGrep ();     
          
          for (var i = erg.length - 1; i >= 0; i--) {
              var _hlinkSource = app.activeDocument.hyperlinkTextSources.add(erg[i]);
              var _destination = app.activeDocument.hyperlinkURLDestinations.add("http://www.google.de");
              app.activeDocument.hyperlinks.add(_hlinkSource, _destination);
          }
          
          • 2. Re: Replacing occurrences of a word by links
            BertrandRaphael Level 1

            Thank you very much Grefel! Your script works perfectly and creates hyperlinks for all the occurrences of my word.

             

            However there are two more things I need : I'd like the hyperlink object to be invisible, and the text occurrence to appear in blue.

             

            I figured I could make the links invisible by using the line

             

            app.activeDocument.hyperlinks.add(_hlinkSource, _destination).visible=false;
            

             

            But I'm not sure how I can make the text turn blue. I suppose I have to use apply some function to erg[i] within the loop, is that right?

            • 3. Re: Replacing occurrences of a word by links
              [Jongware] Most Valuable Participant

              Best is to assign a character style on the links. Make one named "link", and specify your color in there. Then in the script, add this:

               

              erg[i].appliedCharacterStyle = app.activeDocuments.characterStyles.item("link");

              1 person found this helpful
              • 4. Re: Replacing occurrences of a word by links
                BertrandRaphael Level 1

                Excellent idea, thank you very much for all your help.

                • 5. Re: Replacing occurrences of a word by links
                  BertrandRaphael Level 1

                  One problem with that script is that you can't run it a second time to update the URLs of the hyperlinks. If I do I get a message:

                   

                  The object you have chosen is already in use by another hyperlink

                   

                  It seems like the error is caused by the following line:

                   

                  var _hlinkSource = app.activeDocument.hyperlinkTextSources.add(erg[i]);

                   

                  Is there a way to prevent that? For example, can the existing HyperlinkTextSource be removed? I couldn't find the existing  HyperlinkTextSource using app.activeDocument.hyperlinkTextSources.findByID( ), because I don't know its ID.

                   

                  Is there a way to remove or update the hyperlink knowing just erg[i]?

                  • 6. Re: Replacing occurrences of a word by links
                    grefel Level 2

                    hey,

                     

                    mmh, this looks more complex then expected. anyone out there who knows how to reference from a text object to a hyperlinkTextSource ???

                     

                    you can solve it dirty this way:

                    app.changeGrepPreferences = NothingEnum.nothing;
                    app.findGrepPreferences = NothingEnum.nothing;
                    app.findGrepPreferences.findWhat = "SEARCH";
                    var erg = app.activeDocument.findGrep ();    
                    
                    for (var i = erg.length - 1; i >= 0; i--) {
                        
                        // check for existing textSources: 
                        var _hlinkSource = null;
                        var _allHlts = app.activeDocument.hyperlinkTextSources.everyItem().getElements();
                        for (m =0; m < _allHlts.length; m++) {
                            var _hlts = _allHlts[m];
                            if (_hlts.sourceText === erg[i]) {
                                _hlinkSource = _hlts; 
                             }        
                        }    
                        // Only add if there is no textSource
                        if (_hlinkSource == null ) _hlinkSource = app.activeDocument.hyperlinkTextSources.add(erg[i]);
                    
                    
                        var _url =  "http://de.wikipedia.org/wiki/" + erg[i].contents
                        var _destination = app.activeDocument.hyperlinkURLDestinations.itemByName(_url);
                        // check if the destination already exists
                        if (_destination == null) app.activeDocument.hyperlinkURLDestinations.add(_url);
                            
                        var _hlink = app.activeDocument.hyperlinks.add(_hlinkSource);
                        try {
                            _hlink.name = _url;
                        } catch (e) {}
                        _hlink.destination = _destination;
                    }
                    

                     

                    this code looks pretty ugly, because it checks every hyperlinkTextSource against the found text object.

                     

                    another feasible way could be to remove all

                    hyperlinkTextSources, hyperlinkURLDestinations, hyperlinks

                    and run the script again.

                     

                    anyway: i hope someone else has a more elegant solution.