Mail rule applescript

I’m using Tiger, and Mail.app, and I’d like to add a rule that uses an Applescript
to change the encoding of certain messages (with subject starting in LL-L) automatically.

I first experimented to get the encoding part: I didn’t see it in the scriptable stuff in Mail, so
I used GUI-scripting instead:


tell application "Mail" to activate

tell application "System Events"
	tell process "Mail"
		tell menu bar 1
			tell menu bar item "Bericht"
				tell menu 1
					tell menu item "Tekstcodering"
						tell menu 1
							click menu item "Unicode (UTF-8)"
						end tell
					end tell
				end tell
			end tell
		end tell
	end tell
end tell


and this works fine as a stand-alone script: the mail is selected and I choose this script from the menu
and it does its job.

Now I added a rule to Mail to execute the following script whenever the subject was as described:
The above is a handler here (subroutine).


using terms from application "Mail"
	on perform mail action with messages theMessages
		tell application "Mail"
			repeat with eachMessage in theMessages
				select eachMessage
				setUTF8()
			end repeat
		end tell
	end perform mail action with messages
end using terms from

on setUTF8()
	tell application "System Events"
		tell process "Mail"
			tell menu bar 1
				tell menu bar item "Bericht"
					tell menu 1
						tell menu item "Tekstcodering"
							tell menu 1
								click menu item "Unicode (UTF-8)"
							end tell
						end tell
					end tell
				end tell
			end tell
		end tell
	end tell
end setUTF8

But this doesn’t work. Maybe the “select” part didn’t activate the right message?
I don’t see what’s wrong here, do you?

i just tweaked your script to run in scripteditor and its error is, message doesn’t understand select.

sorry i don’t have any suggestions for you at the moment

Thanks for the idea.

I tried this suggestion (with open, and close front window), but it opens the message, but then doesn’t change the encoding,
and the message stayed open, so somehow the setUTF8 handler must have failed…
But when I open the message by hnd and apply the subroutine as a separate script, as before, it does work.
So I’m still mystified.
Does this work at your machine?
Does it matter whether Mail is open, or only in the background etc. ?

Thanks!
If I send myself a test mail, it seems at first not to apply it, but when I use the menu item “apply rules”,
it does work! So maybe Mail doesn’t apply the rules right away? (but when I tell him to move the message
to a mailbox instead of applying a script [testing] it does so right when the message comes in…).
I’ll do some more tests…

But I learnt some new stuff, so thanks for that already!