Re: Applescript in El Capitan Mail rule processes the wrong message
Re: Applescript in El Capitan Mail rule processes the wrong message
- Subject: Re: Applescript in El Capitan Mail rule processes the wrong message
- From: Christian Boyce <email@hidden>
- Date: Tue, 20 Mar 2018 09:43:58 -0700
Hi friends—
I found a way to make my AppleScript work as part of a Mail rule in El Capitan.
It turns out that the incoming message can be moved, colorized, replied to, etc
perfectly well immediately upon being received, which of course is what a rule
is for. Anything that has to do with one of the built-in rule actions works.
When it comes to an AppleScript, the message has to be added to the Mail
database before one can do things with that message, and apparently this takes
a little bit of time.
I ended up doing two things: in Step 1 of the rule, I move the received piece
of mail to a folder called "Appointment Alerts." In my script, I wait. I put in
a delay 30. Then the rest of my script can operate on the message by looking at
the messages in the folder.
It looks like this:
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
using terms from application "Mail"
on perform mail action with messages theMessages for rule theRule
tell application "Mail"
delay 30 -- to make sure the message is "registered"
with (or saved to) the mail database
set myAccount to first account whose name is
"automatic" -- get the right account
tell myAccount
set the_mailbox to first mailbox whose name is
"Appointment Alerts"
end tell
set theMessages to every message of the_mailbox
set aMessage to item 1 of theMessages
set theContent to content of aMessage
-- then I do stuff with the content
end tell
end perform mail action with messages
end using terms from
I'd read that moving a message into a mailbox would be enough to register it,
but that didn't work. Maybe the delay is all I need. It's working like this so
I'm leaving it at that. Originally I had the delay at 10 seconds and it worked,
but the script runs on an unattended Mac and I don't need it to be instant. I
thought I'd give it extra time, for insurance.
FYI
> On Mar 18, 2018, at 5:32 PM, Christian Boyce <email@hidden
> <mailto:email@hidden>> wrote:
>
> I have a problem with a Mail rule in El Capitan. The rule has an AppleScript
> step (at the end) which does a few things with the content of the incoming
> message. I know the script is fine and I know also the triggering is fine.
> And, this all worked great for a long time on a Mac with 10.7.5. But in El
> Capitan, the AppleScript does not get the content of the received message—
> instead, it gets the content of some other message. I think it's the message
> that was received just before the message I want but I am not sure about that.
>
> Here's the strange part. If a message exists in Mail, and I select it and
> choose Message/Apply Inbox Rules (by hand), the message I've selected is
> processed and my script knows which message to work with. But when the
> message comes in and the rule is triggered automatically, the Rule gets the
> content of the message before it in the message list.
>
> If I throw away all existing messages that would match the Rule's trigger,
> leaving me with a blank Inbox, and then I send myself another message that
> matches the Rule's trigger, the AppleScript STILL works on the wrong message—
> something that's in the trash. It's nutty.
>
> I've read about Mail Rules in El Capitan being buggy with respect to
> AppleScript but there has to be a way to make it work. My rule is definitely
> trapping the right message as I can see that it's changing the message's text
> color, and marking it as "read." But then the AppleScript part, which is the
> last step, uses the content of a different message.
>
> Any suggestions? I sure could use some!
>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden