Page 1 of 1

Can't figure out why this doesn't work right

Posted: October 9th, 2013, 3:13 pm
by Milad
Hi, I'm doing a video tutorial series from FXPHD on AEscripting and I've hit a brickwall. I've typed exactly what they have, but for some reason this isn't working right.


var myWindow = new Window ("dialog", "My Window");

myWindow.okButton = myWindow.add("button");
myWindow.okButton.text = "OK"
myWindow.cancelButton = myWindow.add("button");
myWindow.cancelButton.text = "Cancel"
myWindow.show();

myWindow.okButton.onClick = function() {
alert("OK");
myWindow.close();
};

myWindow.cancelButton.onClick = function() {
alert("Cancel");
myWindow.close();
};


2 things I've noticed when I run it is that it will show the window, but if you click on a button it won't do anything as if it won't let me run inline functions. If I turn those into 2 regular functions and define them after the onClick statements, then what happens is that when you click on one button, both alerts pop up as if it's running one function after another.

I know this is probably a silly question, but i'm at the start of this video and can't move on without finding out why it's acting this way. thank you guys.

Re: Can't figure out why this doesn't work right

Posted: October 23rd, 2013, 10:59 am
by willsummers
I ran into this exact same problem. These inline function will run if the entire script is put in an encompassing function like Main() and run that way.

Re: Can't figure out why this doesn't work right

Posted: December 11th, 2014, 1:19 pm
by bradon
it seems to work fine if you put the show window at the bottom?

Code: Select all


var myWindow = new Window ("dialog", "My Window"); 

myWindow.okButton = myWindow.add("button"); 
myWindow.okButton.text = "OK" 
myWindow.cancelButton = myWindow.add("button"); 
myWindow.cancelButton.text = "Cancel"
 

myWindow.okButton.onClick = function() { 
alert("OK");
myWindow.close();
};

myWindow.cancelButton.onClick = function() { 
alert("Cancel");
myWindow.close();
};

myWindow.show();