Two InDesign Script that search and replace tag content or attribute.
I have written Two InDesign Script that search and replace tag content or attribute. I contribute it because there is almost no tutorial to teach you how to do search and replace inDesign xml tag attribute and I have bear a lot of pain to develop it. Eventually finished and hope it can help someone.
============= First one ==============
// basically popup a box to match the tag name and change the attribute in the tag.
==================================
//var xpath =”/*/*/teacup-tag”;
//var root = app.activeDocument.xmlElements[0];
//var node = null;
try {
//var proc = app.xmlRuleProcessors.add([xpath]);
//var match = proc.startProcessingRuleSet(root);
var myDialog = app.dialogs.add({name:”Tag Value Replace Utility”}); //Add a dialog column.
with(myDialog.dialogColumns.add()){
staticTexts.add({staticLabel:”Replace tag value.”});
var myTagEditFieldNameDiag = textEditboxes.add({editContents:”field name”, minWidth:180});
var myTagEditFieldOriginalValue = textEditboxes.add({editContents:”original text”, minWidth:180});
var myTagEditFieldNewValue = textEditboxes.add({editContents:”replace text”, minWidth:180});
} //Show the dialog box.
var myResult = myDialog.show(); //If the user clicked OK, display one message; //if they clicked Cancel, display a different message.
if(myResult == true){
alert(“Tag Attribute Value will be replaced !!”);
replaceTagAttributeValue(myTagEditFieldNameDiag.editContents,
myTagEditFieldOriginalValue.editContents,
myTagEditFieldNewValue.editContents);
} else{
alert(“Replace Tag Attribute Cancel.”);
} //Remove the dialog box from memory.
myDialog.destroy();
} //End of Try
catch(ex){
$.writeln(ex);
}
finally {
proc.endProcessingRuleSet();
proc.remove();
}
function replaceTagAttributeValue(fieldName, originalValue, newValue){
var doc,
root,
childs,
max = 0;
if(app.documents.length==0){ return; }
doc = app.activeDocument;
root = doc.xmlElements[0];
childs = root.evaluateXPathExpression(“//teacup-tag”);
max = childs.length;
if(max==0){ return; }
for(var i = 0; i< max; i++){
$.writeln(“field name: ” + fieldName);
var x = (childs[i].xmlAttributes.item(fieldName));
var tagStr = x.value;
$.writeln(“old value: ” + tagStr);
tagStr = tagStr.replace(originalValue, newValue);
x.value = tagStr;
$.writeln(“new value: ” + tagStr);
//var x = (childs[i].xmlAttributes.item(“data-source”));
//var tagStr = x.value;
//$.writeln(“old value: ” + tagStr);
//tagStr = tagStr.replace(“Financial Report Data Source”, “Financial Report SA Sept Data Source”);
//x.value = tagStr;
//$.writeln(“new value: ” + tagStr);
}
}
============= second one ==============
// basically popup a box to match the tag content with a zero value or double dash and change them into em-dash
===================================
var xpath =”/*/*/teacup-tag”;
var root = app.activeDocument.xmlElements[0];
var node = null;
try {
var proc = app.xmlRuleProcessors.add([xpath]);
var match = proc.startProcessingRuleSet(root);
while(match!=undefined) {
node = match.element;
match = proc.findNextMatch();
content=node.contents;
if(node) {
app.select(node);
}
var myText = app.selection[0];
var content=myText.contents;
//$.writeln(content.search(/[^-0-9.][^0-9.][(\w\W\s\S)]*[^N\/A]/)); > 0
//[^0-9.]|([0-9]*\s/\s[0-9]*\s/\s[0-9]*)
//$.writeln(content);
//$.writeln(content.search(/[\W]/) < 0);
//$.writeln(content.search(/\-\-\-/) );
//$.writeln(content.search(/[^-0-9.][^0-9.][A-z][(\w\W\s\S)]*[^N\/A]/) );
if(content.search(/No Data found for this field/) >= 0) {
//$.writeln(content);
//$.writeln(“replace text: No Data found for this field”);
myText.contents = myText.contents.replace(“No Data found for this field”, “\u2013”);
}
if(content.search(/^0$|^0.00$/) >= 0) {
//$.writeln(content);
//$.writeln(“replace text: –“);
myText.contents = myText.contents.replace(“0.00”, “\u2013”);
}
if(content.search(/–/) >= 0) {
//$.writeln(content);
//$.writeln(“replace text: –“);
myText.contents = myText.contents.replace(“–“, “\u2013”);
}
}//End of While
} //End of Try
catch(ex){
$.writeln(ex);
}
finally {
proc.endProcessingRuleSet();
proc.remove();
}