AppleScript dialog waiting for user input?
February 5, 2010 2:28 PM   Subscribe

Can I make an AppleScript that will display a dialog box, wait for user input, and take an action if the user does not respond to that dialog box after a predetermined period of time?

Basically what I want to do is this:

1. Display a dialog box asking "Are you still there?" with buttons for "Yes" and "I'm leaving now".
2. If the user responds with "Yes", wait 10 minutes and ask the same question again, following the same paths.
3. If the user responds with "I'm leaving now", execute a series of simple actions.
4. If the user doesn't respond to the dialog box for 1 minute, go ahead and act as if the user had responded with "I'm leaving now".

Is there a simple way to pull this off in AppleScript?
posted by joshrholloway to Computers & Internet (5 answers total) 1 user marked this as a favorite
 
The following kind of works:
try
    with timeout of 2 seconds
        tell application "Finder"
            set answer to display dialog "Are you there?" buttons {"Yes", "No"}
        end tell
    end timeout
on error
    set answer to {button returned:"No"}
end try
The problem is that if the timeout is hit, then the dialog box won't go away when the script ends. (Since it's another app showing the dialog on your behalf.) I don't know any way to close it programmatically from AppleScript. Harmless but annoying. If you run display dialog … not inside a tell block, then the timeout logic fails, probably because the script is single-threaded and it's stuck inside a blocking NSAlert() call or similar.

So yes you can do it if you don't mind the dialog hanging around uselessly. And possibly there's an even better method; AppleScript is anything but intuitive.

Also, in testing this I managed to hang Script Editor several times. Nice going, Apple!
posted by serathen at 3:08 PM on February 5, 2010


Best answer: You should put the timeout on the display dialog command, so that the dialog times out. Something like this should work:

on idle
    tell application "Finder"
        activate
        with timeout of 61 seconds
            set r to display dialog "Are you there?" buttons {"Yes", "I'm leaving now"} default button "I'm leaving now" giving up after 60 -- seconds
             if r's gave up or r's button returned is not "Yes" then
                -- do stuff here
             end if
         end timeout
     return 600 -- seconds
end idle

Note I'm not on my Mac now so the above may have errors.
posted by kindall at 3:47 PM on February 5, 2010 [1 favorite]


(I just pasted kindall's example into script editor and it worked fine.)

I'm not sure you need the outer "with timeout of 61 seconds", though, since the "display dialog" will be doing the actual time-out-ing.
posted by hattifattener at 4:36 PM on February 5, 2010


If the timeout of the dialog is longer than AppleScript's default timeout, I believe you'll get an error from AppleScript due to that. It's asking a scripting addition to display that dialog and won't get a response until the dialog times out. I couldn't remember what the default timeout is so I threw that in to make sure it worked.
posted by kindall at 5:31 PM on February 5, 2010


Oh, duh, I didn't catch that. You're using with timeout to extend applescript's default timeout.
posted by hattifattener at 10:08 PM on February 6, 2010


« Older Is it alright to put an oil painting in the trash....   |   Help me remember the name of this 1970's (I... Newer »
This thread is closed to new comments.