1 2 Previous Next 57 Replies Latest reply on Oct 5, 2016 11:31 PM by Kasyan Servetsky

    Scripting Hyperlinks

    flyfisher200 Level 1

      W7, CS6: I have a concordance with page numbers. I want to convert all the page numbers to hyperlinks linking to the same pages in the document (actually an InDesign book). Entries are of the form;

       

      Avalon, 105, 108

      Baddeck, 98

      Bay St. George, 104, 115, 117, 271, 363, 383, 398

       

      All entries have an identical paragraph style applied and a character style applied to the page numbers.

       

      I did find one paid script to do this but it is excessively expensive for a single job. While I would be able to do something similar in PS using actions I'm only on pg 2 of the scripting manual! Can anyone help?

       

      Paul

        • 1. Re: Scripting Hyperlinks
          Obi-wan Kenobi Adobe Community Professional

          Hi, yes !  Read the page 3! …. ;-)

           

          (^/)

          • 2. Re: Scripting Hyperlinks
            Obi-wan Kenobi Adobe Community Professional

            See:

             

            LiveIndex – Id-Extras: InDesign Scripts, Tips & Solutions

             

            Very expensive but so clever and so useful!  :-)

            [ Joke! ]

             

            (^/)

            • 3. Re: Scripting Hyperlinks
              flyfisher200 Level 1

              Is this a joke? If so it's not very helpful.

              • 4. Re: Scripting Hyperlinks
                Kasyan Servetsky Level 5

                Here are a couple of free scripts (together with sample files for testing) -- complex and simple -- which I wrote for a similar sort of work.

                They won't work for you as they are, but you can use one of them as a starting point and adjust it to your specific requirements.

                I made a new version for every new book: all books are too different so I don't think it's possible to make a "universal" script.

                • 5. Re: Scripting Hyperlinks
                  flyfisher200 Level 1

                  Thank you Kasyan. I had found your page yesterday but even sample 2 is beyond me (Sample 1 made my eyes hurt!). With all the documents of a book open and the concordance list, the manual method is to highlight a page number, click on the new hyperlink button, select the proper document and the correct page number, then OK. Is this a difficult sequence to script? I checked all the "hyperlink" scripts on your site but none seem to do what I need---or at least I couldn't see it.

                   

                  Regards, Paul

                  • 6. Re: Scripting Hyperlinks
                    Obi-wan Kenobi Adobe Community Professional

                    Paul,

                     

                    I suppose index entries have been done with ID Index basic features!?  ;-)

                    • 7. Re: Scripting Hyperlinks
                      flyfisher200 Level 1

                      No, of course that's the problem. They aren't really indexes, rather they are concordance lists. The book was originally a print book where hyperlinks weren't a consideration. The book has four "indexes". The two most important were constructed using ID's index panel. The other two using a concordance list script. Now that I'm converting the book to an interactive PDF (reformatted for phone and tablet) the first two indexes come across fine but the other two obviously don't. So I was looking for a script to convert the page numbers in the concordance to hyperlinks. The paid script is almost $100 Cdn, rather expensive for a single use.

                      • 8. Re: Scripting Hyperlinks
                        Peter Spier Most Valuable Participant (Moderator)

                        flyfisher200 wrote:

                        The paid script is almost $100 Cdn, rather expensive for a single use.

                        And what do you charge per hour? Would you break even? If so it's a good buy as you'd have the script for future projects, too, if they come along. It's an even better buy if it would save you more hours than you'd have to work to pay for it.

                        • 9. Re: Scripting Hyperlinks
                          Obi-wan Kenobi Adobe Community Professional

                          … So, as I said a long time ago in post#2, for just US$ 69 (less than CAD$ 100!), buy LiveIndex of our very clever friend Ariel Walden!

                          [ Hi Ariel, think to my commission! Aha! ]

                           

                          (^/) 

                          • 10. Re: Scripting Hyperlinks
                            Obi-wan Kenobi Adobe Community Professional

                            … Or, if only 12 index entries, download a trial version and do it … for free! 

                             

                            (^/)

                            • 11. Re: Scripting Hyperlinks
                              Obi-wan Kenobi Adobe Community Professional

                              Peter,

                               

                              Good point:  For a lot, It's more interesting to spend hours on forums and Web and wait to catch a free solution rather than just ask for a paid one!

                               

                              Paul posted his question at september 27, at 21:59!

                              I gave him a [paid] solution 20 min. later that could have fixed his problem in several minutes [ I was watching football on TV at this moment!  =D ]!

                              Now, 2 days later, the discussion goes on! …

                               

                              (^/) 

                              • 12. Re: Scripting Hyperlinks
                                Eugene Tyson Adobe Community Professional & MVP

                                I found this entry on another forum - no direct link to the script though - only a contact http://indesignsecrets.com/topic/convert-index-page-numbers-into-hyperlinks-across-a-book

                                • 13. Re: Scripting Hyperlinks
                                  Pickory Level 3

                                  Hello,

                                   

                                  Are indexes clickable when exported to interactive pdf from InDesign 6? If they are you might be better off creating index entries.

                                  Indexes, word lists, and concordances | Peter Kahrel

                                   

                                  P.

                                  • 14. Re: Scripting Hyperlinks
                                    Kasyan Servetsky Level 5

                                    Here's a quick & dirty script together with sample documents. I tried to recreate your layout.

                                    Before

                                    Screenshot 2016-09-29 18.26.57.png

                                     

                                    After

                                    Screenshot 2016-09-29 18.27.18.png

                                     

                                     

                                    • "Index" should be the active document
                                    • The page numbers in the index should have "Page Number" character style applied
                                    • All the documents should be open

                                     

                                    Below is the script:

                                    /* Copyright 2016, Kasyan Servetsky
                                    September 29, 2016
                                    Written by Kasyan Servetsky
                                    http://www.kasyan.ho.com.ua
                                    e-mail: askoldich@yahoo.com */
                                    //======================================================================================
                                    var set, doc,
                                    count = 0,
                                    scriptName = "Make hyperlinks";
                                    
                                    PreCheck();
                                    
                                    //===================================== FUNCTIONS  ======================================
                                    function Main() {
                                        app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.NOTHING;
                                        app.findGrepPreferences.findWhat = "\\d+";
                                        app.findGrepPreferences.appliedCharacterStyle = "Page Number";
                                        var foundItems = doc.findGrep(true);
                                        app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.NOTHING;
                                    
                                        if (foundItems.length == 0) ErrorExit("No page numbers were found in the current document.", true);
                                        
                                        for (var i = 0; i < foundItems.length; i++) {
                                            try {
                                                var sourceTextRef = foundItems[i];
                                                MakeHyperlink(sourceTextRef);
                                            }
                                            catch(err) {
                                                $.writeln(err.message + ", line: " + err.line);
                                            }
                                        }
                                        
                                        var report = count + " hyperlink" + ((count == 1) ? " was" : "s were") + " created.";
                                        alert("Finished. " + report, scriptName);
                                    }
                                    //--------------------------------------------------------------------------------------------------------------------------------------------------------
                                    function MakeHyperlink(sourceTextRef) {
                                        var source, destination, hyperlink,
                                        pageNum = sourceTextRef.contents,
                                        obj = GetPage(pageNum);
                                        
                                        if (obj != null) {
                                            source = doc.hyperlinkTextSources.add(sourceTextRef);
                                            destination = obj.docDest.hyperlinkPageDestinations.add(obj.page);
                                            hyperlink = doc.hyperlinks.add(source, destination, {name: "Page_" + pageNum});
                                            if (hyperlink.isValid) {
                                                count++;
                                            }
                                        }
                                    }
                                    //--------------------------------------------------------------------------------------------------------------------------------------------------------
                                    function GetPage(pageNum) {
                                        var obj = null;
                                        
                                        for (var i = 0; i < app.documents.length; i++) {
                                            if (app.documents[i].pages.itemByName(pageNum).isValid) {
                                                obj = {page: app.documents[i].pages.itemByName(pageNum), docDest: app.documents[i]};
                                                break;
                                            }
                                        }
                                        
                                        return obj;    
                                    }
                                    //--------------------------------------------------------------------------------------------------------------------------------------------------------
                                    function PreCheck() {
                                        if (app.documents.length == 0) ErrorExit("Please open a document and try again.", true);
                                        doc = app.activeDocument;
                                        if (!app.activeDocument.saved) ErrorExit("The current document has not been saved since it was created. Please save the document and try again.", true);
                                        Main();
                                    }
                                    //--------------------------------------------------------------------------------------------------------------------------------------------------------
                                    function ErrorExit(error, icon) {
                                        alert(error, scriptName, icon);
                                        exit();
                                    }
                                    //--------------------------------------------------------------------------------------------------------------------------------------------------------
                                    
                                    1 person found this helpful
                                    • 15. Re: Scripting Hyperlinks
                                      heavyboots Level 2

                                      Well that's much more elegant than what I came up with! Mine basically processes every number in the selection and requires a list of documents to run… :-/

                                       

                                       

                                      tell front document of application "Adobe InDesign CS4"

                                        set desktopFolderPath to path to desktop folder as string --I put all my test documents in a desktop folder called Test Book

                                       

                                      --THIS IS THE LIST OF DOCUMENTS

                                      --EACH ITEM HAS A PATH TO THE DOCUMENT, THE STARTING PAGE NUMBER, THE ENDING PAGE NUMBER, AND AN INDEX OFFSET (eg -20 converts page 25 to the 5th page in the document with pages 21 thru 30)

                                        set myDocList to {{desktopFolderPath & "Test Book:Document1.indd", 1, 10, 0}, {"" & (desktopFolderPath & "Test Book:Document2.indd"), 11, 20, -10}, {"" & (desktopFolderPath & "Test Book:Document3.indd"), 21, 30, -20}}

                                        set numRange to {"1", "2", "3", "4", "5", "6", "7", "8", "9", "0"}

                                        set currentNum to ""

                                        set startNum to 0

                                        set endNum to 0

                                       

                                      -- Loop through the selection, pulling out numbers and assigning them hyperlinks

                                        repeat with i from 1 to count every character of selection

                                        if ((contents of character i of selection) is in numRange) then

                                        --Check if this is a number

                                        if startNum is equal to 0 then

                                        --If we're starting a new number, set all values to the new num

                                        set startNum to i

                                        set endNum to i

                                        set currentNum to contents of character i of selection

                                        else

                                        --Otherwise, change the end character index and append this to end of the current num

                                        set endNum to i

                                        set currentNum to currentNum & contents of character i of selection

                                        end if

                                        else

                                        --If it's not a number, try and assign a hyperlink if we have a number

                                        if startNum > 0 then

                                        --First, figure out which document this number belongs in and assign values based on that

                                        set thisPath to ""

                                        repeat with j from 1 to count every item of myDocList

                                        if item 2 of item j of myDocListcurrentNum and item 3 of item j of myDocListcurrentNum then

                                        set thisPath to item 1 of item j of myDocList

                                        set indexOffset to item 4 of item j of myDocList

                                        end if

                                        end repeat

                                        if length of thisPath > 0 then

                                        --If we found a document to associate with this number, make a destination and a source and link them

                                        set myHyperlinkDestination to make new hyperlink external page destination with properties {document path:thisPath, destination page index:currentNum as number + indexOffset}

                                        set myHyperlinkSource to make new hyperlink text source with properties {source text:text from character startNum to character endNum of selection}

                                        make new hyperlink with properties {destination:myHyperlinkDestination, source:myHyperlinkSource, name:"Page: " & currentNum & " Start char: " & startNum}

                                        else

                                        display dialog "Couldn't find a document associated with page number " & currentNum

                                        end if

                                       

                                        --After linking, always reset our starting values back to scratch

                                        set startNum to 0

                                        set endNum to 0

                                        set currentNum to 0

                                        end if

                                        end if

                                        end repeat

                                      end tell

                                      • 16. Re: Scripting Hyperlinks
                                        Obi-wan Kenobi Adobe Community Professional

                                        Hi Kas! 

                                         

                                        Always so nice! Your script is almost perfect!

                                        I find a problem somewhere (I think in the function MakeHyperlink(sourceTextRef): when the script has created an hyperlink, e.g., at page "11", if it finds another page "11", this new page won't be catched and treated!

                                         

                                        (^/)

                                        • 17. Re: Scripting Hyperlinks
                                          flyfisher200 Level 1

                                          I understand your point but I'm both retired and not working for someone else. Rather I'm converting one of my own books to an interactive PDF having reformatted the book for phone and tablet. I'll never sell enough to recoup the price of that script.

                                          Paul

                                          • 18. Re: Scripting Hyperlinks
                                            flyfisher200 Level 1

                                            More like 500!

                                            • 19. Re: Scripting Hyperlinks
                                              flyfisher200 Level 1

                                              Yes, properly formatted indexes do become hyperlinked when exporting to Interactive PDF from CS6. Yes I could have individually indexed over a thousand entries in the concordance lists but...

                                              • 20. Re: Scripting Hyperlinks
                                                flyfisher200 Level 1

                                                A huge thanks Kasyan. Your Dropbox is currently overloaded but I'll try early morning my time tomorrow. Meanwhile I'll try a copy/paste from the post.

                                                 

                                                Paul

                                                • 21. Re: Scripting Hyperlinks
                                                  flyfisher200 Level 1

                                                  Sorry I replied too quickly. If you construct an index in CS6 using the Index function, it will become hyperlinked automatically when exporting the book to an interactive PDF. However, if you construct an index using a concordance list script like Peter Kahrel's, it won't be hyperlinked when exporting. This makes sense because when using a simple concordance list script there are no index markers in the documents. Really these yield an automated Find/Change process---perfectly usable and very handy for print documents. This was how my concordance lists were populated originally.

                                                  Paul

                                                  • 22. Re: Scripting Hyperlinks
                                                    Peter Spier Most Valuable Participant (Moderator)

                                                    Fair enough. Your time is basically worth nothing, so anything you spend is a deficit.

                                                    • 23. Re: Scripting Hyperlinks
                                                      Kasyan Servetsky Level 5

                                                      For some reason, DropBox refuses to share my public links saying that my account generates too much traffic. Here's another link to the sample files and the script.

                                                       

                                                      Obi-wan Kenobi

                                                      I didn't say I was going to write a perfect script: I called it "quick & dirty" (the term was borrowed from Dave Saunders). Tonight I was sitting at work and had no work to do, and to avoid falling asleep I started entertaining myself by writing the script.

                                                      To write a more or less decent script I need "real" files for testing. This one I wrote based on the scarce information the OP had provided. I assume that page numbers never duplicate as in all normal books. Of course, It's easy to handle such a situation: e.g. add an error logging feature that would warn the user that two pages were found whose number was 11, and the first found page was used for the hyperlink.

                                                      Also, I think it would make sense to add a dialog box for the user to select settings for hyperlinks: fitting options, visible or not, etc.

                                                      I also have no idea how the character style is applied in the index: I assume that separately to each page number (without spaces and commas). But it can be applied to all numbers in one go, or the index may have page ranges; in this case, the script needs some tweaking.

                                                       

                                                      —Kas

                                                      • 24. Re: Scripting Hyperlinks
                                                        Pickory Level 3

                                                        I am glad you have found a solution.

                                                         

                                                        To add to the mix.

                                                         

                                                        I have a free script with a bit of UI.

                                                        http://bit.ly/2dtsSwK

                                                        And a bit about it.

                                                        QuickIndex - Kerntiff Publishing Systems

                                                         

                                                        Probably doesn't fit the bill here, but might be of use later.

                                                         

                                                        P.

                                                        • 25. Re: Scripting Hyperlinks
                                                          flyfisher200 Level 1

                                                          As O-W K surmised, the occurrence of multiple instances of the same page number in the concordance has led to 262 "failures." Let me explain what this is about. I have written books about fly fishing and this one contains many Atlantic salmon fly patterns. Fly fishers are interested in which rivers a pattern has succeeded or simply recommended for. So I created a concordance list of all the pages a river is mentioned. In the print version this suffices, however in an interactive pdf it would be nice to make the page references live. Kas' script got about 3/4 of the page references. It occurred to me that if the script applied a character style to the Index page numbers that are now hyperlinks I could use F/C to delete these and then run the script again. Rinse and repeat and I should quickly get everything. Is this a simple modification?

                                                           

                                                          Paul

                                                          • 26. Re: Scripting Hyperlinks
                                                            flyfisher200 Level 1

                                                            Tried that, no joy!

                                                            Paul

                                                            • 27. Re: Scripting Hyperlinks
                                                              Obi-wan Kenobi Adobe Community Professional

                                                              Paul,

                                                               

                                                              Quick question: Can you make one document instead of a book? 

                                                               

                                                              (^/)

                                                              • 28. Re: Scripting Hyperlinks
                                                                flyfisher200 Level 1

                                                                Yes, I could.

                                                                 

                                                                Paul

                                                                • 29. Re: Scripting Hyperlinks
                                                                  Obi-wan Kenobi Adobe Community Professional

                                                                  … So, do it! I'll give you the way to fix your problem! Unless Kas corrects his script - Better! 

                                                                   

                                                                  (^/)

                                                                  • 30. Re: Scripting Hyperlinks
                                                                    flyfisher200 Level 1

                                                                    For some unfathomable reason, InD decided that there should be two page 58. Didn't bother Acrobat before when exporting from the book panel but in this combined document, all the links in the two properly indexed indexes show as links in Acrobat but they're dead. So unless there's a mod to the original script I'll just drop the extra two concordances.

                                                                     

                                                                    Paul

                                                                    • 31. Re: Scripting Hyperlinks
                                                                      Kasyan Servetsky Level 5

                                                                      Here's a new version.

                                                                       

                                                                      1. The starting point -- with some duplicated page numbers.

                                                                      1.png

                                                                      2. I run the script for the 1st time. Successfully created hyperlinks are marked in green, duplicated in red (the swatches are created automatically)

                                                                      2.png

                                                                      3. I remove the red swatch (the script skips the red and green page numbers to avoid reprocessing them) and remove duplicates manually

                                                                      3.png

                                                                      4. Finally I run it again and it makes two more h-links

                                                                      4.png

                                                                       

                                                                      /* Copyright 2016, Kasyan Servetsky
                                                                      October 3, 2016
                                                                      Written by Kasyan Servetsky
                                                                      http://www.kasyan.ho.com.ua
                                                                      e-mail: askoldich@yahoo.com */
                                                                      //======================================================================================
                                                                      var scriptName = "Make hyperlinks",
                                                                      set, doc, swatchOK, swatchDuplicates,
                                                                      count = 0;
                                                                      
                                                                      PreCheck();
                                                                      
                                                                      //===================================== FUNCTIONS  ======================================
                                                                      function Main() {
                                                                          app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.NOTHING;
                                                                          app.findGrepPreferences.findWhat = "\\d+";
                                                                          app.findGrepPreferences.appliedCharacterStyle = "Page Number";
                                                                          var foundItems = doc.findGrep(true);
                                                                          app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.NOTHING;
                                                                      
                                                                          if (foundItems.length == 0) ErrorExit("No page numbers were found in the current document.", true);
                                                                          
                                                                          var arr = [];
                                                                          for (var k = 0; k < foundItems.length; k++) {
                                                                              arr.push(foundItems[k].contents);
                                                                          }
                                                                          
                                                                          var sortedArr = arr.slice().sort();
                                                                          var duplicates = [];
                                                                          
                                                                          for (var i = 0; i < arr.length - 1; i++) {
                                                                              if (sortedArr[i + 1] == sortedArr[i]) {
                                                                                  duplicates.push(sortedArr[i]);
                                                                              }
                                                                          }
                                                                          
                                                                          for (var f = 0; f < foundItems.length; f++) {
                                                                              try {
                                                                                  var sourceTextRef = foundItems[f];
                                                                                  
                                                                                  if (IsInArray(sourceTextRef.contents, duplicates)) {
                                                                                      sourceTextRef.fillColor = swatchDuplicates;
                                                                                  }
                                                                                  else if (sourceTextRef.fillColor != swatchOK && sourceTextRef.fillColor != swatchDuplicates) {
                                                                                      MakeHyperlink(sourceTextRef);
                                                                                  }
                                                                              }
                                                                              catch(err) {
                                                                                  $.writeln(err.message + ", line: " + err.line);
                                                                              }
                                                                          }
                                                                          
                                                                          var report = count + " hyperlink" + ((count == 1) ? " was" : "s were") + " created.";
                                                                          alert("Finished. " + report, scriptName);
                                                                      }
                                                                      //--------------------------------------------------------------------------------------------------------------------------------------------------------
                                                                      function MakeHyperlink(sourceTextRef) {
                                                                          var source, destination, hyperlink,
                                                                          pageNum = sourceTextRef.contents,
                                                                          obj = GetPage(pageNum);
                                                                          
                                                                          if (obj != null) {
                                                                              source = doc.hyperlinkTextSources.add(sourceTextRef);
                                                                              destination = obj.docDest.hyperlinkPageDestinations.add(obj.page);
                                                                              hyperlink = doc.hyperlinks.add(source, destination, {name: "Page_" + pageNum});
                                                                              if (hyperlink.isValid) {
                                                                                  count++;
                                                                                  sourceTextRef.fillColor = swatchOK;
                                                                              }
                                                                          }
                                                                      }
                                                                      //--------------------------------------------------------------------------------------------------------------------------------------------------------
                                                                      function GetPage(pageNum) {
                                                                          var obj = null;
                                                                          
                                                                          for (var i = 0; i < app.documents.length; i++) {
                                                                              if (app.documents[i].pages.itemByName(pageNum).isValid) {
                                                                                  obj = {page: app.documents[i].pages.itemByName(pageNum), docDest: app.documents[i]};
                                                                                  break;
                                                                              }
                                                                          }
                                                                          
                                                                          return obj;    
                                                                      }
                                                                      //--------------------------------------------------------------------------------------------------------------------------------------------------------
                                                                      function CheckSwatches() {
                                                                          if (doc.swatches.itemByName("===== OK =====") == null) {
                                                                              swatchOK = doc.colors.add({name : "===== OK =====", model : ColorModel.PROCESS, space : ColorSpace.RGB, colorValue : [0, 255, 0]});
                                                                          }
                                                                          else {
                                                                              swatchOK = doc.swatches.itemByName("===== OK =====");
                                                                          }
                                                                      
                                                                          if (doc.swatches.itemByName("===== DUPLICATES =====") == null) {
                                                                              swatchDuplicates = doc.colors.add({name : "===== DUPLICATES =====", model : ColorModel.PROCESS, space : ColorSpace.RGB, colorValue : [255, 0, 0]});
                                                                          }
                                                                          else {
                                                                              swatchDuplicates = doc.swatches.itemByName("===== DUPLICATES =====");
                                                                          }
                                                                      }
                                                                      //--------------------------------------------------------------------------------------------------------------------------------------------------------
                                                                      function IsInArray(string, arr) {
                                                                          for (x in arr) {
                                                                              if (string.toLowerCase() == arr[x].toLowerCase()) {
                                                                                  return true;
                                                                              }
                                                                          }
                                                                          return false;
                                                                      }
                                                                      //--------------------------------------------------------------------------------------------------------------------------------------------------------
                                                                      function PreCheck() {
                                                                          if (app.documents.length == 0) ErrorExit("Please open a document and try again.", true);
                                                                          doc = app.activeDocument;
                                                                          if (!app.activeDocument.saved) ErrorExit("The current document has not been saved since it was created. Please save the document and try again.", true);
                                                                          CheckSwatches();
                                                                          Main();
                                                                      }
                                                                      //--------------------------------------------------------------------------------------------------------------------------------------------------------
                                                                      function ErrorExit(error, icon) {
                                                                          alert(error, scriptName, icon);
                                                                          exit();
                                                                      }
                                                                      //--------------------------------------------------------------------------------------------------------------------------------------------------------
                                                                      
                                                                      1 person found this helpful
                                                                      • 32. Re: Scripting Hyperlinks
                                                                        Obi-wan Kenobi Adobe Community Professional

                                                                        Hi Kas,

                                                                         

                                                                        No reason to remove the "50" entry on the "Baddeck …" line. In fact, the "double" entries needs to be treated!

                                                                        Not tested on a book but the script doesn't seem to work on a single doc!

                                                                         

                                                                        (^/)

                                                                        • 33. Re: Scripting Hyperlinks
                                                                          flyfisher200 Level 1

                                                                          Hi Kas,

                                                                           

                                                                          Thanks again for the work. Unfortunately, as O-W K pointed out, it's the duplicates that need hyperlinks. In your example there are two page 100 after Avalon; this does not occur in concordance. However, it's the second example with a page 50 associated with two different rivers that causes the problem. Here is a longer chunk of the concordance. In essence the script should ignore the green on the second pass and hyperlink the reds, changing them to green. Because there are instances where a page number occurs 3, 4, or even 5 times the script may have to be run multiple times. The "objects" are river names and a tier may recommend the pattern for any number of rivers.

                                                                          Thanks, Paul

                                                                           

                                                                          Avalon, 105, 108

                                                                          Baddeck, 98

                                                                          Bay St. George, 104, 115, 117, 271, 363, 383, 398

                                                                          Big Salmon, 13, 374

                                                                          Blackwater, 346

                                                                          Bonaventure, 69, 90, 94, 120, 151, 161, 162, 165, 167, 168, 193, 203, 217, 240, 274, 277, 284, 293, 301, 303, 308, 316, 376, 377, 384, 385, 390, 394, 398, 431

                                                                          Bondal, 91, 92

                                                                          Cains, 3, 5, 306, 324

                                                                          Cascapedia, 69, 162, 168, 187, 193, 201, 204, 229, 234, 274, 300, 303, 309, 311, 321, 324, 376, 384, 387, 431, 433, 434, 436

                                                                          Castor, 188, 363

                                                                          Causapscal, 303

                                                                          Cheticamp, 40, 41

                                                                          Codroy, 110, 111, 117, 270, 315, 317, 339, 398

                                                                          Crabbe’s, 83,117, 153, 241

                                                                          Dartmouth, 42, 64, 79, 132, 150, 152, 184, 195, 209, 303, 432, 457

                                                                          Dee, 4, 82, 94, 122, 286, 320, 345, 379, 382, 394, 403

                                                                          Delay, 416

                                                                          Derwent, 353

                                                                          Dungarvon, 32

                                                                          Eagle, 258, 363

                                                                          Escoumins, 194, 303, 411

                                                                          • 34. Re: Scripting Hyperlinks
                                                                            Kasyan Servetsky Level 5

                                                                            Obi-wan Kenobi

                                                                            It works for me on a single document in the same way as on the whole book (to be more exact all documents open):

                                                                            10-3-2016 3-36-58 PM.png

                                                                            • 35. Re: Scripting Hyperlinks
                                                                              flyfisher200 Level 1

                                                                              Would your proposed solution work by working on one document at a time?

                                                                              • 36. Re: Scripting Hyperlinks
                                                                                Obi-wan Kenobi Adobe Community Professional

                                                                                Kas,

                                                                                 

                                                                                Yes! but with the same problem [see previous post]!

                                                                                 

                                                                                (^/)

                                                                                • 37. Re: Scripting Hyperlinks
                                                                                  Kasyan Servetsky Level 5

                                                                                  Here is a new version. Have no time to test it carefully at the moment.

                                                                                  /* Copyright 2016, Kasyan Servetsky
                                                                                  October 3, 2016
                                                                                  Written by Kasyan Servetsky
                                                                                  http://www.kasyan.ho.com.ua
                                                                                  e-mail: askoldich@yahoo.com */
                                                                                  //======================================================================================
                                                                                  var scriptName = "Make hyperlinks",
                                                                                  set, doc, swatchOK,
                                                                                  count = 0;
                                                                                  
                                                                                  PreCheck();
                                                                                  
                                                                                  //===================================== FUNCTIONS  ======================================
                                                                                  function Main() {
                                                                                      app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.NOTHING;
                                                                                      app.findGrepPreferences.findWhat = "\\d+";
                                                                                      app.findGrepPreferences.appliedCharacterStyle = "Page Number";
                                                                                      var foundItems = doc.findGrep(true);
                                                                                      app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.NOTHING;
                                                                                  
                                                                                      if (foundItems.length == 0) ErrorExit("No page numbers were found in the current document.", true);
                                                                                      
                                                                                      for (var f = 0; f < foundItems.length; f++) {
                                                                                          try {
                                                                                              var sourceTextRef = foundItems[f];
                                                                                              
                                                                                              if (sourceTextRef.fillColor != swatchOK) {
                                                                                                  MakeHyperlink(sourceTextRef);
                                                                                              }
                                                                                          }
                                                                                          catch(err) {
                                                                                              $.writeln(err.message + ", line: " + err.line);
                                                                                          }
                                                                                      }
                                                                                      
                                                                                      var report = count + " hyperlink" + ((count == 1) ? " was" : "s were") + " created.";
                                                                                      alert("Finished. " + report, scriptName);
                                                                                  }
                                                                                  //--------------------------------------------------------------------------------------------------------------------------------------------------------
                                                                                  function MakeHyperlink(sourceTextRef) {
                                                                                      var source, destination, hyperlink,
                                                                                      pageNum = sourceTextRef.contents,
                                                                                      obj = GetPage(pageNum);
                                                                                      
                                                                                      if (obj != null) {
                                                                                          source = doc.hyperlinkTextSources.add(sourceTextRef);
                                                                                          destination = obj.docDest.hyperlinkPageDestinations.add(obj.page);
                                                                                          
                                                                                          var name = "Page_" + pageNum;
                                                                                  
                                                                                          if (!doc.hyperlinks.itemByName(name).isValid) {
                                                                                              hyperlink = doc.hyperlinks.add(source, destination, {name: name});
                                                                                          }
                                                                                          else {
                                                                                              hyperlink = doc.hyperlinks.add(source, destination, {name: name + "_" + String(Math.random()).replace(/^0\./, "")});
                                                                                          }
                                                                                      
                                                                                          if (hyperlink.isValid) {
                                                                                              count++;
                                                                                              sourceTextRef.fillColor = swatchOK;
                                                                                          }
                                                                                      }
                                                                                  }
                                                                                  //--------------------------------------------------------------------------------------------------------------------------------------------------------
                                                                                  function GetPage(pageNum) {
                                                                                      var obj = null;
                                                                                      
                                                                                      for (var i = 0; i < app.documents.length; i++) {
                                                                                          if (app.documents[i].pages.itemByName(pageNum).isValid) {
                                                                                              obj = {page: app.documents[i].pages.itemByName(pageNum), docDest: app.documents[i]};
                                                                                              break;
                                                                                          }
                                                                                      }
                                                                                      
                                                                                      return obj;    
                                                                                  }
                                                                                  //--------------------------------------------------------------------------------------------------------------------------------------------------------
                                                                                  function CheckSwatches() {
                                                                                      if (doc.swatches.itemByName("===== OK =====") == null) {
                                                                                          swatchOK = doc.colors.add({name : "===== OK =====", model : ColorModel.PROCESS, space : ColorSpace.RGB, colorValue : [0, 255, 0]});
                                                                                      }
                                                                                      else {
                                                                                          swatchOK = doc.swatches.itemByName("===== OK =====");
                                                                                      }
                                                                                  }
                                                                                  //--------------------------------------------------------------------------------------------------------------------------------------------------------
                                                                                  function PreCheck() {
                                                                                      if (app.documents.length == 0) ErrorExit("Please open a document and try again.", true);
                                                                                      doc = app.activeDocument;
                                                                                      if (!app.activeDocument.saved) ErrorExit("The current document has not been saved since it was created. Please save the document and try again.", true);
                                                                                      CheckSwatches();
                                                                                      Main();
                                                                                  }
                                                                                  //--------------------------------------------------------------------------------------------------------------------------------------------------------
                                                                                  function ErrorExit(error, icon) {
                                                                                      alert(error, scriptName, icon);
                                                                                      exit();
                                                                                  }
                                                                                  //--------------------------------------------------------------------------------------------------------------------------------------------------------
                                                                                  
                                                                                  • 38. Re: Scripting Hyperlinks
                                                                                    flyfisher200 Level 1

                                                                                    Still no luck I'm afraid. It doesn't yield any links when run a second time. I tried deleting all the green page numbers and rerunning but that had no effect either. Did I misunderstand the sequence after running the first time?

                                                                                     

                                                                                    Paul

                                                                                    • 39. Re: Scripting Hyperlinks
                                                                                      Obi-wan Kenobi Adobe Community Professional

                                                                                      Not tested on a book but it works fine on a single doc.!

                                                                                       

                                                                                      Great work, Kas!    [ With pleasure, I'll take time to study your code! ]

                                                                                       

                                                                                      (^/)

                                                                                      1 2 Previous Next