2 Replies Latest reply on Nov 23, 2010 2:24 PM by JohnDevlon

    [JS][CS4-5] Apply hyperlink to xml tagged object




      Can anyone please help me? I'm strugling with this problem for days....



      I'm trying to apply a new hyperlink to a xml tagged object (piece of text, frame or image)...

      The script trows an error "wrong source. Expected text but received XML element"


      Does anyone know how i can fix this?










      var myDoc = app.documents[0];




      alert("Done !");  





      //recursive function


      function Check(elm){

               for(var i=0; i<elm.xmlElements.length; i++){

                     myXMLElement = elm.xmlElements[i];


                     XMLelementName = myXMLElement.markupTag.name.toString();


                     // only apply to tagged object using the tag "Hyperlink"

                     if (XMLelementName == "Hyperlink"){


                            var myHyperlinkURL = myDoc.hyperlinkURLDestinations.add("http://www.google.com");

                            myHyperlinkURL.name = "http://www.google.com";


                            var myObject = elm.parent;


                            if(!( elm.xmlAttributes.item("href") == null)){

                                      // it's a picture

                                     // i'm not sure this will work

                                  myHyperlinkSource = myDoc.hyperlinkPageItemSources.add(myObject);                 


                                     // it's not a picture

                                    // Error is in the line below

                                  myHyperlinkSource = myDoc.hyperlinkTextSources.add(myObject);                 


                            var myHyperlink = myDoc.hyperlinks.add(myHyperlinkSource, myHyperlinkURL);




             // process all sub elements

             for (var i = 0; i < elm.xmlElements.length; i++){




        • 1. Re: [JS][CS4-5] Apply hyperlink to xml tagged object
          Loic.Aigon Adobe Community Professional

          You are trying to add a hyperlinkto a xmlElement which is not a linkable element. You have to access the content of the tag itself.


          var doc = app.activeDocument;
          var xmlRoot = doc.xmlElements[0];
          var tag1 = xmlRoot.xmlElements[0]; //Image
          var tag2 = xmlRoot.xmlElements[1]; //Text


          tag1.texts.length //0
          tag2.texts.length //1


          tag1.images.length //1
          tag2.images.length //0


          hope it helps

          • 2. Re: [JS][CS4-5] Apply hyperlink to xml tagged object
            JohnDevlon Level 1



            Thanks for your feedback.


            Due to your great feedback, I've made some changes to the line below...


            var myObject = elm.texts[0];


            When performing the script, a hyperlink is added to all text in the textframe, not only to the text tagged by the xml tag. Does anyone know how this can be fxed?


            Also, when trying to do the same thing for  an image, but this doen't work...


            var myObject = elm.images[0];


            An error occures.


            And what about a empty graphic frame ? How can I add a hyperlink to frame if it's tagged by a certain xml tage?


            Any feedback would be helpfull...