0

I need to insert a word sqrt(plain text without tags ) in the xml element of m:msqrt like below:

enter image description here

I tried:

var path = (app.activeDocument.fullName.parent.fsName).toString().replace(/\\/g, '/');
//path of active document(sample.indd)

var xmlPath = (app.activeDocument.fullName.parent.fsName).toString().replace(/\\/g, '/')+"/with_aid2.xml";

xmlFile = File (xmlPath);

if(xmlFile.exists){
    xmlFile.open();
    var myString = xmlFile.read();
    xmlFile.close();
    myXml = new XML ();
    myXml = XML(myString);
    var msqrtCount = myXml.xpath("//m:msqrt");

}else{alert(xmlPath+" doesn't exist!");}

for(var i=0;i<msqrtCount.length();i++){  
    var added =    msqrtCount[i];
    added.contents="sqrt";       
}

I get an error says invalid content property.

CRGreen
  • 3,406
  • 1
  • 14
  • 24
Learning
  • 848
  • 1
  • 9
  • 32

1 Answers1

0

I'm confused. Are you attempting to add the text to an InDesign document or to an XML file you are importing to your InDesign document? The code you provided appears to be accessing an XML file, loading it into memory and then attempts to add text to a tag in the XML file. I would handle this outside of InDesign if this is your goal.

If you want to add text to a tagged area in an InDesign document, use XML Rules Processing as below:

#include "glue code.jsx"

var myDocument = app.activeDocument;
addContent(myDocument, "new content");

function addContent(myDocument, myContent){
    var myRuleSet = new Array(new addContentToTag);

    with(myDocument){
        var elements = xmlElements;
        __processRuleSet(elements.item(0), myRuleSet);
    }

    function addContentToTag(){
        this.name = "addContentToTag";
        this.xpath = "/Root/Story/Tag";
        this.apply = function(myElement, myRuleProcessor){
                myElement.contents = myContent;
                return true;
        };
    }
}

(Please note the "glue code.jsx" file is included. This must be in your scripts directory for XML Rules Processing to function properly).

dgenr8
  • 1