Applescript Rule not being applied to incomming messages in Mail.app

This is the only rule i have enabled in mail.app. I want this to be applied to incoming email messages but it never gets run! However if i click on the email message and manually apply the rule it will do what i ask flawlessly. The rule is only supposed to run if the given email message was addressed from my email address. The reason why i have this is because whenever i send an email out, mail.app stores a copy in my “sent” folder. But whenever mail.app downloads the next batch of emails from gmail.com, it gets a duplicate copy of that email.

Rule: If any of the following conditions are met:
From Contains “myusername@gmail.com

Perform the following actions:
Run Applescript “~/path/to/script.scpt”


using terms from application "Mail"
	on perform mail action with messages message_list for rule this_rule
		tell application "Mail"
			set last_message to message 1 of mailbox "Sent Messages" of account "GMAIL"
			set last_message_id to the message id of last_message
			if message_list ≠ {} then
				repeat with received_message in message_list
					try
						set received_message_id to the message id of received_message
						if received_message_id is equal to last_message_id then
							delete received_message
						else
							move received_message to mailbox "Sent Messages" of account "GMAIL"
						end if
					end try
				end repeat
			end if
		end tell
	end perform mail action with messages
end using terms from

Since im working with multiple email clients, I dont always want to erase that duplicate email. The point is that the script works when i apply it to an email but doesnt get run automatically when i receive an email. Any ideas?

thanks

Model: powerbook g4
AppleScript: 1.10.7
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

Hi,

I don’t know, why the script isn’t working properly when attaching it to a rule,
but you can delete the line if message_list ≠{} and the correspnding end if
because there is at least one message, if the script is triggered by the rule

In every case a loop with the repeat with anItem in theItems form, which has an empty list
will never passed through

Ah, i actually put in some display dialog debug statements and it appears that the script IS running, the problem is on the line delete received_message. It doesnt delete the message! So i changed it to moved the messages directly to the trash folder instead.

using terms from application "Mail"
	on perform mail action with messages message_list for rule this_rule
		tell application "Mail"
			display dialog "Running Dup Email Deleter Script!"
			set last_message to message 1 of mailbox "Sent Messages" of account "GMAIL"
			set last_message_id to the message id of last_message
			repeat with received_message in message_list
				try
					set received_message_id to the message id of received_message
					if received_message_id is equal to last_message_id then
						-- delete received_message
						move received_message to mailbox "Deleted Messages" of account "GMAIL"
						display dialog "Moved message to Trash"
					else
						move received_message to mailbox "Sent Messages" of account "GMAIL"
						display dialog "Moved message to Sent Messages"
					end if
				end try
			end repeat
		end tell
	end perform mail action with messages
end using terms from

That sort of works, but now Mail.app is crashing randomly when the script is being run! Oh lord! Could it be the dialogs? This script seems simple enough that it should not be crashing mail.app! Maybe All the moving messages around is messing up with Mail.app’s way of processing the messages that are coming in…