Copy link to clipboard
Copied
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);
}
}
Small modification.. please use the below script..
...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.acti
Copy link to clipboard
Copied
Small modification.. please use the below script..
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");
var myResult = myWindow.show()
if(myResult == 1){
main();
}
else if (myResult== 2){
alert("You clicked Cancel!");
exit(0);
}
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);
}
}
Copy link to clipboard
Copied
Cancel works like a charm now. Thank you very much tpk!
Just to make sure I understand the logic, all of the buttons in a window are assigned a value based on the order in which they are added to the window. So, if I create a window with 5 buttons, each will return 1-5 values. Is that correct?
Copy link to clipboard
Copied
Your script exit with
Runtime Error: Error Code# 24: exit is not a function @ file '~/Applications/Scripts/test.js' [line:51, col:1]
Photoshop CC 2019 MacOS Mojave
Copy link to clipboard
Copied
I too get the "exit is not a function" error running in Illustrator 2020. Trying to learn how to make dialogs.
Copy link to clipboard
Copied
Brief explanation:
When working with exit() make sure that you are running the code with InDesign directly.
Alternatively wrap your code into a function and use return instead.
Regards,
Uwe Laubender
( ACP )