Doing something while a panel is open

I want the computer to speak some text using the say command after a panel opens up. The only problem is it doesn’t say anything until the panel closes with a cancel button I have in it. Heres the code


set thePanel to load panel "Panel" from nib "main"
display panel thePanel
say "whatever"

It doesn’t do the last line of code until the panel closes.

You have different way to manage that. That depends of what you want to do.

Take care that you PERHAPS need to use AppleScript Studio, yesterday I had the same problem and when searching though the help I saw that ASS overides AS Extension Diplay, I can’t help about that. So with ASS I found different ways :

A - You just don’t want a modal dialog
A1 - You have a window openned/visible
Then instead of display use:


display yourPanel attached to theWindow

A2 - You doesn’t have a window opened/visible
A21 - A dirty way


-- DIRTY way
-- Works and make your panel non modal as you want BUT it generates errors in the log.
display yourPanel attached to yourPanel

A22 - Cleaner way
So it’s better not use display at all but instead to load your panel or ensure in Interface Builder that it has been created and not released.
Then:


-- To open the panel in non modal mode :
set yourPanel's visible to true

--
-- You do your stuff here without to wait that the panel is closed
--

-- To "close" the panel but keep it in memory :
set yourPanel's visible to false

B - You want that your panel is modal but want to run some code after it has been opened and before it is closed.
Then you have to connect an event of your dialog to your script in Interface Builder.
For example, “Window->opened” but you’ll need try what event is the best for your need. Then despite the dialog is modal the event will be thrown and will call your code:


on opened theObject
if name of theObject is "ASWindowName" then
--
-- You do your stuff here without to wait that the panel is closed
--
end if