Skip to main content
Participant
January 2, 2018
Answered

Script UI Cancel button still runs the script

  • January 2, 2018
  • 2 replies
  • 3757 views

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

       }

}

This topic has been closed for replies.
Correct answer tpk1982

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

       }

}

2 replies

Community Expert
August 31, 2021

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 )

tpk1982
tpk1982Correct answer
Legend
January 2, 2018

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

       }

}

Participant
January 2, 2018

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?