message being application class on incoming mail in Apple Mail

Hello,
I have a quite specific problem and wander if somebody encountered this already.
The idea is to write a Apple Mail script to be used in rules, with the purpose of saving the attachment in the emails into a folder on the disk.

All works fine if the account the email is incoming into is not an Exchange type of account. And even these emails are processed OK if I process them by selecting the messages and applying the rule.

But if the script is invoked at the moment email is incoming, and then account type is Exchange, then it breaks claiming it cannot read the attachment (or whichever other) property of the list item.

As it turns out, if I get the class of the list item, normally it is and should be a message class, but in this particular case it is Application class

Here is an example which can show the error


using terms from application "Mail"
	on perform mail action with messages message_list for rule this_rule
		try
			tell application "Mail"
				set listlength to number of items of message_list as integer
				repeat with theMessage in message_list
					set theMessageClass to class of the theMessage
					set theMessageSubject to subject of theMessage
					display dialog theMessageClass
				end repeat
			end tell
		on error errMsg number errNum
			display dialog "error in main " & errMsg & " " & errNum
		end try
	end perform mail action with messages
end using terms from

Any idea?
Thanks

Hello, this is very interesting!

How about testing for the class of the mail, and if it is application, then wait for a second, and try all over, until your message is of class message, and not application?

Hi,

it might be easier to exclude the accounts in the rule definition

Hello Stefan!

That is obviously the easiest solution, I wonder however, if this is what happenes with pop accounts too.

@McUsr - thanks for the hint, I do not believe that the object I get from the Apple Mail will dynamically change class i.e. to access it after class change I probably need a new handle, but still I will test it and let you know, it will have to wait Monday though as I do not have Exchange account on my home mac, and to sync the one from work would take forever.
Pop accounts are less of a problem to me because I do not use them any more.

@StefanK - sure I can exclude them, but most of the messages are incoming exactly through this account, so it is a bit of a headache. What I do now is, exclude them and then apply the rule by selecting them in the mailbox and right click “apply rules”, this works fine.

I will post the test results.

Hi!

One solution, that may be possible, is to relay your exhange mail through another mail account, that is through something that works with your rule?

I tested it and it does not work.
It is not particularly clever script but this was a shortest test I could do, I simply went through the same list the rule handed over to me after a wait of 10 seconds, the class did not change.

re relying the messages, this would mean that either the sender should send me a message to a different email address, or I would be replying from a different email address. In both cases this would create confusion and would add complexity to the setup (two email accounts to care for) - if I understood you correct, but thanks for your comment anyway.

any other idea?

Tx
Zsolt

using terms from application "Mail"
	on perform mail action with messages message_list for rule this_rule
		try
			tell application "Mail"
				set listlength to number of items of message_list as integer
				repeat with theMessage in message_list
					set theMessageClass to class of the theMessage
					--set theMessageSubject to subject of theMessage
					display dialog "dialog1" & theMessageClass
				end repeat
				delay 10
				set listlength to number of items of message_list as integer
				repeat with theMessage in message_list
					set theMessageClass to class of the theMessage
					--set theMessageSubject to subject of theMessage
					display dialog "dialog2" & theMessageClass
				end repeat
			end tell
		on error errMsg number errNum
			display dialog "error in main " & errMsg & " " & errNum
		end try
	end perform mail action with messages
end using terms from

I meant that you set up a mail acount, and uses that account to receive all the mail you get from exchange server. Then receive the mail from that account, that only contains mail from your exchange account, and is pretty much invisble for everyone, as when you answer your mail, you answer them through your exchange account.

That was what I meant really, in order to make your mail rules work.

It may be a bad idea though, for other reasons, I don’t know how it is to answer mail through another account than the one you received mail with. So, I see problems, about organizing your messages later, and I have no idea if there is any security concerns regarding your email either.

Your company may not be interested in having you relaying your email through a third part account.

As for your script, I think you are right in that the class won’t change within it.

The first things I’d do if I were you, is to see if some third party tool are able to do this, I know there are tools that works with mail rules, maybe one of those are capable.

I don’t like buying out of problems, but if 5-10 bucks gets you out of those problems. and I think you can try before you buy.

Another solution, I kind of think will work, would be to have your mail rule not trying to do something with the messages, but run an external script, that target mail from “outside”, and then deals with the messages.

That approach gets you out of your current context, and though more cumbersome than a normal rule, I think it will work.

You shouldn’t pass anything to the script, just run it, so it gets a new scripting context, and let it do everything by itself.

You’d have to write a file (lockfile to the disk,) so your mail rule knows it is to wait triggering it again, until the other script has finished running.

This is cumbersome, and resource hogging, but I think such an approach may work.

Have you tried setting up the Exchange account to use imap? (in Preferences, accounts)

Hopefully mouramartins advice works!