Mail.app Rule-Triggered Script Sends The Wrong Data?

Here’s a weird one…

I was writing (what I thought was) a simple AppleScript to use in a tutorial on my 10.6.2 system.

The script is initiated by a Mail.app rule and is designed to export the content of messages that trigger it to a pre-determined folder. (Source Code Below)

When selecting the message and applying the rule manually, everything proceeds as expected.

When triggered automatically, the script exports the content of the previous message to the resulting text file.

HUH?!?!? :confused:

Any knowledge about why this is happening, fixes, or suggestions for a different way to go about things would be appreciated!

Kind Regards,

Justin

using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		repeat with aMessage in theMessages
			set theContent to the content of aMessage as text
			set theID to the id of aMessage as string
			(*IS THE EXPORT FOLDER ON THE DESKTOP?*)
			tell application "Finder"
				if (the folder "Temp Export Folder" exists) then set FolderLoc to (the folder "Temp Export Folder")
				if not (the folder "Temp Export Folder" exists) then
					set FolderLoc to (make new folder at (path to desktop folder) with properties {name:"Temp Export Folder"})
				end if
				set SaveLoc to FolderLoc as string
			end tell
			(*SET THE FILEPATH *)
			set theFilePath to SaveLoc & "Message_Number_" & theID & ".txt"
			(*EXPORT MESSAGE TO TXT FILE *)
			set theFileReference to open for access theFilePath with write permission
			write theContent to theFileReference as «class utf8»
			close access theFileReference
		end repeat
	end perform mail action with messages
end using terms from

Model: Macbook
Browser: Safari 531.21.10
Operating System: Mac OS X (10.6)

An Addendum:

A little Google-fu suggests that this may be a Snow Leopard-related bug:

Comment Thread – http://www.cupcakewithsprinkles.com/growl-notifications-for-apple-mail-on-mac-os-x-snow-leopard-10-6/

From stackoverflow – http://stackoverflow.com/questions/1597730/applescript-rule-action-appears-to-be-passed-the-wrong-message-in-apple-mail

Anyone care to speculate as to why this is happening – or want to try this script on both Leopard and Snow Leopard systems to see if they can replicate/isolate the issue?

Hey Justin,

I’m not sure why it’s causing the problem, but in my testing, the issue appears to be the following line…

set theID to the id of aMessage as string

Try changing this to get the message id instead of the id. This seems to resolve the problem for me, although it will lengthen your file names.

using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		repeat with aMessage in theMessages
			set theContent to the content of aMessage as text
			set theID to message id of aMessage as string
			(*IS THE EXPORT FOLDER ON THE DESKTOP?*)
			tell application "Finder"
				if (the folder "Temp Export Folder" exists) then set FolderLoc to (the folder "Temp Export Folder")
				if not (the folder "Temp Export Folder" exists) then
					set FolderLoc to (make new folder at (path to desktop folder) with properties {name:"Temp Export Folder"})
				end if
				set SaveLoc to FolderLoc as string
			end tell
			(*SET THE FILEPATH *)
			set theFilePath to SaveLoc & "Message_Number_" & theID & ".txt"
			(*EXPORT MESSAGE TO TXT FILE *)
			set theFileReference to open for access theFilePath with write permission
			write theContent to theFileReference as «class utf8»
			close access theFileReference
		end repeat
	end perform mail action with messages
end using terms from

Hope this helps.

-Ben

Ben,

Thank you so much for lending your time and expertise to helping figure this out!

Your revised code works when processing messages that arrive via IMAP/POP accounts… but, to me, it looks like mail arriving via an Exchange account still sends the wrong data through the script.

I tried sending sample messages with every possible permutation of account type and, in my case, only the messages sent to my Exchange server show this issue.

Anyone else with both Exchange and POP/IMAP willing to help test this out this theory (or who has noticed other Exchange-related anomalies with scripts run from a Mail rule)?

Hey Justin,

Sorry, I don’t have an exchange account with which to test. In my previous tests, the id line was causing the problem. One suggestion might be to disable this line all together, and try saving your files using a different name, such as seconds since midnight…

set theDate to current date
set theMidnightDate to date (date string of theDate)
set theSecondsSinceMidnight to theDate - theMidnightDate

I can also tell you that there are some miscellaneous buggy issues with Mail rule scripting in Snow Leopard (maybe Leopard too). I’ve already filed several bug reports about these. I haven’t seen this specific issue before, but if you can reproduce it, I encourage you to let Apple know about it too… http://bugreporter.apple.com.

-Ben

Yeah – for the purposes of getting my post online, I’ll find something else to change it to… and, again, I appreciate all your help!

So, if anyone else here has an Exchange account and can confirm that this is a bug, I’ll go ahead and put it on Apple’s RADAR! :wink:

I have a user of one of my Scripts having what appears to be this same issue. Mail.app Rule is triggered by an incoming Exchange email.

The script attempts to process the message within the normal: repeat with eachMessage in theMessages

But the script cannot get any info back on the message from either properties, sender, date received.

The script does not try and obtain the id or message id.

I cannot believe this is still not fixed. Ok I can bt has any one come up with a work around…

Cheers.