Mail rules with AppleScript timing out

Hi all,


Now that I switched to ML it seems my mail rules with AppleScripts time out most of the times. The console shows this error message:


Mail[1440]: Failed execution of AppleScript: /Users/Username/Library/Application Scripts/com.apple.mail/NuvieSnapshotMAIL.scpt

Error Domain=NSPOSIXErrorDomain Code=2 "The operation couldn’t be completed. /Users/Username/Library/Application Scripts/com.apple.mail/NuvieSnapshotMAIL.scpt: execution error: Mail got an error: AppleEvent timed out. (-1712)

˘ " UserInfo=0x7f9fb2d648f0 {NSURL=file://localhost/Users/Username/Library/Application%20Scripts/com.apple. mail/NuvieSnapshotMAIL.scpt, NSLocalizedFailureReason=/Users/Username/Library/Application Scripts/com.apple.mail/NuvieSnapshotMAIL.scpt: execution error: Mail got an error: AppleEvent timed out. (-1712)

˘ }


the Mail rules triggers on SVN commit messages. For example:


If *ALL* of the following conditions are met:

Message content contains nuvie/trunk +

Any Recipient contains nuvie-svn@lists.sourceforge.net


Perform the following actions:

Run AppleScript NuviesnapshotMAIL


If I run the Applescript manually it runs fine.


The script looks like this


tell application "Mail"

activate

set snapshotdialog to display dialog "New revision of Nuvie. Build snapshot?" buttons {"Cancel", "OK"} giving up after 180 with icon caution with title "Nuvie Snapshot"

if button returned of snapshotdialog = "OK" or gave up of snapshotdialog is true then

activate application "Terminal"

delay 2

tell application "System Events" to tell process "Terminal"

keystroke "t" using {command down}

end tell

tell application "Terminal"

repeat with win in windows

try

if get frontmost of win is true then

do script "cd ~/code/sh; . nuviesnapshot.sh" in (selected tab of win)

end if

end try

end repeat

end tell

end if

end tell


This worked fine in 10.6 and 10.7.


Anyone got an idea what is going wrong there or should I file a bug report with Apple?

Posted on Aug 12, 2012 6:13 AM

Reply
6 replies

Aug 12, 2012 6:27 AM in response to Gnarlodious

Hmm, unfortunately "Enable access for assistive devices" was already enabled. Why would that play a role, though (out of interests sake)?


I wouldn't mind some pointers for trimming that script (I already trimmed it, you should have seen its beginning 🙂). One thing I *thought* I could do without was the Mail tell block but I found that without it or not having everything in the Mail tell block, made the script fail to run at all or never get to the foreground (if Mail wasn't in front) or never run...

Aug 12, 2012 6:45 AM in response to GigDR

Well then I don't know. Applescript Editor doesn't offer much for debugging. Even a free editor will give more information, and make it easier to comment out blocks of code so you can isolate the problem.


Anywhere Applescript says keystroke "t" using {command down} means it is using GUI scripting, a technology that is slow and unreliable. GUI Scripting requires that "Assistive Devices" be enabled for some unknown reason, which means a totally obscure setting can break all your GUI scripts. In your case, creating a new Terminal window can be accomplished without GUI scripting, which means your script is way old. You could replace that whole GUI mess with something like:


tell application "Terminal"

if not (exists window 1) then do script ""

do script "cd ~/code/sh; . nuviesnapshot.sh" in front window

end tell

If you got the original script from Nuvie, they may have an update that will work in 10.8. Just keep in mind that Mail.app is now being optimized for iCloud compatibility so a lot of old functionality has been removed.


Aug 12, 2012 11:08 AM in response to Gnarlodious

Thanks for explaining the relation of GUI scripting and "Assistive devices".


The reason why I'm using the GUI scripting is that there doesn't seem to be another way to open a new tab in Terminal. I could make it open a new window instead, but I like the cleaner look of tabs. Running it in an existing terminal window is something I want to avoid at all cost so the stuff I'm working on is not affected by building snapshots (the script changes a lot of developer environment variables).


The script was made by myself for other projects I'm doing automatic snapshots for, so there is no newer one by Nuvie 🙂


Thanks for your help so far.

Aug 14, 2012 9:12 AM in response to GigDR

Kind of solved it. Apparently the "giving up after 180" part is the culprit. More than 119 seconds makes Mail lose its temper and it aborts the seemingly timed out event.


Since I already logged a bug report with Apple I'll update that bug report with these findings. Perhaps someone else can confirm this so I know it's not just my machine that stalls at 120 seconds upwards...

Nov 18, 2013 10:22 PM in response to GigDR

had a similar issue with emails with very large attachements. The Mail rule timed-out at 120 seconds, which was before the attachment fully downloaded / cached locally.


found one solution that may work for you: wrap the tell in a with timeout.


the below structure worked for me (allows 10 minutes for the message to fully download):



using terms from application "Mail"

on perform mail action with messagestheMessagesfor ruletheRule

with timeout of 600 seconds

tell application "Mail"

-- do commands...

end tell

end timeout

end perform mail action with messages

end using terms from




note: the above works on Mountain Lion. And the with timeout was not needed on Snow Leopard.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Mail rules with AppleScript timing out

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.