7 Replies Latest reply on Feb 23, 2014 6:43 PM by josh009

    Automated link creation?

    josh009 Level 1

      We produce a monthly catalogue that contains approximately 500 part numbers that appear in the form of 'XXXX XXXX'.

       

      Once the catalogue has been sent to print we use a third party online eBook creator that auto detects all instances of 'XXXX XXXX' and then inserts static text (http://abc.com.au/search?q=) before the part number to create a hyperlinked URL, eg.

       

      http://abc.com.au/search?q=05512365

       

      My question is, is InDesign capable of doing the same thing?

       

      Apologies if I haven't explained myself very well - am more than happy to provide more info if it helps.

       

      Thanks in advance.

        • 1. Re: Automated link creation?
          Trevorׅ Adobe Community Professional

          Hi Josh

           

          This is an easy way to do it

          app.findGrepPreferences = app.changeGrepPreferences = null;
          app.findGrepPreferences.findWhat = "\\<(\\d\\d\\d\\d) (\\d\\d\\d\\d)\\>";
          app.changeGrepPreferences.changeTo = "http://abc.com.au/search?q=$1$2" 
          var doc = app.activeDocument,
                hyperStyle = doc.characterStyles.itemByName ("Hyperlink Character Style"); // change name or leave as is or remove this line and the line below
          if (hyperStyle.isValid) app.changeGrepPreferences.appliedCharacterStyle = hyperStyle; // this is the line below
          doc.changeGrep();
          app.menuActions.item("$ID/Convert URLs to Hyperlinks...").invoke();
          
          

           

           

          HTH

           

          Trevor

          1 person found this helpful
          • 2. Re: Automated link creation?
            josh009 Level 1

            Hi Trevor,

             

            thanks for the help!

             

            It's almost what I'm after but it's not quite right.

             

            When I run the script it's correctly identifying the part numbers and creating the hyperlinks except rather than continuing to display the part number as XXXX XXXX, it's displaying the full URL, eg. abc.com.au/search?q=XXXXXXXX

             

            Can the script be amended so that the part number displayed in the document remains unchanged?

             

            Thanks again :)

            • 3. Re: Automated link creation?
              Trevorׅ Adobe Community Professional

              Yep, I misunderstood you there, I should have realized that's what you wanted..

              I'm too busy now to do that one but it's quite simple and you should get an answer here soonish, if not I'll try answer if I have time.

              • 4. Re: Automated link creation?
                Trevorׅ Adobe Community Professional

                Ok

                This is a really dirty answer and not the way to do it BUT untill you get a better one.

                Run the above script make the Hyperlinks and then run the following script.

                 

                app.findGrepPreferences = app.changeGrepPreferences = null;
                app.findGrepPreferences.findWhat = "http\:\/\/abc\\.com\\.au/search\\?q\=(\\d\\d\\d\\d)(\\d\\d\\d\\d)";
                app.changeGrepPreferences.changeTo = "$1 $2" 
                app.activeDocument.changeGrep();
                
                1 person found this helpful
                • 5. Re: Automated link creation?
                  josh009 Level 1

                  Thanks again Trevor.

                   

                  I managed to get it working with a single script by combining elements of the first solution you posted with another script i stumbled across.

                   

                  For anyone that's wondering, this is what i ended up using:

                   

                   

                   

                   

                  var myDoc = app.activeDocument;

                  var myLinkStyle = myDoc.characterStyles.itemByName("link"); // the Character style of the links

                  var myURL = "http://abc.com.au/search?q="; // url of your company

                  var i, myLinks, myLink, mySource, myDestination;

                   

                  for (i = myDoc.hyperlinkTextSources.length-1; i >= 0; i--){

                      mySource =  myDoc.hyperlinkTextSources[i];

                      if (mySource.sourceText.appliedCharacterStyle == myLinkStyle){

                          mySource.remove(); // removes existing hyperlinks to avoid "double bookings"

                      }

                  }

                   

                  app.findGrepPreferences = null;

                  app.findGrepPreferences.findWhat = "\\<(\\d\\d\\d\\d) (\\d\\d\\d\\d)\\>";

                  myLinks = myDoc.findGrep();

                  app.findGrepPreferences = null;

                   

                   

                   

                  for (i = 0; i < myLinks.length; i++){

                     myLink = myLinks[i];

                     mySource = myDoc.hyperlinkTextSources.add(myLink);

                     myDestination = myDoc.hyperlinkURLDestinations.add(myURL + myLink.contents);

                     myDoc.hyperlinks.add(mySource, myDestination);

                  }

                  • 6. Re: Automated link creation?
                    Trevorׅ Adobe Community Professional

                    That it definitely the correct direction to go but not quite there yet.

                     

                    A couple of suggestions.

                    1) Either use changeGrep or  Grep styles to automatically apply the link character style to the cat. numbers.

                     

                    2) Although the script removes the hyperlink sources if they exist it does not remove the destinations so if you need to edit a cat. number and re-run the script then your get an extra 500 not needed hyp. destinations for each re-run.  After several reruns that could start to get problematic. I would have checked to see if existing sources have change their contents and if they need a different hyp. destination to the one they presently may or may not have, accordingly deleting or changing or adding the destination.

                     

                    Regards,

                     

                    Trevor

                    • 7. Re: Automated link creation?
                      josh009 Level 1

                      OK, i'm back again.

                       

                      Seems I need to revise the original request.

                       

                      Now we need to detect the part number (XXXX XXXX) and inject it into a hyperlink that looks like this:

                       

                      http://www.abc.com.au/part/00338400?utm_source=BW%20Catalogue&utm_medium=Catalogue&utm_term=00338400&utm_campaign=Catalogue

                       

                      I've tried playing with the script however keep getting errors

                       

                       

                      Any help will be most appreciated!