6 Replies Latest reply on Jan 4, 2017 9:35 AM by johnpredmore

    Help Creating Cross-References Linked to Text Anchors

    johnpredmore

      Hi I'm fairly new to scripting so I'm having trouble modifying the following script. I've gotten it to do all I need to do except that I need to change the link type from URL to Text Anchor. Can you please help?

       

        • 1. Re: Help Creating Cross-References Linked to Text Anchors
          johnpredmore Level 1

          Here's the script if it didn't show up above for you

           

          var myDoc = app.activeDocument; 

          app.findGrepPreferences = app.changeGrepPreferences = null; 

           

          app.findGrepPreferences.findWhat = "a"; 

          app.findGrepPreferences.appliedCharacterStyle = myDoc.characterStyles.item("Superscript");

          app.findGrepPreferences.appliedParagraphStyle = myDoc.paragraphStyleGroups.item("Table").paragraphStyles.item("Table Title");

           

           

          var myFound1 = myDoc.findGrep(); 

           

          count = 0 

          for(k=0; k<myFound1.length; k++) 

          try{ 

              var myFind = myFound1[k]; 

              var myHyperlinkSource = app.activeDocument.hyperlinkTextSources.add(myFound1[k]) 

              var myHyperlinkURLDestination = app.activeDocument.hyperlinkURLDestinations.add(myFound1[k].contents) 

              var myHyperlink = app.activeDocument.hyperlinks.add(myHyperlinkSource, myHyperlinkURLDestination, {name: myFound1[k].contents+count++}) 

              count++ 

          }catch(e){} 

           

          alert(count/2 + " Occurrences Changed")

          • 2. Re: Help Creating Cross-References Linked to Text Anchors
            Peter Kahrel Adobe Community Professional & MVP

            It's indeed confusing that InDesign uses two different terms for the same object: text anchor and text destination.

             

            Change "myHyperlinkURLDestination" to "myHyperlinkTextDestination" three times, and change "myFound1[k].contents" to "myFound1[k]"

             

            var myHyperlinkTextDestination = app.activeDocument.hyperlinkTextDestinations.add (myFound1[k]);
            var myHyperlink = app.activeDocument.hyperlinks.add (myHyperlinkSource, myHyperlinkTextDestination, . . .);
            

             

            Peter

            • 3. Re: Help Creating Cross-References Linked to Text Anchors
              johnpredmore Level 1

              Thanks for your response Peter, is the following the code you are proposing? I'm still not getting it to work; perhaps you can see where I'm messing up.

               

               

              Thanks again!

              • 4. Re: Help Creating Cross-References Linked to Text Anchors
                johnpredmore Level 1

                Here's the script if it didn't show up above for you.

                 

                var myDoc = app.activeDocument; 

                app.findGrepPreferences = app.changeGrepPreferences = null; 

                 

                app.findGrepPreferences.findWhat = "a"; 

                app.findGrepPreferences.appliedCharacterStyle = myDoc.characterStyles.item("Superscript");

                app.findGrepPreferences.appliedParagraphStyle = myDoc.paragraphStyleGroups.item("Table").paragraphStyles.item("Table Title");

                 

                 

                var myFound1 = myDoc.findGrep(); 

                 

                count = 0 

                for(k=0; k<myFound1.length; k++) 

                try{ 

                    var myFind = myFound1[k]; 

                    var myHyperlinkTextDestination = app.activeDocument.hyperlinkTextDestination.add(myFound1[k]);

                    var myHyperlink = app.activeDocument.hyperlinks.add (myHyperlinkSource, myHyperlinkTextDestination, {name: myFound1[k].contents+count++}); 

                    count++ 

                }catch(e){} 

                 

                alert(count/2 + " Hyperlinks Were Created!") 

                • 5. Re: Help Creating Cross-References Linked to Text Anchors
                  Peter Kahrel Adobe Community Professional & MVP

                  It should be app.activeDocument.hyperlinkTextDestinations.add (. . .) (mind the 's')

                  And you need to specify the source: links are from sources to destinations. What are you trying to link? You look for 'a' in a particular character and paragraph style, then you create an anchor at the found a's, then you create a link from somewhere to that a. Where is that 'somewhere'?

                  • 6. Re: Help Creating Cross-References Linked to Text Anchors
                    johnpredmore Level 1

                    Thanks for your explanation Peter; it really helped it click for me. I was trying to link to a "somewhere" (anchor tag) called "a".

                    Here's the code that worked for me based on your tip:

                     

                    var myDoc = app.activeDocument;

                    app.findGrepPreferences = app.changeGrepPreferences = null;

                     

                    app.findGrepPreferences.findWhat = "a"; // Grep for what you need to find

                    app.findGrepPreferences.appliedCharacterStyle = myDoc.characterStyles.item("Superscript");

                    app.findGrepPreferences.appliedParagraphStyle = myDoc.paragraphStyleGroups.item("Table").paragraphStyles.item("Table Title");

                     

                     

                    var myFound1 = myDoc.findGrep();

                     

                    count = 0

                    for(k=0; k<myFound1.length; k++)

                    {

                    try{

                        var myFind = myFound1[k];

                        var myHyperlinkSource = app.activeDocument.hyperlinkTextSources.add(myFound1[k]) 

                        var myHyperlinkTextDestination = app.activeDocument.hyperlinkTextDestinations.item("a"); // Name of anchor to link to

                        var myHyperlink = app.activeDocument.hyperlinks.add (myHyperlinkSource, myHyperlinkTextDestination, {name: myFound1[k].contents+count++});

                        count++

                    }catch(e){}

                    }

                     

                    alert(count/2 + " Hyperlink Cross-References Were Created!")

                     

                     

                     

                    Thanks again Peter!