Run script after window loads

Hi all!

I am writing up a program and I need some help with initial window loading.

When the program starts up I have two windows. One is visible at launch time, the other is not. The user enters some data into the first window and clicks a button. The button sets the visibility of the 1st window to false and the visibility of the second window to true. Now I want the next part of the script to happen automatically when the second window appears. How would I go about doing this? (I’ve played around with a few different options and none really seem to work. I get the most success with “became main” but then it runs again if I click to a different program and then click back.)

Thanks a ton!

Can you tie the event you need to happen to the “on clicked” event that makes the window visible? That seems to me the easiest way to make sure it will only happen when you first make the window visible.

Otherwise, maybe you can use “became main” but also have a variable you set that keeps track of whether you have made the window visible already.

Something like (my syntax may not be exactly correct):

on became main
if iAlreadyActivatedTheWindow then
   --do nothing
else  --window is being set to visible for the first time
   --Do your code that needs to happen when the window first opens
   set iAlreadyActivatedTheWindow to true
end if

You would need to have a close button so you can add an on clicked task to set the variable back to false
on clicked theObject
set visible of window “theWindow” to false
set iAlreadyActivatedTheWindow to false
end

simply test if the visible of window 2 is true and if it is execute your code.

I thought of that, but that would need to be in the on clicked event before setting the window to be visible. If it was in the became main section it would always be true as you are setting the window to be visible before the became main secion would run.

If possible, I still think that trying the code that needs to run when the window becomes visible to the on clicked event would be the easiest and involve the least amount of code.

Thanks for the responses! I have implemented your solution Matt-Boy and it seems to be working nicely. Thanks again.