3 Replies Latest reply on Dec 13, 2016 1:26 PM by HeulwenM

    How to loop through multiple shared Hyperlink destinations and turn off "sharing"

    HeulwenM Level 1

      Hi there,

       

      I've been using this other forum post as a guide : Shared Hyperlink Destination (thread/791822) but unfortunately I can't quite get the behaviour i need.

       

      Essentially I have documents that contain lots of shared destination URL hyperlinks. So there are multiple shared destinations and there are multiple hyperlinks using each shared destination.

       

      I need to give each hyperlink their own destination URL and remove all shared urls. This then allows me to loop through all the hyperlinks and individually adjust their destination URL based on some other criteria (which I do later in the script).

       

      My understanding is that to remove shared destinations I need to change the "hidden" property from "false" to "true" for shared destinations. It also seems that the only way to do this is to delete and recreate the hyperlink URL destination.

       

      The problem with my current approach is that it removes the shared destination correctly, but it only adds an unshared location back to one of the hyperlinks that was using that shared destination. All the other hyperlinks that were using that shared destination now have NO destination value.

       

      This has been my approach :

      1. Loop through all hyperlinks that contain a HyperlinkURLDestination and have the destination.hidden value set to 'false'
      2. For the current hyperlink, capture the destinationURL into a variable.
      3. Now remove the destination from that hyperlink.
      4. Add a new destination URL to the current hyperlink using the value stored in the variable (which is the same as what it was originally) and set 'hidden' to true (so destination is not shared).

       

      The above works perfectly for the current hyperlink, but all other hyperlinks that used that shared Destination URL now have no value.

       

      To solve the problem I'm thinking the following approach might work but I'm having trouble working out the correct code:

      1. Instead of looping through hyperlinks, loop through hyperlinkURLDestinations (whose hidden value is set to 'false')
      2. For the current hyperlinkURLDestination, capture the destinationURL into a variable
      3. Now get a 'list' of all the hyperlinks that use the current hyperlinkURLDestination
      4. Delete the hyperlinkURLDestination
      5. Loop thru each hyperlink in the list created at step 3 and add a new destination URL to the current hyperlink using the value stored in the variable (which is the same as what it was originally) and set 'hidden' to true (so destinations aren't shared).
      6. Now proceed to next hyperlinkURLDestination and repeat steps

       

      My key problem is I don't know what javascript I need to write to for Step 3 - how do I get that list of hyperlinks that use the current hyperlinkURLDestination??? Any help to work out how to do that (or any suggestions on a better approach) would be greatly appreciated!

       

      Many thanks, HJ

        • 1. Re: How to loop through multiple shared Hyperlink destinations and turn off "sharing"
          HeulwenM Level 1

          Here is another idea that I might try:

           

          1. Loop thru all hyperlinks and put a copy of the URLdestination text into the hyperlink label

          2. Once all hyperlinks have been looped thru, delete all shared hyperlink URLdestinations

          3. Now loop back thru hyperlinks again and take the value in the label and use it to re create the hyperlink URLdestination for that hyperlink.

           

          HJ

          • 2. Re: How to loop through multiple shared Hyperlink destinations and turn off "sharing"
            Pickory Level 3

            Hello,

            Here is a lump of code I use to build a list of destinations and source.

            //===================

              var hypers = app.documents[0].hyperlinks.everyItem().getElements();

              

              for ( len = 0; len < hypers.length; len++ )

              {

              // ===== START destinationURL

              try

              {

              if (( hypers[len].destination ) && ( hypers[len].destination.hasOwnProperty("destinationURL")) && (hypers[len].destination.destinationURL.length ))

              {

              if ( hypers[len].source instanceof HyperlinkPageItemSource )

              continue;

              if ( 'undefined'==typeof hypers[len].source )

              continue;

              if ( 'undefined'==typeof hypers[len].source.sourceText )

              continue;

              if ( 'undefined'==typeof hypers[len].source.sourceText.parentTextFrames[0] )

              continue;

             

              if ( hypers[len].source.sourceText.contents.length )

              {

              hyperDesinations[nn] = {};

              hyperDesinations[nn]["PARENT"] = hypers[len].source.sourceText.parentTextFrames[0];

              hyperDesinations[nn]["id"] = hypers[len].source.sourceText.parentTextFrames[0].id;

              hyperDesinations[nn]["TYPE"] = "URLDest";

              hyperDesinations[nn]["SRC"] = hypers[len].source;

              hyperDesinations[nn]["DEST"] = hypers[len].destination.destinationURL;

              hyperDesinations[nn]["PARA"] = "";

              nn++;

                                continue;

              }

              }

              }

              catch ( e ) {;}

            1 person found this helpful
            • 3. Re: How to loop through multiple shared Hyperlink destinations and turn off "sharing"
              HeulwenM Level 1

              Thanks Pickory - I'll probably use this code for another issue. As for this issue I ended up going with this solution:

               

              1. Loop thru all hyperlinks and put a copy of the URLdestination text into the hyperlink label

              2. Once all hyperlinks have been looped thru, delete all shared hyperlink URLdestinations

              3. Now loop back thru hyperlinks again and take the value in the label and use it to re create the hyperlink URLdestination for that hyperlink.

               

              Its probably not the fastest option in terms of performance but it was very easy to script : )

               

              Here is an extract from my script for the above steps:

               

              var myDoc=app.documents[0]

              var myLinks=myDoc.hyperlinks.everyItem().getElements()

              var myLinkDestinations = myDoc.hyperlinkURLDestinations.everyItem().getElements()

              var myShareStatus

              var myDestName = "www.xx.com"

              var myDest

               

              // 1. Loop thru all hyperlinks and put copy of the URL destination text in hyperlink label.

              // Do only for hyperlinks that : (a) have a destination  (b) is of destination type 'HyperlinkURLDestination' 

              // (c) the destination link contains 'www.xx.com' and (d) the destination link is shared

               

               

              for(var i = myLinks.length-1; i>=0; i--)

                  {

              // Have 'try' statement to bypass links that are broken/don't have a destination

              try{

              myShareStatus = myLinks[i].destination.hidden

              if (myLinks[i].destination instanceof HyperlinkURLDestination && myShareStatus = "false" ){

              if (myLinks[i].destination.destinationURL.indexOf('www.xx.com') >= 0){

                 

              // 2. If the hyperlink is shared, the hyperlink URL destination value needs to be added to the hyperlink label

                  if (myShareStatus = "false"){

                     myDest = myLinks[i].destination.destinationURL 

                     myLinks[i].label  = myDest

              }

              }

              }

              }

              catch (err) {

                 //Do nothing

              }

              }

               

               

              // 3. Loop thru hyperlink URLdestinations and delete any that are shared

              for(var e = myLinkDestinations.length-1; e>=0; e--)

                  {

              myShareStatus = myLinkDestinations[e].hidden

              if (myLinkDestinations[e].destinationURL.indexOf('www.xx.com') >= 0 && myShareStatus = "false"){

                 myLinkDestinations[e].remove();

                 }

              }

               

               

              // 4. Loop thru hyperlinks and recreate hyperlink URLDestination from value in 'label' field.

              //Only recreate URLDestination from 'label'  if value in 'label' field contains www.xx.com

              for(var i = myLinks.length-1; i>=0; i--)

                  {

              if (myLinks[i].label.indexOf('www.xx.com') >= 0){

                  myDest = myLinks[i].label

                  myCounter = myCounter + 1

                  myDestName =  myDestName + String(myCounter)   

              var myNewDest = myDoc.hyperlinkURLDestinations.add( myDest, {name:myDestName, hidden: true} );

                     myLinks[i].destination = myNewDest;

              }

              }