User interaction disallowed -- Dialog toolkit giving up

I have an app that uses dialog tool kit in a repeat loop. It displays the dialog tool kit window, and if the user does not interact it gives up after a set number of seconds, then repeats (each time with updated info).

It works perfectly, until some time after the mac goes to sleep (but not immediately) the script throws an errorL

'AppleScript Execution Error
User interaction disallowed’

So it seems like either the giving up, or displaying the window, is seen by the mac as user interaction when the mac is in sleep mode.

Any suggestions on how to avoid this? I simply trap for the error, if it’s unavoidable.

Ed, I guess you’ve checked the Accessibility settings.

I sometimes get that error when my applet is not frontmost and it’s code tries to do something. Only solution I’ve found so far is to add “tell me to activate” before code which does something.

Thanks, Garry, It’s strange, the activate command only delays the error by a few minutes.

Accessibility settings are updated and correct. This only happens a few minutes after the mac has gone to sleep, but not right away after sleep begins.

I assume you’re using the “giving after” command for this.
It simply dismisses the window.
Do you explicitly quit your application to ensure it’s not continuing to run? (or generate error -128)

Yes I do use giving up after, but, no, the app doesn’t quit.

The dialog is in an endless repeat loop and and if the user doesn’t interact in time the dialog closes, the app does it’s next iteration of processing and displays a new dialog.

Yep, you’re bumping into one of those classic AppleScript quirks. When the Mac goes to sleep or the screen locks, it just refuses to let scripts put stuff on the screen or interact with the UI. That’s where the “User interaction disallowed” (-1713) error comes from—basically, AppleScript is saying “nobody’s home, try again later.”

A lot of folks also trap for error -1712, which is the timeout error (“AppleEvent timed out”). Here’s why:
When your dialog or AppleScript command is waiting for user input, and the Mac is asleep or locked, sometimes the system doesn’t immediately throw -1713. Instead, it might just sit there waiting…and waiting…until it gives up and times out. That’s when you get -1712. So both errors are signs that “the user isn’t available right now,” just in slightly different flavors.

Here’s a little skeleton I like to use:

   set retryCount to 0
set maxRetries to 30 -- Or whatever makes sense for your use

repeat
    try
        -- Your dialog display code here
        exit repeat -- success, so break out of the loop
    on error errMsg number errNum
        if errNum = -1712 or errNum = -1713 then
            set retryCount to retryCount + 1
            if retryCount > maxRetries then
                display dialog "Giving up after waiting for user interaction." buttons {"OK"}
                exit repeat
            end if
            delay 5 -- wait a bit, then try again
        else
            error errMsg number errNum -- re-throw anything unexpected
        end if
    end try
end repeat

If you don’t want your script running forever when the Mac is locked, just toss in that maxRetries thing. Otherwise, yeah—trap the error, chill for a few seconds, and try again. There’s no real way around it, since the system literally won’t let you show dialogs until someone wakes it up.

So yeah—catching both -1712 and -1713 just covers your bases for when the Mac is locked, asleep, or otherwise not letting AppleScript play with the UI. That way, your script doesn’t crash, and as soon as someone comes back to the Mac, the dialog can pop up like nothing happened.

Hope that clears it up! AppleScript UI scripting is a weird world sometimes, but catching these errors is honestly the best you can do.

Cheers,
Antonio