6 Replies Latest reply on Oct 3, 2013 3:15 AM by BEGINNER_X

    Problem in create Hyperlink

    BEGINNER_X Level 3

      Hi All,

       

      I create hyperlink by finding http and www in my active document.                //Works well

       

      But the problem is, if the url or websites contents coming twice or above then I found error   //not able to create hyperlink source with the same name

       

      i.e., www.gmail.com (comes 2 or more time in the document, then the below code is not working fine)

       

       

      var myDoc = app.activeDocument;
      app.findGrepPreferences = app.changeGrepPreferences = null;
      //Find http and www
      app.findGrepPreferences.findWhat = "(?i)(http|www)\\S+[\\l\\u|\\d]"
      var myFound1 = myDoc.findGrep();
      count = 0
      for(k=0; k<myFound1.length; k++)
      {
          var myFind = myFound1[k];
          var myHyperlinkSource = app.activeDocument.hyperlinkTextSources.add(myFind)
          var myHyperlinkURLDestination = app.activeDocument.hyperlinkURLDestinations.add(myFind.contents)
          var myHyperlink = app.activeDocument.hyperlinks.add(myHyperlinkSource, myHyperlinkURLDestination, {name: myFind.contents})
      //    var myHyperlink = app.activeDocument.hyperlinks.add(myHyperlinkSource, myHyperlinkURLDestination, {name: myFind.contents+count++})
      }
      

       

      Please find the screenshot for your more references.

       

      hyperlink.png

      Please help to find the solution.

       

      Thanks in advance

      Beginner_X

        • 1. Re: Problem in create Hyperlink
          TᴀW Adobe Community Professional & MVP

          Try:

           

          var myFound1 = myDoc.findGrep()[0];

           

          Ariel

          • 2. Re: Problem in create Hyperlink
            Kasyan Servetsky Level 5

            I solved this problem by adding an incrementing number in round brackets if a hyperlink name is already taken.

            Another (simpler) solution is to put "add hyperlink" line into try-catch block, but in this case you'll have a default name if the error occurs.

            Here's the function:

             

            function MakeHyperlink(text, url) {
                if (url.match(/https?:\/\//) == null) {
                    url = "http://" + url;
                }
            
                url = url.replace(/\.$/, "");
                
                var name = text.contents;
                
                var oriName = name;
            
                if (doc.hyperlinks.itemByName(name) != null) {
                    var increment = 1;
                    while (doc.hyperlinks.itemByName(name) != null) {
                        name = oriName + " (" + increment++ + ")";
                    }
                }
                
                try {
                    var dest = doc.hyperlinkURLDestinations.itemByName(name);
                    if (!dest.isValid) {
                        dest = doc.hyperlinkURLDestinations.add(url , {hidden: true});
                    }
                    var source = doc.hyperlinkTextSources.add(text);
                    var hyperlink = doc.hyperlinks.add(source, dest, {highlight: HyperlinkAppearanceHighlight.NONE, visible: false});
                    
                    try {
                        hyperlink.name = name;
                    }
                    catch(err) {
                        //$.writeln(err.message + ", line: " + err.line);
                    }
                    
                    if (hyperlink.isValid) {
                        hypCount++;
                        if (set.tempColors) source.sourceText.fillColor = swatchOK;
                        if (set.charStyle) source.appliedCharacterStyle = charStyle;
                    }
                }
                catch(err) {
                    errorsCount++;
                    
                    var swatchProblem = MakeSwatch("===== PROBLEM =====", {
                        model : ColorModel.PROCESS,
                        space : ColorSpace.RGB,
                        colorValue : [255, 0, 0]
                        });
                    
                    text.fillColor = swatchProblem;
                    
                    //$.writeln("Error message: " + err.message + ", Line: " + err.line + ", URL: " + url);
                    arr.push("Error message: " + err.message + ", Line: " + err.line + ", URL: " + url);        
                }
            }
            
            1 person found this helpful
            • 3. Re: Problem in create Hyperlink
              tobias.wantzen Level 1

                  var myHyperlink = app.activeDocument.hyperlinks.add(myHyperlinkSource, myHyperlinkURLDestination, {name: myFind.contents});

              //    var myHyperlink = app.activeDocument.hyperlinks.add(myHyperlinkSource, myHyperlinkURLDestination, {name: myFind.contents+count++})
              ;

               

              This should be the right approach: the name must be unique. Doesn’t the commented line instead of the line directly before it work? Alternatively you could simply do something like this:

              var myHyperlink = app.activeDocument.hyperlinks.add(myHyperlinkSource, myHyperlinkURLDestination, {name: "hyperlink_"+String(count++)});


              Tobias

              1 person found this helpful
              • 4. Re: Problem in create Hyperlink
                BEGINNER_X Level 3

                Hi Master Kasyan and Master Ariel,

                 

                Could you please help, still I not found solution.

                 

                Where can I add increment:

                 

                //    var myHyperlink = app.activeDocument.hyperlinks.add(myHyperlinkSource, myHyperlinkURLDestination, {name: myFind.contents+count++})   //not works good

                 

                 

                Use this code also:

                 

                var myFound1 = myDoc.findGrep()[0];

                 

                But still error comes.

                 

                Thanks in advance

                Beginner_X

                 


                • 5. Re: Problem in create Hyperlink
                  tobias.wantzen Level 1

                  What do you mean by »// not works good« - what happens?

                   

                  Kasyan kindly supported us with a complete function from his code library. Have you tried to use his function instead of yours?

                   

                  Tobias

                  • 6. Re: Problem in create Hyperlink
                    BEGINNER_X Level 3

                    Hi Kasyan, Ariel, tobias,

                     

                    Thanks for your support.

                     

                    I tried with my code only.

                     

                    The problem is in my testing file only.

                     

                    After that I try with another file it works fine.

                     

                    Again thanks for your valuable time and support.

                     

                    Thanks

                    Beginner_X