Processing mail with a shell script from a message filter

I’ve never written an AppleScript in my life, but have lots of experience with standard Unix text processing tools.

I’d like to set up an Apple Mail message filtering rule that pipes the entire message (including all headers, if possible) into the standard input of a shell script. Assuming for the time being that my shell script is just “cat >~/scriptout.txt”, what’s the smallest amount of AppleScript glue I need to make this work?

Alternatively, if there’s no easy way for AppleScript to talk to the shell’s standard input, is there a

do shell script "cat " & quoted form of (something) & " >~/scriptout.txt"

or

do shell script "echo " & quoted form of (something) & " | cat >~/scriptout.txt"

variant that will get the job done?

Thanks for your time.

If you already have a shell script, you can call it using the do shell script command along with the Mail message information. This script should work for you:

using terms from application "Mail"
	on perform mail action with messages The_Messages
		tell application "Mail" to repeat with This_Message in The_Messages
			set all_of_it to (This_Message's all headers) & (This_Message's content)
			do shell script "PATH TO YOUR SHELL SCRIPT" & space & all_of_it
		end repeat
	end perform mail action with messages
end using terms from

The only weakness I found in testing this was that if the message has attachments, not only will they not be processed, this does not seem to even indicate that they exist.

Good luck, hope this helps.

Thanks very much, Craig.

From my limited reading on “do shell script”, it seems to me like your suggested script is going to try to pass the entire literal text of the mail message to the specified shell command as a command line parameter, rather than making it standard input. It seems likely to me that I’d actually need something like

using terms from application "Mail"
    on perform mail action with messages the_messages
        tell application "Mail" to repeat with this_message in the_messages
            set whole_message to (all headers of this_message) & (content of this_message)
            do shell script "echo " & quoted form of whole_message & " | /path/to/shell/script"
        end repeat
    end perform mail action with messages
end using terms from

Is passing the literal text to “echo” as a command line parameter really the best way to feed it in there? Does AppleScript truly have no inbuilt way to send stuff down a standard Unix pipe?

A point of curiosity: under what circumstances would this script ever end up processing more than one message in the_messages? Is the repeat block just a convenient way to drill down into the messages container, or will it sometimes actually repeat?

And can it really be true that there is no property of a message object that is simply the unfiltered text exactly as received (basically everything that ends up in an .emlx file except the Apple-generated XML cruft at the bottom)?

Sorry to keep pestering you with this instead of just going and trying things out, but I’m actually trying to work this stuff out for a friend with a Mac; I don’t personally have access to one.

From my own limited experience, I believe the only other way is to create a .txt file somewhere, but you are still left with passing that file as an argument, not just piping it in. My question is simply whether it really matters how the shell script recieves the data? When sending stuff via do shell script, there will always be a time lag, so it really should not matter whether it is piped in, or passed as an argument; once the shell gets the data, it should be able to do whatever it is designed to do.

I still ponder that one myself; but this appears to be the only reliable way to get Mail to actually process every message that your Rules identify. It may very well be that this handler always receives a list of one item, but who really knows? Perhaps someday it will send 2 or more messages at once. There are discussions out there on this site about this topic, but I don’t feel like searching for them right now to point them out. (I have to get to work soon.)

I have no clue about that personally. Mail is scriptable, but not COMPLETELY so. There are still a number of commands in the AppleScript dictionary for Mail that simply do not function. We obsessed scripters continue to wait, but improvements in the scriptability of Mail are surprisingly slow to occur. I know that Microsoft’s Entourage has fairly robust AppleScript access, but I never use it and have never scripted it myself. There are a lot of people here that have, however, and it is certainly possible to work that direction.

The pestering is never a problem; the lack of a personal Mac is a tragedy. We will all mourn for you.

Don’t mourn too much - I really enjoy using my present Ubuntu Edgy system, and would probably run the same thing even if my hardware was Mac instead of Dell.

Thanks again for the help :slight_smile: