Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Mail Rules Applescripts Works when "Applied" but not on incoming

Does anyone have a clue why a basic mail rule applescript would work fine when a message(s) is selected and the "Apply Rules" menu comannd is performed, but does not execute at all on the exact same email is received and the mail rules are excuted automatically.


Very fustrating to try to figure this one out. How could the enviroment change the execution (or nonexecution) of the script.


I am running Lion.

Posted on Jan 10, 2012 10:14 AM

Reply
21 replies

Jan 10, 2012 11:01 AM in response to Eric Brown2

First thing I'd think of is that the rule is never reached. Rules are run in order and if a prior rule stopped rule evaluation the new rule may never be reached.


Try moving the new rule to the top of the list to make sure it does get evaluated. You could also add the set color of mail action to this rule before the AppleScript runs to see if this rule is being evaluated.


regards

Jan 10, 2012 12:52 PM in response to Eric Brown2

You need to be more specific about what is and isn't working.


From your post you say that the sound plays, therefore the script IS running.


If it's not doing what you EXPECT then it's likely a flaw in the script logic, in which case it would help if you posted the script for others to see.

There is a known, long-standing issue that prevents Mail.app from sending outgoing mails based on an incoming message, so that could be an explanation if that's the part that's failing, but right now we're blind as to what the issue might be.

Jan 10, 2012 1:17 PM in response to Eric Brown2

Thanks again and I understand you point. Let me be clear. There is a Mail rule that executes an applescript. I want this applescript/rule to execute on INCOME messages (yes, I am aware that Mail does not support outgoing mail rules). So the rule says "if true, execute the applescript", more specifically if it is a certain recipiant, it should execute the Applescript.


The Applescript and the rule works just fine when I select a message with the recipent that will make my rule logic true and I use "Apply Rules". When I receive that exact same message (sent to me), it does not work.


To test that the rule was executing, I added "play sound" right before "Run Applescript", and had the message resent. I received the message, the sound plays and my applescript does not perform.


I select that just received message, perform "Apply Rules", it plays the sound and the script works. So I am left to believe that the execution of "Apply Rules" places the script into an environment diferent then when it is receiving incoming messages wher ehen the latter, the code no long executes. Right now I can not even say that rule runs the script. The rule definately runs, but I am not sure of the script.


From a scripting standpoint I must have the code correct, otherwise it would not work when I use "Apply Rules", but it does, perfectly.


Hopefully this helps. Thanks again.

Jan 10, 2012 1:26 PM in response to twtwtw

As requested:

User uploaded file

User uploaded file


Here is the actual script:


-- Check if Growl is running:

tell application "System Events"

set isRunning to (count of (every process whose bundle identifier is "com.Growl.GrowlHelperApp")) > 0

end tell

--Only display notifications if Growl is running:


if isRunning = false then


-- display dialog "Growl is not running"

end if



tell application "GrowlHelperApp"


-- Make a list of all the notification types


-- that this script will ever send:


-- set the allNotificationsList to {"New Email"}

set the allNotificationsList to {"MailNotify1", "MailNotify2"}



-- Make a list of the notifications


-- that will be enabled by default.


-- Those not enabled by default can be enabled later


-- in the 'Applications' tab of the growl prefpane.


-- set the enabledNotificationsList to {"New Email"}

set the enabledNotificationsList to {"MailNotify1"}



-- Register our script with growl.


-- You can optionally (as here) set a default icon


-- for this script's notifications.


-- register as application "Mail" all notifications allNotificationsList default notifications enabledNotificationsList icon of application "Mail"


registeras application "MailRulesScript" all notificationsallNotificationsListdefault notificationsenabledNotificationsListicon of application "Mail"

end tell


-- Mail Rule Trigger

-- tell application "Mail"

--set theSelectedMessages to selection

using terms from application "Mail"

on perform mail action with messagestheSelectedMessagesfor ruletheRule

repeat with thisMessage in theSelectedMessages


-- Process the current message


-- Grab the subject and sender of the message

set growlSubject to subject of thisMessage & "***xx"

set growlSender to my ExtractName(sender of thisMessage)


-- Use the first 100 characters of a message

set growlMessage to (content of thisMessage)

set growlLength to (length of growlMessage)


if growlLength > 100 then

set growlMessage to (characters 1 through 100 of growlMessage) & "
"

end if


set growlMessage to growlSubject & "


" & growlMessage



-- Send a Notification

tell application "GrowlHelperApp"


show rollup


notifywith name "MailNotify1" titlegrowlSenderdescriptiongrowlMessageapplication name "MailRulesScript"

end tell

end repeat


--end tell

end perform mail action with messages

end using terms from

-- *ExtractName*

--

-- gathers the name portion from the "From: " line

--

-- Source: robJ [http://forums.macosxhints.com/archive/index.php/t-19954.html]

to ExtractName(sender_)

if sender_ begins with "<" then

return text 2 thru -2 of sender_

else

set oldTIDs to text item delimiters

try

set text item delimiters to "<"

set name_ to first text item of sender_

set text item delimiters to oldTIDs

on error

set text item delimiters to oldTIDs

end try

return name_

end if

end ExtractName


---------


Thanks again for the help.

Jan 10, 2012 5:20 PM in response to Eric Brown2

Okay determine the problem, I have no solution and it is very strange.


It appears that something is happening to the data when the Applescript is executed on incoming message then when it is executed under "Apply Rules". Here what I did:


I used "say" commands to follow the execution of the script. Everything is fine until after the "Repeat" statement. After the Repeat statement my "say" commands still function. As soon as I execute any "set" command to get fields from the data, we stop. Nothing from that point forward happens. See the script below:


using terms from application "Mail"

on perform mail action with messagestheSelectedMessagesfor ruletheRule

say "Here 1"


set theCount to counttheSelectedMessages

say "the number of messages is " & theCount as rich text

repeat with thisMessage in theSelectedMessages


say "Hear 4"

say "hear is 4"


-- Process the current message


-- Grab the subject and sender of the message


--set growlSubject to subject of thisMessage


--set growlSender to extract name from sender of thisMessage


--set growlSender to "ME"

say "hear 5"


-- Use the first 100 characters of a message

set growlMessage to (content of thisMessage)

set growlLength to (length of growlMessage)

say "Yep"

if growlLength > 100 then

set growlMessage to (characters 1 through 100 of growlMessage) & "
"

end if


say "I am Here"

set growlMessage to growlSubject & "


" & growlMessage

say "ready to send note"


-- Send a Notification

tell application "GrowlHelperApp"


show rollup


notifywith name "MailNotify1" titlegrowlSenderdescriptiongrowlMessageapplication name "MailRulesScript"

end tell

say "note sent"

end repeat


--end tell


--end perform_Mail_action

end perform mail action with messages

end using terms from


---------


It says "Hear 5", but no "Yep". Again using Apply Rules, we are good.

The script says "Hear 5", but no "Yep". Again using "Apply Rules", we are good. No matter what I do, after the first "set" command in the loop, we are done. If I uncomment a set command before "Hear 5", it never get there. Comment the set command out....works.


Strange. Can anyone recreate this problem. I did a plain venilla version of this code and have the same issue. Look like a bug?

Jan 10, 2012 6:36 PM in response to Frank Caggiano

I'm curious why you're not using the GrowlMail plugin that's shipped with Growl. you seem to be duplicating its function.


That aside, I'm pretty sure you are either running into a threading problem or a latency problem.


Threading problem: Mail processes incoming messages on separate threads, but may process the 'Apply Now...' command on the main thread. Applescript is not threaded, and will not work across threads.


Latency problem: Mail triggers rules when it first gets the headers for emails, but email servers don't pour data downstream the way web servers do, so there is usually a noticeable lag between when the headers arrive and when the body is finished arriving. Applescript (which is notoriously bad at metaphysics) will have trouble querying the contents of an email that hasn't arrived yet. (again, this won't affect 'Apply Now...' because the message bodies are already downloaded)


There's nothing to do about either of those problems, if those are what the problem is, except rewrite your script to accommodate them.

Jan 10, 2012 11:47 PM in response to twtwtw

Thanks all very good insights here!


twtwtw, thanks what you are saying makes sense, but that would means using applescript on a mail rule would yield unpredicable results for anyone who trys. I tried delaying execution of the script (that *****), but no change in the outcome. Tried a different system, no change, same problem. My test messages are very short. Given your theory (which is good), I would expect that one would work every once in awhile. I have failed 100% in my attempt on income emails.


If what you are sayin is true, that the problem is in the collection of the email contents, your right there is little I can do about it.


Also GrowlMail is broken under Lion. Maybe that's why.


Thanks you have been helpful.

Sep 4, 2012 3:45 PM in response to Eric Brown2

Some good points here. Just wanted to say that I'm having the same problem: after upgrading to Mountain Lion the applescript that I use to reply to certain emails has stoped working on incoming emails. I have the script in the "new" correct folder. The funny thing is that the script works when you "apply rules" from the messages dropdown menu.


Any updates to the problem?

Mail Rules Applescripts Works when "Applied" but not on incoming

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