I have this AppleScript:
tell application "Finder" to display dialog "derp" -- display a dialog
tell application "System Events" to keystroke return -- dismiss that dialog by simulating the pressing of the "return" key
and when it is executed, I thought that the dialog would be dismissed by simulating the pressing of the "return" key using keystroke return
. Thanks.
Your script won't work. When you tell an application to do something the applescript waits for the application to do it before the rest of the code is performed. As such the script is waiting for the Finder to complete its task before moving on to the system events code. So essentially in your script the system events command isn't run until after the dialog is dismissed which means you can never dismiss a dialog this way.
However, you can tell applescript not to wait for a response from an application like this...
ignoring application responses
tell application "Finder"
activate
display dialog "blah"
end tell
end ignoring
delay 0.5
tell application "System Events" to keystroke return
Since applescript is single threaded, another way would be to use two separate processes. One to show the dialog and a second to dismiss the dialog. You could do that with 2 different applescripts, one for each task. Another way would be to use the shell to create one process, then you send that process to the background so the applescript doesn't wait for the shell to finish, and then dismiss the dialog. Here's how you could do it that way.
do shell script "osascript -e 'tell application \"Finder\"' -e 'activate' -e 'display dialog \"blah\"' -e 'end tell' > /dev/null 2>&1 &"
delay 0.5
tell application "System Events" to keystroke return
So you see there's several ways to do this. Good luck.