Scripting for Mail with Hazel [newbie]


I’m a new user of Hazel and have only a very very basic grasp of some AS stuff. (And am not a geek generally).

By a lot of muddling I’ve managed to produce some scripts that basically work with Hazel. But in relation to Mail, I need a way of being confident that a script triggered by Hazel (where Hazel is working on a ~/Library/Mail “Messages” folder) will “pick up” the same message that Hazel was working on. Not such a problem if dealing with one message at a time; but I get a bit lost if say several messages are copied to a mailbox at the same time.

Here’s one of my scripts, which I use to delete a message from a user mailbox. My Hazel rule has first copied the message to ~/Documents; then it triggers the script:

on hazelProcessFile(infile)
	tell application "Mail"
		set theMessage to message 1 of mailbox "Hold"
		delete theMessage
	end tell
end hazelProcessFile

The “message 1” reference seems to work solidly if there’s only one message in “Hold”. But I’m nervous if I’ve just moved 4 messages together from INBOX to “Hold”. …

So my question: Can I be confident that the message the applescript will select as “message 1” will be the same one that Hazel has just copied to documents? Or is it possible that I’ve ended up deleting a message that hasn’t been copied yet (and now never will be)?

Hope this makes sense. Enlightenment humbly sought …

We have got to stop meeting like this! :smiley:

Even though I don’t have Hazel set up I wouldn’t be confident since there’s no parity check between what Hazel is doing and what Mail is deleting.
“Message 1” is just the first message. So if I have 50 messages I can loop 50 times, deleting message 1 (because after deleting message 1, message 2 becomes message 1, and so on.) until they’re all gone. Similarly, you wouldn’t want to use message 2 since it would error once we got down to a single message (and there’d be no second message to delete). You following so far? :wink:

You need to determine which message in Mail matches Hazel’s infile. I’m messing around with Hazel right now but the embedded Applescript seems buggy.

Jim Neumann

Hi Jim,

It was your suggestion that I post here re applescript stuff, so I suppose you may as well be the one to answer when I get here …

Yes I do actually follow so far, and will await any further insights you may gain …

One thing I can clarify is that I’m not likely to do any really mega damage with my script such as it is. The Hazel rules that trigger these scripts specify files with date added in the last hour.