Script UI Cancel button still runs the script
I'm new to ScriptingUI, but I'm trying to get the hang of it. I can get the functions to work with my Search.onClick statement, but when I click the Cancel button, it still runs the function reserved for the Search.onClick. My guess is the structure of my script is wrong. Any feedback would be greatly appreciated.
var indexTagName = "IndexTag";
var doc = app.activeDocument;
var myWindow = new Window ("dialog", "Grep XML Attributes");
myWindow.orientation = "row";
myWindow.alignChildren = "bottom";
var myInputGroup = myWindow.add ("group");
myInputGroup.add ("statictext", undefined, "Search:");
myInputGroup.orientation = "column";
myInputGroup.alignChildren = "left";
var mySearch = myInputGroup.add ("edittext", undefined, null);
mySearch.characters = 40;
mySearch.active = true;
myInputGroup.add ("statictext", undefined, "Replace:");
var myReplace = myInputGroup.add ("edittext", undefined, null);
myReplace.characters = 40;
myReplace.active = false;
var myButtonGroup = myWindow.add ("group");
myButtonGroup.orientation = "column";
var Search = myButtonGroup.add ("button", undefined, "OK");
var Cancel = myButtonGroup.add ("button", undefined, "Cancel");
myWindow.show ();
main();
function main(){
var searchText = mySearch.text;
var replaceText = myReplace.text;
Search.onClick = treeRecurence(doc.xmlElements.firstItem(), searchText, replaceText);
Cancel.onClick = exit();
return;
}
function treeRecurence (node, searchText, replaceText) {
var childNodes = node.xmlElements;
if (node.markupTag.name == indexTagName) {
var atts = node.xmlAttributes;
if (atts.itemByName("value").isValid){
var att = atts.itemByName("value");
if (att.value.match(searchText)){
att.value = String(att.value).replace(searchText , replaceText);
}
}
}
var childNodesCount = childNodes.count();
if (childNodesCount == 0) return;
for (var n = 0; n < childNodesCount; n++) {
treeRecurence(childNodes.item(n), searchText, replaceText);
}
}
