Skip to main content
Inspiring
November 23, 2010
Question

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

  • November 23, 2010
  • 1 reply
  • 561 views

Hi,

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?

Thanks

Code:

var myDoc = app.documents[0];

Check(myDoc.associatedXMLElement);

alert("Done !");  

//recursive function

function Check(elm){

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

               myXMLElement = elm.xmlElements;

               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);                 

                      }else{

                               // 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++){

            Check(elm.xmlElements);

      }

}

This topic has been closed for replies.

1 reply

Loic.Aigon
Legend
November 23, 2010

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

Inspiring
November 23, 2010

Hi,

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...

Tim