Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does this AppleScript get an "AppleEvent handler failed" error on Mac OS X 10.5 Leopard?
    text
    copied!<p>First, I know, Leopard is the old-fogey at this point, but it's the minimum I'd like to be able to support with this script, so please bear with me (it's not even possible under Mac OS X 10.4 Tiger due to a <a href="http://www.makkintosshu.com/journal/mails-broken-replyredirectforward-applescript-support" rel="nofollow">bug w/Mail <code>redirect</code>/<code>forward</code>/<code>reply</code> bug I had uncovered long ago</a>).</p> <p>I've been revisiting building an AppleScript to work in conjunction with <a href="http://osx.topicdesk.com/content/view/37/58/" rel="nofollow">Topic Desk's <code>spamtrainer</code></a>, so it goes through a selection of messages in the Junk folder and attempts to redirect them to a specified mailbox. Here's what I have so far (note that the actual sending is commented out at the moment, more on that later):</p> <pre><code>(* Train Spam - Redirect (not forward) email from the Junk folder (which hasn't already been redirected or * determined by the mail server to be spam) to an appropriate spam mailbox on the mail server. * Developed to be used in conjunction w/spamtrainer on a Mac OS X Server mail server. * * v0.1 2011-07-27 - Morgan Aldridge * Initial version. *) using terms from application "Mail" on perform mail action with messages selectedMsgs in mailboxes selectedMailboxes tell application "Mail" -- configuration variables set junkPrefix to "***JUNK MAIL***" set junkRecipient to "junkmail@domain.tld" -- ensure that we're in the Junk mailbox -- (it'd be disasterous to accidentally redirect mail for training from another mailbox) if (count of selectedMailboxes) is 1 and (first item of selectedMailboxes) is junk mailbox then set selCount to (count of selectedMsgs) set redirectedCount to 0 repeat with counter from 1 to selCount set msg to item counter of selectedMsgs -- if the subject doesn't start with junkPrefix and the message hasn't already been redirected, then redirect it -- (otherwise, if it starts with junkPrefix, it means it was already detected as junk by the mail server) -- (and, obviously, if it was already redirected, that was probably for the sake of junk training as well) if subject of msg does not start with junkPrefix and not was redirected of msg then set newRedirectMsg to redirect msg with opening window tell newRedirectMsg -- set the to recipient to that of the specified spam mailbox on the mail server make new recipient at beginning of to recipients with properties {address:junkRecipient} -- remove any bcc or cc recipient (we don't want to be spamming anyone else in the process) delete bcc recipients delete cc recipients end tell -- actually send the message -- send newRedirectMsg else display dialog "Oops, the message was already flagged as junk by the mail server or you've already redirected it!" end if end repeat else display dialog "Oops, you're not in your Junk mailbox!" end if end tell end perform mail action with messages end using terms from -- this is required when _not_ running from the Script menu (e.g. Script Editor, FastScripts, etc.) using terms from application "Mail" on run tell application "Mail" to set sel to selection tell application "Mail" to set selBox to selected mailboxes of message viewer 1 tell me to perform mail action with messages (sel) in mailboxes (selBox) end run end using terms from </code></pre> <p>When running it from <code>Script Editor.app</code> (so it goes through the <code>on run</code> and <code>tell me to</code>) and a small number of Junk messages select in Mail (this is under Mac OS X 10.5.8 Leopard), it seems to work for the first message (opening a new redirected message, w/to recipient set, and CC/BCC fields emptied), and the other redirected message windows open, but their To/CC/BCC fields are not updated and <code>Script Editor.app</code> pops up the dialog with the "Mail got an error: AppleEvent handler failed" error. The contents of the Event Log are as follows:</p> <pre><code>tell application "Mail" get selection {message id 464214 of mailbox "Junk", message id 464213 of mailbox "Junk", message id 464211 of mailbox "Junk"} get selected mailboxes of message viewer 1 {junk mailbox} get junk mailbox junk mailbox get subject of message id 464214 of mailbox "Junk" ": Your Invitation Into a Global Directory" get was redirected of message id 464214 of mailbox "Junk" false redirect message id 464214 of mailbox "Junk" with opening window outgoing message id 400031520 make new recipient at beginning of every to recipient of outgoing message id 400031520 with properties {address:"junkmail@domain.tld"} to recipient 1 of outgoing message id 400031520 delete every bcc recipient of outgoing message id 400031520 delete every cc recipient of outgoing message id 400031520 get subject of message id 464213 of mailbox "Junk" "Nominate Your Favorite Products for the Community Choice Awards" get was redirected of message id 464213 of mailbox "Junk" false redirect message id 464213 of mailbox "Junk" with opening window outgoing message id 378471104 make new recipient at beginning of every to recipient of outgoing message id 378471104 with properties {address:"junkmail@domain.tld"} "Mail got an error: AppleEvent handler failed." </code></pre> <p>So, clearly it seems like the failure is with setting the <code>to recipients</code> in all but the first message. Subsequent runs of the script without relaunching Mail result in failures on the first message as well.</p> <p>The oddest part of this issue is that, when quitting mail after running the script, there's a hidden, blank window for <em>each</em> of the redirected messages (even the one that was successful) that pops up asking if you want to save changes or not. This sounds <em>very</em> similar to something noted in <a href="http://hintsforums.macworld.com/showthread.php?t=110845" rel="nofollow">this thread</a>, but I'm not sure what causes it.</p> <p>So, what's causing this error and failure? How can I resolve it? And, for extra credit, why are the hidden, blank messages created and how can I prevent that from occurring?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload