-1

Is it really that hard? Is my google-fu obtuse? How the heck do I make a cancel button that CANCELS the execution of the rest of the script?

My script is dumb. I'm not a good coder, I know that, but here's what I've got:

I'm automating INDD CScloud. Script first opens a Window, operator has options as radio buttons. at bottom are standard "OK" and "Cancel" buttons, then the script has some functions that do stuff based on the options shown in the Window. What I want to happen is ALL CODE EXECUTION to STOP when I hit the "Canel" button. Thats it! nothing fancy! but all it seems to do is close the dialog Window and continue without registering changes. GRRR.

here's my crappy code:

var asker = new Window ("dialog","Answer Some Stuff!");
    var firstselector = asker.add ("panel", undefined, "Which Thing First?")
        firstselector.alignChildren = "left"
        firstselector.preferredSize = [250,100]
        thing1a = typeselector.add ("radiobutton", undefined, "Thing 1a")
        thing2a = typeselector.add ("radiobutton", undefined, "Thing 2a")
        thing3a = typeselector.add ("radiobutton", undefined, "Thing 3a")
        thing1a.value = true;
    var secondselector = asker.add ("panel", undefined, "Which Thing Second?")
        secondselector.alignChildren = "left"
        secondselector.preferredSize = [250,100]
        thing1b = secondselector.add ("radiobutton", undefined, "Thing 1b")
        thing2b = secondselector.add ("radiobutton", undefined, "Thing 2b")
        thing3b = secondselector.add ("radiobutton", undefined, "Thing 3b")
        thing1b.value = true;
    var thirdselector = asker.add ("panel", undefined, "Do Thing Three?")
        thirdselector.alignChildren = "left"
        thirdelector.preferredSize = [250,75]
        certyes = thirdselector.add ("radiobutton", undefined, "Yes")
        thidno = thirdselector.add ("radiobutton", undefined, "No")
        thirdyes.value = true;
                asker.add ("button", undefined, "Do It!", {name: "ok"});
                asker.add ("button", undefined, "Cancel", {name: "cancel"});    
asker.show();

var mainPart = function(){
if (thing1a.value == true && thing1b.value == true)
{
do some stuff
}
if (thing2a.value == true && thing1b.value == true)
{
do some different stuff
}
})();
var secondPart = function(){
if (thing3.value == true && thing1b.value == true)
{
do this other stuff
}
if (thing3.value == true && thing2b.value == true)
{
do some other stuff entirely
}
alert("All The Stuff Is Done!");
}
)();

All I want to happen is that when the operator clicks the "Cancel" button the script exits and mainPart and secondPart are NOT executed.

I tried using a .onClick, like this:

var cancelBtn = asker.add('button',undefined,'Cancel');
cancelBtn.preferredSize.width = 175;
cancelBtn.onClick=function(){
  exit();  
 }

but apparently exit() is not valid and the code just keeps going... so i tried break, it errors about "illegal use of break outside a loop" but then proceeded to execute the rest of the code anyway...

help me be less dumb, please.

JawzX
  • 293
  • 1
  • 2
  • 15

1 Answers1

1

Ach, you made me laugh with your self-deprecations.

I got rid of the third part of your code (to simplify and stop getting errors), and fixed a type-o ("thirdelector" instead of "thirdselector"). Also, to get things running, I changed the "typeselector" references to "firstselector" in the first part. I commmented out the "do some stuff" lines. Then I added what I generally use to get the cancel functionality in my scripts ( "this.parent.close()" )

Here is my running script that you can use to get to where you want.

[EDIT - I have added stuff to demonstrate how to get the data to flow correctly. Note that the original question about the cancel button has already been answered. Study this and you should be able to glean basic but powerful ideas from it.]

var asker = new Window ("dialog","Answer Some Stuff!");
    var firstselector = asker.add ("panel", undefined, "Which Thing First?")
        firstselector.alignChildren = "left"
        firstselector.preferredSize = [250,100]
        thing1a = firstselector.add ("radiobutton", undefined, "Thing 1a")
        thing2a = firstselector.add ("radiobutton", undefined, "Thing 2a")
        thing3a = firstselector.add ("radiobutton", undefined, "Thing 3a")
        thing1a.value = true;
    var secondselector = asker.add ("panel", undefined, "Which Thing Second?")
        secondselector.alignChildren = "left"
        secondselector.preferredSize = [250,100]
        thing1b = secondselector.add ("radiobutton", undefined, "Thing 1b")
        thing2b = secondselector.add ("radiobutton", undefined, "Thing 2b")
        thing3b = secondselector.add ("radiobutton", undefined, "Thing 3b")
        thing1b.value = true;

   var cancelBtn = asker.add('button',undefined,'Cancel');
   var okBtn = asker.add('button',undefined,'OK');
   cancelBtn.preferredSize.width = 175;

okBtn.onClick=function(){
  mainPart();
  this.parent.close();  
 }
cancelBtn.onClick=function(){
  this.parent.close();  
 }

asker.show();

var mainPart = function(){

if (thing1a.value == true && thing1b.value == true)
{
 alert('1a and 1b');

 } else if (thing2a.value == true && thing1b.value == true) {
  alert('2a and 1b');
} else {
    alert('something else');
//do some different stuff
}
}
CRGreen
  • 3,406
  • 1
  • 14
  • 24
  • Thanks, I figure if I can't be a good coder I should at least be entertaining ;) On a sad note this does not work :/ – JawzX Jan 20 '15 at 13:37
  • if I make the code `var mainPart = function(){ alert("It Worked!");}` the alert does not pop up with either your cancel button or the standard "OK" button. If I change it to `var mainPart =( function(){ alert("It Worked!");})();` the code runs and the alert pops up no matter which button is clicked... meaning it's not cancelling execution... Is there something fundamental I'm missing about javascript execution? – JawzX Jan 20 '15 at 13:44
  • See edit in post. you can organize the script in two ways (simply put): 1) dialog 'one shot' style (seen in example), or 2) 'on the fly' functionality where the script stays open and you continue to do stuff. See my after effects examples on crgreen.com – CRGreen Jan 21 '15 at 21:16
  • This answer does not work. I have been unable to get back to this project for a while, but the suggestions all result in continued code execution. – JawzX Apr 28 '15 at 16:38
  • I don't know what the problem is that you're having. If I run the code I posted, nothing but the closing of the window happens when I hit "Cancel". You're going to have to explain what you are unsatisfied with. – CRGreen Apr 28 '15 at 17:08