10 Replies Latest reply: Feb 12, 2014 8:57 AM by Petteri_Paananen RSS

    Script for converting hyperlinks to buttons?

    Petteri_Paananen Community Member

      Hi!

       

      Does anyone know if there´s a script for converting hyperlinks to buttons with Go To URL action with same URL that was used with hyperlink?

        • 1. Re: Script for converting hyperlinks to buttons?
          Kasyan Servetsky Community Member

          Are you a scripter?

          I made such a script but it was intended for a specific workflow and can't be used as it is. You'll have to rework it to your needs or, at least, can use it as a starting point.

          If you're interested, I'll post it.

           

          Kas

          • 2. Re: Script for converting hyperlinks to buttons?
            Petteri_Paananen Community Member

            Hi

             

            I´m not scripter but I have edited scripts so I could give it a change if you were so kind to send that script to me....=) And I have associates who are more experienced than I am, so I can even ask help....

             

            Thanks a lot!

            • 3. Re: Script for converting hyperlinks to buttons?
              Kasyan Servetsky Community Member

              Here it is:

               

              /* Copyright 2012, Kasyan Servetsky
              November 29, 2012
              Written by Kasyan Servetsky
              http://www.kasyan.ho.com.ua
              e-mail: askoldich@yahoo.com */
              //======================================================================================
              var scriptName = "Convert hyperlinks to buttons - 1.0";
              
              Main();
              
              //===================================== FUNCTIONS  ======================================
              function Main() {
                  var hyperlink, source, sourceText, destination, page, arr, outlinedText, gb, button, behavior,
                  barodeCount = 0,
                  hypCount = 0;
                  if (app.documents.length == 0) ErrorExit("Please open a document and try again.", true);
                  var startTime = new Date();
                  
                  var doc = app.activeDocument;
                  var layer = doc.layers.item("Buttons");
                  var swatch = doc.swatches.item("RGB Yellow");
                  var hyperlinks = doc.hyperlinks;
                  
                  var progressWin = new Window ("window", scriptName);
                  progressBar = progressWin.add ("progressbar", undefined, 0, undefined);
                  progressBar.preferredSize.width = 450;
                  progressTxt = progressWin.add("statictext", undefined,  "Starting processing hyperlinks");
                  progressTxt.preferredSize.width = 400;
                  progressTxt.preferredSize.height = 30;
                  progressTxt.alignment = "left";
                  progressBar.maxvalue = hyperlinks.length;
                  progressWin.show();
                  
                  for (var i = hyperlinks.length-1; i >= 0; i--) {
                      hyperlink = hyperlinks[i];
                      source = hyperlink.source;
                      sourceText = source.sourceText;
                      destination = hyperlink.destination;
                      page = sourceText.parentTextFrames[0].parentPage;
                      
                      barodeCount++;
                      progressBar.value = barodeCount;
                      progressTxt.text = "Processing hyperlink " + hyperlink.name + " (Page - " + page.name + ")";
                      
                      arr = sourceText.createOutlines(false);
                      outlinedText = arr[0];
                      gb = outlinedText.geometricBounds;
                      outlinedText.remove();
                      
                      button = page.buttons.add(layer, {geometricBounds: gb, name: hyperlink.name});
                      button.fillColor = swatch;
                      button.fillTint = 50;
                      button.groups[0].transparencySettings.blendingSettings.blendMode = BlendMode.MULTIPLY;    
                      behavior = button.gotoURLBehaviors.add();
                      behavior.url = destination.destinationURL;
                      
                      hyperlink.remove();
                      source.remove();
                      
                      hypCount++;
                  }
                  
                  var endTime = new Date();
                  var duration = GetDuration(startTime, endTime);
                  progressWin.close();
              
                  alert("Finished. " + hypCount + " hyperlinks were convertted to buttons.\n(time elapsed: " + duration + ")", scriptName);
              
              }
              //--------------------------------------------------------------------------------------------------------------------------------------------------------
              function GetDuration(startTime, endTime) {
                  var str;
                  var duration = (endTime - startTime)/1000;
                  duration = Math.round(duration);
                  if (duration >= 60) {
                      var minutes = Math.floor(duration/60);
                      var seconds = duration - (minutes * 60);
                      str = minutes + ((minutes != 1) ? " minutes, " :  " minute, ") + seconds + ((seconds != 1) ? " seconds" : " second");
                      if (minutes >= 60) {
                          var hours = Math.floor(minutes/60);
                          minutes = minutes - (hours * 60);
                          str = hours + ((hours != 1) ? " hours, " : " hour, ") + minutes + ((minutes != 1) ? " minutes, " :  " minute, ") + seconds + ((seconds != 1) ? " seconds" : " second");
                      }
                  }
                  else {
                      str = duration + ((duration != 1) ? " seconds" : " second");
                  }
              
                  return str;
              }
              //--------------------------------------------------------------------------------------------------------------------------------------------------------
              function ErrorExit(error, icon) {
                  alert(error, scriptName, icon);
                  exit();
              }
              
              • 4. Re: Script for converting hyperlinks to buttons?
                Petteri_Paananen Community Member

                Thanks a lot, this works just fine... I changed the RGB Yellow to None and button name to be simply link... then it works for my purpose...

                I probaply remove requirement for layer named Buttons too... or at least I add there few lines which generate that required layer on the fly...

                 

                Petteri

                • 5. Re: Script for converting hyperlinks to buttons?
                  Petteri_Paananen Community Member

                  One more question.. sorry...=)

                   

                  I ran some tests and noticed that only text hyperlinks are supported... do you have any tip how it could be possible to get image/graphic hyperlinks supported as well.. if it´s easy enough I could try to do it myself....

                   

                  Petteri

                  • 6. Re: Script for converting hyperlinks to buttons?
                    Kasyan Servetsky Community Member

                    Hi Petteri,

                     

                    Here's quick & dirty solution (totally untested). I assume that images are simply placed in their containers (e.g. a rectangle, oval, etc. — not in a group or nested).

                    Here I posted a sample document so you could test the script against it.

                    Currently the script works with two types of hyperlinks: which have either HyperlinkTextSource or HyperlinkPageItemSource as source. But if necessary, other types can be easily added.

                     

                    /* Copyright 2014, Kasyan Servetsky
                    February 3, 2014
                    Written by Kasyan Servetsky
                    http://www.kasyan.ho.com.ua
                    e-mail: askoldich@yahoo.com */
                    //======================================================================================
                    var scriptName = "Convert hyperlinks to buttons - 2.0",
                    doc;
                    
                    Main();
                    
                    //===================================== FUNCTIONS  ======================================
                    function Main() {
                        var hyperlink, source, sourceText, destination, page, arr, outlinedText, gb, button, behavior, sourcePageItem,
                        barodeCount = 0,
                        hypCount = 0;
                        if (app.documents.length == 0) ErrorExit("Please open a document and try again.", true);
                        var startTime = new Date();
                        
                        doc = app.activeDocument;
                        var layer = MakeLayer("Buttons");
                        var swatch = MakeColor("RGB Yellow", ColorSpace.RGB, ColorModel.process, [255, 255, 0]);
                        var hyperlinks = doc.hyperlinks;
                        
                        var progressWin = new Window ("window", scriptName);
                        progressBar = progressWin.add ("progressbar", undefined, 0, undefined);
                        progressBar.preferredSize.width = 450;
                        progressTxt = progressWin.add("statictext", undefined,  "Starting processing hyperlinks");
                        progressTxt.preferredSize.width = 400;
                        progressTxt.preferredSize.height = 30;
                        progressTxt.alignment = "left";
                        progressBar.maxvalue = hyperlinks.length;
                        progressWin.show();
                        
                        for (var i = hyperlinks.length-1; i >= 0; i--) {
                            hyperlink = hyperlinks[i];
                            source = hyperlink.source;
                            destination = hyperlink.destination;
                            
                            if (source.constructor.name == "HyperlinkTextSource") {
                                sourceText = source.sourceText;
                                page = sourceText.parentTextFrames[0].parentPage;
                                arr = sourceText.createOutlines(false);
                                outlinedText = arr[0];
                                gb = outlinedText.geometricBounds;
                                outlinedText.remove();            
                            }
                            else if (source.constructor.name == "HyperlinkPageItemSource") {
                                sourcePageItem = source.sourcePageItem;
                                gb = sourcePageItem.geometricBounds;
                                page = sourcePageItem.parentPage;
                            }
                            
                            barodeCount++;
                            progressBar.value = barodeCount;
                            progressTxt.text = "Processing hyperlink " + hyperlink.name + " (Page - " + page.name + ")";
                            
                            if (source.constructor.name == "HyperlinkTextSource" || source.constructor.name == "HyperlinkPageItemSource") {
                                button = page.buttons.add(layer, {geometricBounds: gb, name: hyperlink.name});
                                button.fillColor = swatch;
                                button.fillTint = 50;
                                button.groups[0].transparencySettings.blendingSettings.blendMode = BlendMode.MULTIPLY;    
                                behavior = button.gotoURLBehaviors.add();
                                behavior.url = destination.destinationURL;
                                
                                hyperlink.remove();
                                source.remove();
                                
                                hypCount++;
                            }
                        }
                        
                        var endTime = new Date();
                        var duration = GetDuration(startTime, endTime);
                        progressWin.close();
                    
                        alert("Finished. " + hypCount + " hyperlinks were convertted to buttons.\n(time elapsed: " + duration + ")", scriptName);
                    
                    }
                    //--------------------------------------------------------------------------------------------------------------------------------------------------------
                    function GetDuration(startTime, endTime) {
                        var str;
                        var duration = (endTime - startTime)/1000;
                        duration = Math.round(duration);
                        if (duration >= 60) {
                            var minutes = Math.floor(duration/60);
                            var seconds = duration - (minutes * 60);
                            str = minutes + ((minutes != 1) ? " minutes, " :  " minute, ") + seconds + ((seconds != 1) ? " seconds" : " second");
                            if (minutes >= 60) {
                                var hours = Math.floor(minutes/60);
                                minutes = minutes - (hours * 60);
                                str = hours + ((hours != 1) ? " hours, " : " hour, ") + minutes + ((minutes != 1) ? " minutes, " :  " minute, ") + seconds + ((seconds != 1) ? " seconds" : " second");
                            }
                        }
                        else {
                            str = duration + ((duration != 1) ? " seconds" : " second");
                        }
                    
                        return str;
                    }
                    //--------------------------------------------------------------------------------------------------------------------------------------------------------
                    function MakeLayer(name, layerColor) {
                        var layer = doc.layers.item(name);
                        if (!layer.isValid) {
                            layer = doc.layers.add({name: name});
                            if (layerColor != undefined) layer.layerColor = layerColor;
                        }
                        return layer;
                    }
                    //--------------------------------------------------------------------------------------------------------------------------------------------------------
                    function MakeColor(colorName, colorSpace, colorModel, colorValue) {
                        var doc = app.activeDocument;
                        var color = doc.colors.item(colorName);
                        if (!color.isValid) {
                            color = doc.colors.add({name: colorName, space: colorSpace, model: colorModel, colorValue: colorValue});
                        }
                        return color;
                    }
                    //--------------------------------------------------------------------------------------------------------------------------------------------------------
                    function ErrorExit(error, icon) {
                        alert(error, scriptName, icon);
                        exit();
                    }
                    //--------------------------------------------------------------------------------------------------------------------------------------------------------
                    
                    • 7. Re: Script for converting hyperlinks to buttons?
                      Petteri_Paananen Community Member

                      Hei this is absolutely GREAT! Thank you million times Kasyan...=)

                       

                      Petteri

                      • 8. Re: Script for converting hyperlinks to buttons?
                        Arvindkmar Community Member

                        Hi Kasyan,

                         

                        Thank you very much for your reply. I am not a scriptor. I am just a layout operator in Indesign. I have tested both the scripts provided by you, they are working but unfortunately I cannot use it.

                         

                        I want to request you if you could help me. I have a URL Character style in my book, according to your script "Make hyperlinks from URLs - 1.0" made on "September 12, 2013" it is find all the URL but the script is changing the original text of the book and replacing with the domain name.

                         

                        I just want the script to find the URL style and convert it to Hyperlink.

                         

                        For example this is the actual URL in the book:

                         

                        https://developer.atlassian.com/display/DOCS/ConfigureIDEAtousethe+SDK

                         

                        ORIGINAL text changed to when I run the script:

                         

                        developer.atlassian.com

                         

                        When I run the script the original text in the book is changed to the Domain name, and also Hyperlink is created for the full original text in PDF.

                         

                         

                         

                        Its appreciated that you take the initiative and reply to our queries.

                        • 9. Re: Script for converting hyperlinks to buttons?
                          patrickbcm Community Member

                          Hello,

                           

                          I have another question. I make Apps with adobe DPS and when i have a open Indesign document it will work, but sometime i get a magazine as PDF.

                          I import the pdf in Indesign CS5 Mac.

                          Is it also possible to fix this script for pdf document?

                           

                          Kind regards,

                          Patrick

                          • 10. Re: Script for converting hyperlinks to buttons?
                            Petteri_Paananen Community Member

                            This is interesting.

                             

                            I have to deal with placed PDFs as well. As far as I know, InDesign treats placed PDFs a bit like images so I would be greatly suprised if it turned out to be possible to read hyperlink data from them and re-generate native InDesign hyperlinks or buttons based on that information.

                             

                            I really hope I´m wrong tough...