14 Replies Latest reply on Nov 16, 2016 6:43 AM by luizdsilveira

    Script for converting hyperlinks to buttons?

    Petteri_Paananen Level 4

      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 Level 5

          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 Level 4

            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 Level 5

              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 Level 4

                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 Level 4

                  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 Level 5

                    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();
                    }
                    //--------------------------------------------------------------------------------------------------------------------------------------------------------
                    
                    1 person found this helpful
                    • 7. Re: Script for converting hyperlinks to buttons?
                      Petteri_Paananen Level 4

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

                       

                      Petteri

                      • 8. Re: Script for converting hyperlinks to buttons?
                        Arvindkmar Level 1

                        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 Level 1

                          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 Level 4

                            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...

                            • 11. Re: Script for converting hyperlinks to buttons?
                              luizdsilveira Level 1

                              Hi.

                               

                              I read those messages and I know they are from some years ago, but I have a problem (that took me to here) and maybe you can help.

                               

                              I have a 100 pages magazine and need to make hyperlinks of some URLs in the middle of the text (no problem) and in some advertising pages too (PDFs). I'm using a script that automatically convert text to hyperlink using GREP, but it can do nothing to the PDF ads, so I do them manually.

                               

                              Today I was thinking in maybe include in my script something to take the URL for each ad and put it in a text frame, so the script will auto make it as a hyperlink. The problem is user will need to click direct in the text to make it work. Looking at your script, with this button over the image, I was thinking if there is no way to do that automatically. I don't know exactly, but maybe I can group my text frame with the URL for the ad with the ad and the script will take the whole group and make a big button over them.

                               

                              What do you think? Is there a way to do that?

                               

                              Thanks,

                              Luiz

                              • 12. Re: Script for converting hyperlinks to buttons?
                                Kasyan Servetsky Level 5

                                Hi Luiz,

                                My script -- in post 6 -- converts existing hyperlinks (both text and images) to buttons, like so:

                                Before

                                11-15-2016 11-50-45 PM.png

                                After

                                11-15-2016 11-51-03 PM.png

                                Image

                                11-15-2016 11-51-36 PM.png

                                Text

                                11-15-2016 11-52-15 PM.png

                                Here are the 'before and after' sample files (CC 2015.4) I just used for testing and making the screenshots above.

                                I am sure: if you can do this manually in InDesign, this can be automated by script. The script may find an object by an object style; or find every placed PDF and make hyperlinks out of them, like you select an object and click 'Create new hyperlink' button. But I don't see where it should get the URLs from.

                                 

                                — Kas

                                 

                                P.S. Originally the script was created for the table of contents in folio (Adobe Digital Publishing Suite) -- otherwise the links didn't work there.

                                • 13. Re: Script for converting hyperlinks to buttons?
                                  luizdsilveira Level 1

                                  Hi, Kasyan.

                                   

                                  Thanks for the explanation. I will do some tests here. Some people told me I can maybe first get the URL from the PDF Metadata and after that I can use your script. I already made it read this metadata information, I just need to spend some time and create the middle part. Lets see what will happen.

                                   

                                  Thanks a lot!

                                  • 14. Re: Script for converting hyperlinks to buttons?
                                    luizdsilveira Level 1

                                    Hi.

                                     

                                    I've just finished the script based in yours. I need to do some manual work because not all PDFs inserted in the file are Advertising and not all of them will have a URL to be clicked, so my idea was to:

                                     

                                    1. Select an ad manually

                                    2. Run the script

                                    3. Script will check where the PDF is and open a file called "url.txt" in its folder

                                    4. This TXT file has the URL for that specific ad.

                                    5. Just to make things clear, each ad is in one separated folder. I preferred to make a TXT file because ad names are changing all the time with new updated ads, but not the URL. Besides that, will be easier for me to have one text file instead of changing PDF metadata in all new ads I will receive. Hope it makes sense.

                                     

                                    tell application "Adobe InDesign CC 2015"

                                      set user interaction level of script preferences to interact with all

                                      tell the active document

                                     

                                      set the_selection to selection -- Returns a list    

                                      repeat with newselection in the_selection

                                      set the_graphic to item 1 of newselection

                                      set the_graphics_link to item link of the_graphic

                                      set FilePath to file path of the_graphics_link

                                     

                                      set theFilepath to FilePath

                                      set text item delimiters of AppleScript to ":"

                                      set mgFolder to (text items 1 thru -2 of theFilepath as string) & ":"

                                      set text item delimiters of AppleScript to ""

                                     

                                      set aScriptPath to mgFolder & "url.txt"

                                     

                                      tell application "Finder"

                                      activate

                                     

                                      set arquivo to (read (alias aScriptPath))

                                      set NewName to arquivo

                                     

                                      end tell

                                     

                                      activate

                                     

                                      --make hyperlink

                                      if not (exists hyperlink URL destination NewName) then

                                      set TheH to make hyperlink URL destination with properties {name:NewName, destination URL:NewName}

                                      else

                                      set TheH to hyperlink URL destination NewName

                                     

                                      end if

                                     

                                     

                                      set TheHSName to NewName

                                      set TheHS to make hyperlink page item source with properties {name:TheHSName, source page item:the_graphic, hidden:false}

                                     

                                      make new hyperlink with properties {destination:TheH, source:TheHS, visible:false, highlight:invert}

                                     

                                     

                                      end repeat

                                      end tell

                                    end tell