Display dialog error when attached to a window

So this script works :

set question to display dialog (localized string "Is this a question?" buttons {localized string "No!", localized string "Yes!"} default button 2
set answer to button returned of question
if answer is equal to (localized string "Yes!") then
display dialog "ok"
end if

but if I add attached to window “main” after default button 2, then I get an error that the variable question is not defined.

Any help?


if you attach the dialog panel to a window, the dialog ended handler is called, when a button is pressed.
For the differences take a look at this sample project

/Developer/Examples/AppleScript Studio/Display Dialog/

Thanks, but to be sure, I need to copy all the actions in to the dialog ended handler ? (even if it’s like 200 lines ?)

Yes, consider that AppleScript Studio is more handler oriented unlike plain AppleScript which is more “straight forward” oriented

OK, I think it works now, I’ve stripped the Display Dialog example down to this and it works :

on clicked theObject
	set theReply to display dialog "This is a dialog" buttons {"OK", "Not OK"} default button "OK" attached to window "main"
end clicked

on dialog ended theObject with reply theReply
	if button returned of theReply is equal to "OK" then
		display dialog "Hey, this works..."
	end if
end dialog ended

And I don’t need to copy all the lines over again…

Thanks for your help :smiley:
Now I’m searching for getting my properties working with the user defaults but I’m going to look into Apple their documents and the threads here…

EDIT 21:30 (my time)

if I add
if showdialog is true
code from above
end if

then it won’t work… This is normal right ?