开发者

AppleScript - interacting with a dialog window

开发者 https://www.devze.com 2023-03-17 14:15 出处:网络
I have this AppleScript: tell application \"Finder\" to display di开发者_运维问答alog \"derp\" -- display a dialog

I have this AppleScript:

tell application "Finder" to display di开发者_运维问答alog "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.


The "display dialog" command includes a giving up after [number] parameter, which dismisses the dialog automatically after [number] seconds. A quick example:

tell application "Finder" to display dialog "Quick, press a button!" buttons{"1","2","3"} default button 1 giving up after 5

This code produces a dialog containing three buttons. You can click any of them as long as you do it within the time specified (in this case, 5 seconds). If you fail to do so, the "dialog reply" record returned by the command will be something like this:

{button returned:"1", gave up:true}

I hope this helps! :)

0

精彩评论

暂无评论...
验证码 换一张
取 消