Welcome Dialog

Welcome to the Community!

We have a brand new look! Take a tour with us and explore the latest updates on Adobe Support Community.


Script UI Cancel button still runs the script

Community Beginner ,
Jan 02, 2018 Jan 02, 2018

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

       }

}

TOPICS
Scripting

Views

2.2K

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Guide , Jan 02, 2018 Jan 02, 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.acti...

Likes

Translate

Translate
Guide ,
Jan 02, 2018 Jan 02, 2018

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

       }

}

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 02, 2018 Jan 02, 2018

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?

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 28, 2019 Oct 28, 2019

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

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Aug 28, 2021 Aug 28, 2021

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.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Aug 31, 2021 Aug 31, 2021

Copy link to clipboard

Copied

LATEST

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 )

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines