Simple Mail Rule not working.

Hi MacScripters. I’m having a problem with Apple Mail Rules. I just can’t get it to work. Heres the simple script im using just for test purposes:

using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		repeat with thisMessage in theMessages
			set theText to (contents of thisMessage) as string --coercion  probably not necessary 
display dialog theText
			end repeat
	end perform mail action with messages
end using terms from

My rule simply states
If All Conditions Match:
From Contains: Twitter
Subject Contains: Direct message

Then I have it run my script.

I have deleted all other Mail Rules to make sure was no “Stop Evaluating Rules” going on.

However nothing ever happens. Rule never triggers. Even when I click an email and hit Apply Rules nothing happens at all. Any ideas?

EDIT: If I change the action to something like just marking it as flagged it works fine. But running an applescript does not.

Browser: Safari 525.20.1
Operating System: Mac OS X (10.5)

You are not supposed to use interaction in a mail rule script (e.g. display dialog), this is why your script does not work.

Ah ok. Say I have a piece of script that sends a post to Twitter (using cURL) would that work?

Something like this:

property user : "hendo13"
property pass : "mypassword"
set f to (text 1 thru -21 of (path to me as string)) & "ASTwitterLibrary.scpt"
set lib to load script alias f

using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		tell application "Mail"
			repeat with thisMessage in theMessages
				set theText to (contents of thisMessage) as string
			end repeat
		end tell
		lib's postTweet(user, pass, "@chtestaccount thanks for the email!")
	end perform mail action with messages
end using terms from

Here’s the script modified but still not working as a rule. However, this script worked fine as a standalone (using the selected message in mail opposed to “on perform mail action blah blah blah…” Very strange

(*BEGIN EDITING*)
property user : "hendo13" --your Twitter username
property pass : "mypass" --you Twitter Password
property theReply : "" --enter your message here.
(*END EDITING*)
property theRecipient : "" --You don't need to edit this. The script auto-parses the recipient for you.
property theText : ""
set f to (text 1 thru -21 of (path to me as string)) & "ASTwitterLibrary.scpt"
set lib to load script alias f

using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		tell application "Mail"
			repeat with thisMessage in theMessages
				set theText to (content of thisMessage) as string
			end repeat
		end tell
		
		set AppleScript's text item delimiters to {"http://twitter.com/direct_messages/create/"}
		set theSource to text items 2 thru -1 of theText as string
		set AppleScript's text item delimiters to {" "}
		set theRecipient to text item 1 of theSource
		set AppleScript's text item delimiters to {""}
		set text item delimiters to space
		set theRecipient to theRecipient's text items as string
		
		set text item delimiters to {""}
		--return theRecipient
		lib's postTweet(user, pass, "@" & theRecipient & " " & theReply)
	end perform mail action with messages
end using terms from




I guess the rule will never pass the two set lines after the property lines.

Well I can still move those until after the mail stuff happens right? I’ll give it a try.

EDIT: Nope that didn’t work. I guess I’ll just have to use the actual code instead of the library.

Well that’s not working either.

(*BEGIN EDITING*)
property user : "hendo13" --your Twitter username
property pass : "password" --you Twitter Password
property theReply : "Thanks for testing for me!" --enter your message here.
(*END EDITING*)
property theRecipient : "" --You don't need to edit this. The script auto-parses the recipient for you.
property theText : ""


using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		tell application "Mail"
			repeat with thisMessage in theMessages
				set theText to (content of thisMessage) as string
			end repeat
		end tell
		
		set AppleScript's text item delimiters to {"http://twitter.com/direct_messages/create/"}
		set theSource to text items 2 thru -1 of theText as string
		set AppleScript's text item delimiters to {" "}
		set theRecipient to text item 1 of theSource
		set AppleScript's text item delimiters to {""}
		set text item delimiters to space
		set theRecipient to theRecipient's text items as string
		
		set text item delimiters to {""}
		--return theRecipient
		
		postTweet(user, pass, "@" & theRecipient & " " & theReply)
	end perform mail action with messages
end using terms from

on postTweet(theUser, thePassword, theMessage)
	set TwitterUpdate to "/usr/bin/curl" & ¬
		" --user " & quoted form of (theUser & ":" & thePassword) & ¬
		" --data status=" & quoted form of encodeURL(theMessage) & ¬
		" [url=http://twitter.com/statuses/update.xml]http://twitter.com/statuses/update.xml"[/url]
	
	-- Now try sending the Tweet to Twitter
	
	
	set TwitterResponse to do shell script TwitterUpdate
	
	if TwitterResponse contains "Could not authenticate" then
		return "Tweet failed to post. Your username or password may be incorrect, or Twitter might be expieriencing difficulties."
	else
		return "Tweet successfully posted"
	end if
	
end postTweet
on encodeURL(someURL)
	return do shell script "/usr/bin/python -c '" & ¬
		"from sys import argv; " & ¬
		"from urllib import quote; " & ¬
		"print quote(unicode(argv[1], \"utf8\"))' " & quoted form of someURL
end encodeURL