MacOS – Messages.app AppleScript handler Override 10 second script timeout

applescriptmacosmessagesscript

I have a working Messages.app Applescript handler. Most of the handlers are empty, except the "on message received" handler. This handler runs a shell script which responds with outputs that are expected to be sent as responses to the message sender.

The issue: My Script takes between 6 and 12 seconds. In the cases it takes over 10 seconds, Messages appears to have a default Applescript handler timeout of 10 seconds. After 10 seconds a popup is delivered and Messages becomes unusable until the "Wait" button is pressed. Upon pressing the button, the handler finishes, and all is well.

Applescript Timout Alert

Possible Solutions:

  • Ideal: Disable/Change the Applescript messages timeout.
  • Make handler script pass enough information to respond to the same message into the script running under nohup or $ [script] & then have the script launch it's own Applescript to respond.
  • make Applescript try to click "Wait" button in messages before responding (not sure if this would work).

Thank you for any help!

Best Answer

Unfortunately Apple hasn't made this easy on us. They have provided no way to change this timeout and sometimes it's the script itself taking too long to run (because Messages.app is terrible at running scripts). I run a rather extensive applescript handler myself, and I interact with Messages.app through AppleScripts run by other processes. Sometimes things freak out and I'm not around to fix it. The workaround I've come up with is to check for a window with the "Wait" button and to click it. Sometimes though, my scripts run right after another and I end up with possibly dozens of windows like the screenshot you pasted. In my case, it is easier to force Messages to quit and then re-open it. I wrote a small AppleScript that does exactly this. You can compile it as an application with "stay open after run handler" selected and it will always make sure Messages is working and open without error windows. You can find it here: https://github.com/davidnewhall/IndigoAppleScripts/blob/master/Restart_Messages.applescript

The other option you could (easily) develop is to run your script in the background and respond once the data is available; that's probably a better approach. You can run osascript from the command line (or a script) to run an AppleScript. There are plenty of examples in my github repo of how to send messages externally.

Good luck!

Related Question