[JS][CS4-5] Apply hyperlink to xml tagged object
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);
}
}