Skip to content
Henry's Web Development Blog
0
  • Home
  • About me
    • My Resume
  • Sites I have worked on
  • Tutorial / Class Notes
    • Web Design Basic
    • CLBC Photoshop Class
  • Shop
    • My Account
    • Cart
    • Checkout

Two InDesign Script that search and replace tag content or attribute.

November 20, 2012

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

Recent Posts

  • Ministry of Government and Consumer Services | Ontario.ca
  • Working with Marvellous Homes
  • Working in Intel Corp for their Project Management System
  • Working with Kimberley System
  • Work at Bell Media ….

Archives

  • May 2019
  • July 2018
  • January 2018
  • July 2017
  • July 2016
  • May 2015
  • April 2014
  • October 2013
  • February 2013
  • November 2012
  • April 2012
  • March 2012
  • February 2012
  • January 2012
  • October 2011

Categories

  • Development
  • Uncategorized
  • Web Design

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

Copyright Henry's Web Development Blog 2021 | Theme by ThemeinProgress | Proudly powered by WordPress