Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Attach AppleScripts to Mail.app rules Apps
In case you missed it, the version of mail included with 10.2.3 can execute Applescripts as part of a rule. This will open up a lot of possibilities. A member of the Mail.app development team passed along this script that uses the 'perform_mail_action' handler to access messages that match the given rule or rule object. In other words, it kicks up a dialog box that automagically tells you a rule was matched and what the message was that matched.
on perform_mail_action(info)
tell application "Mail"
set selectedMessages to |SelectedMessages| of info
set theRule to |Rule| of info
repeat with eachMessage in selectedMessages
set theSubject to subject of eachMessage
set theRuleName to name of theRule
set theText to "The rule named '" & theRuleName & "'
matched this message:" & return & return &
"Subject: " & theSubject
display dialog theText
end repeat
end tell
end perform_mail_action
In the example, 'info' is an Applescript record with two keys: |SelectedMessages| and |Rule|. |SelectedMessages| is a list of message objects that match the conditions for the rule. As messages are fetched and evaluated in batches, this list may contain more than one message, so be sure to take that into account. |Rule| is the rule object that triggered the script action.

You don't have to make any modifications to the script itself to make it work. Simply save the above text as a compiled script editor and include it with a mail rule to see how it works.

[Editor's note: I have not tested this one other than ensuring that the script syntax is acceptable to the Script Editor.]
    •    
  • Currently 3.00 / 5
  You rated: 3 / 5 (3 votes cast)
 
[35,917 views]  

Attach AppleScripts to Mail.app rules | 31 comments | Create New Account
Click here to return to the 'Attach AppleScripts to Mail.app rules' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Cool
Authored by: Snaro on Jan 16, '03 11:37:03AM

I've really been looking for that. First of all, I was wondering how long it would take Apple to implement AppleScript into Mail rules. Now that they'd done that, I could not find information how to access the messages that applied to a specific rule. I will implement that into my future scripts.



[ Reply to This | # ]
iCal Scripts
Authored by: alex_kac on Jan 16, '03 01:17:02PM

Now we just need an iCal script to work with Mail so that any time we get a vCalendar request, it goes directly to iCal right away.

Anybody up to the challenge?



[ Reply to This | # ]
iCal Scripts
Authored by: navratil on Jan 17, '03 03:00:47PM

I hacked together a combination AppleScript/perl script a while ago that basically used AppleScript to save the body of an iCal/vCal invitation in the currently-open mail message to a flatfile, and then perl to insert the invite into a particular calendar in iCal.app.

It's crap, but it might be a useful starting point for someone else to clean up and use these hooks.

If anyone wants a copy of the scripts, drop me a line at joe at navratils dot org.



[ Reply to This | # ]
But it might be unreliable...........
Authored by: alajuela on Jan 16, '03 02:12:29PM

I have been trying to get Mail to run a script-containing rule which takes a message and forwards it over to iCal [this allows me to keep my iCal roughly in sync w/ my PC Outlook client].

When I run the script manually on one or more messages, it works, but to say that it is undependable inside a Mail rule is a generous understatement. I have not yet figured out if this is a script or Mail flakiness problem.

I do think this functionality is a very good; I just gotta figure out what makes it work best.



[ Reply to This | # ]
But it might be unreliable...........
Authored by: JackRepenning on Sep 20, '04 08:32:32PM

Yes, I find Mail's invocation of scripts to be unreliable as well. My script runs fine if I cmd-a opt-cmd-l (select all, run rules), but as mail arrives it's very very spotty, misses I think more messages than it actually hits. I'm pretty sure Mail is not running my script (as opposed to my script dropping bits somewhere), based on adding debugging stuff that logs itself in various ways (I've used a dialog, and also I've used application "Mail"'s create new outgoing message, so it's not all in my face).

I also find that my properties aren't being saved, as properties ought to be. I wanted to use properties to save stuff from run to run, but no joy on that one.

---
Where is the knowledge we have lost in information?
-- T. S. Eliot, "Choruses from 'The Rock'"



[ Reply to This | # ]
eMail Attachments.
Authored by: prk on Jan 16, '03 06:07:20PM

I've been trying to learn AppleScript. I have not been able to do this yet. I want to write a script that will run as a rule in Mail.app. What I really want is for the attachment to be loaded into preview, then exported as a jpg or gif file. I can then ssh the file to my web site. I get PDF ad files that I convert to gif files and put them on a dealer web site. I have a script for Linux to do this, but ImageMagick does not do too well with some of the PDF's I get (like I get a lot of white space around stuff). Anyone know where I can get help to build a script like this? Or maybe someone could write a simple script that does it and I can take that script and add to it and learn how it works. Thanks!



[ Reply to This | # ]
eMail Attachments.
Authored by: russh on Jan 16, '03 06:42:14PM

Unfortunately, Preview is not currently scriptable... hard to believe, but true. Send feedback to Apple and ask them to add this functionality... its seems like a natural doesn't it?

Photoshop can do what you want, combined Applescript with Photoshop's built-in scripting. (Expensive, unless you already have Photoshop.)

Graphic Converter could do it with other types of images, but doesn't do PDF.

Maybe someone else knows of an inexpensive and alternative app that can handle this.

Regarding Applescript, there is a great <GREAT> beginning Applescript article in the December 2002 issue of MacWorld magazine. (Cover: "What's .Mac Worth?") Perhaps you can dig one up in a library or from a friend. It's written by Sal Soghoian, the product manager for Applescript and terrific guru/evangelist. I took a one day class from him last year and the article is basically worth a half day of the class.

Good luck. I encourage you to explore Applescript. It sounds like it could help in a production environment like yours. Visit http://www.apple.com/applescript and check out some of the power.



[ Reply to This | # ]
eMail Attachments.
Authored by: LostOnline on Jan 21, '05 04:46:57PM

I am having a problem that I'm sure someone out there would consider common sense. Unfortunately my sense is just not that common. I am new to the applescript world so I don't know too much. I have programed in C, C+, basic, and G7, so I understand technobabble just fine.
What I'm trying to do is get Mail.app on my MAC OSX v. 10.3.7 to save the attachments automatically. We get the same 2 E-Mails every day, from the same person, with a file attachment with the same filename.
What I'm trying to do is write a script that will save the attachment (which I can't figure out) rename it (easy enough) with adding the date to the end of the file (still working on, with no success), and changing the file type (because for some reason it makes it a textedit document instead of a text document, which it was when we used AOL)

So what I need to know is what tags to use to tell it to save the attachments.

And then I need to know how to add todays date to the filename when I save it

here's what I got
[code] tell application "Mail"
activate
open selected messages
save attachment as "TEXT"
end tell
tell application "Finder"
activate
set file type of document file "3456A.csv" of folder "Desktop" of folder "me" of folder "Users" of startup disk to "TEXT"
set name of document file "3456A.csv" of folder "Desktop" of folder "me" of folder "Users" of startup disk to "3456A.csv" {HERE I NEED TO INSERT THE DATE PREFERABLE IN M/D/YY FORMAT SO THAT IT LOOKS LIKE "3456A.csv1/21/05" or "3456A.csv 1/21/05"}
end tell [/code]
[code] tell application "Finder"
activate
set file type of document file "3456A_up.csv" of folder "Desktop" of folder "me" of folder "Users" of startup disk to "TEXT"
set file type of document file "3456A.csv" of folder "Desktop" of folder "me" of folder "Users" of startup disk to "TEXT"
set name of document file "3456A_up.csv" of folder "Desktop" of folder "me" of folder "Users" of startup disk to "3456A_up.csv"
set name of document file "3456A.csv" of folder "Desktop" of folder "me" of folder "Users" of startup disk to "3456A.csv"
end tell [/code]
This second one is the one I'm currently running just to changethe file type to TEXT so that I can import it into Filemaker Pro

Any help would be appriciated

Thanks,
LostOnline



[ Reply to This | # ]
announce messaes with speech
Authored by: cyberRodent on Jan 16, '03 06:40:18PM
I've been thinking about setting something to read the FROM of incoming messages and pass it along the this cl speech utility called "say" that give CL control over the built in speech synthesis. I think that with this I can have my Mac finally tell me when I get messages from ceratain folk -- this would be much preferred to setting up procmail and fetchmail to handle all my mail and piping what I want to this CL app . last I heard say is available from : http://www.umlautllama.com -- if it works out I'll post a solution. --- cyberRodent

[ Reply to This | # ]
announce messaes with speech
Authored by: russh on Jan 16, '03 07:02:31PM
You can do that with a modified version of the Applescript I posted: Create a mail rule that screens for a specific sender. "joeblow@nowhere.com" Then as the last action in the rule, attach a compiled Applescript like this:
on perform_mail_action(info)
	tell application "Mail"
		set selectedMessages to |SelectedMessages| of info
		set theRule to |Rule| of info
		repeat with eachMessage in selectedMessages
			set theSender to sender of eachMessage
			set theRuleName to name of theRule
			set theText to "You have a new message from" & theSender
			say theText
		end repeat
	end tell
end perform_mail_action
I just tested the above. It works. When a message comes in that triggers the rule (in this example, from "Joe Blow"), your Mac will say: "You have a new message from Joe Blow " The script will add the name of any sender you set up a rule for. There is likely a way to make it not say the email address. That exercise is left to the reader. :)

[ Reply to This | # ]
announce messaes with speech
Authored by: russh on Jan 16, '03 07:10:52PM
Hate to reply to my own message, but not sure how to edit. I just wanted to be clear: this script does NOT use "Say" or any other CLI application. The "say" in the script part of standard Applescript. No piping or terminal commands required. :) (Not that there is anything wrong with that...)

I just wanted to be clear.. it really is that simple.

[ Reply to This | # ]
How 'bout this one?
Authored by: applepal on Jan 16, '03 08:12:11PM

it's the first i ever wrote (with the first ost as example, thx)... this one works for me...

on perform_mail_action(info)
set volume 4
tell application "Mail"
set selectedMessages to |SelectedMessages| of info
set theRule to |Rule| of info
set amountofselectedMessages to count selectedMessages
if amountofselectedMessages > 1 then
set begintxt to "you have " & amountofselectedMessages & " new messages. [[emph -; rate 100]]from, "
else
set begintxt to "you have a message from "
end if
set counter to 0
repeat with eachMessage in selectedMessages
set counter to counter + 1
set theSender to sender of eachMessage
if counter > 1 then
set begintxt to "[[emph +; rate 200]]and from, "
end if
set theText to begintxt & theSender & " "
set adresbegin to offset of " 0 then
set theText to get text 1 thru adresbegin of theText
end if
say theText
end repeat
end tell
set volume 1
end perform_mail_action



[ Reply to This | # ]
How 'bout this one?
Authored by: applepal on Jan 16, '03 08:15:56PM

argh...

set adresbegin to offset of "<" in theText



[ Reply to This | # ]
How 'bout this one?
Authored by: russh on Jan 16, '03 09:15:40PM

Niiice. Fun script... though I think it would drive me nuts if I left it on for ALL messages. ;)

In addition to the typo you mentioned, you also need to delete the last "end if".. it's extra.



[ Reply to This | # ]
Unadventurous
Authored by: Eponymous on Jan 17, '03 12:53:26AM

Couldn't these be a bit more powerful?

You could leave mail running and send messages to yourself that activate certain features on your mac. Let's say you're away and don't want to leave on filesharing. Write a script that responds to a message from your account with a particular subject from your account that turns it on. Or restarts your machine, or whatever.



[ Reply to This | # ]
Unadventurous
Authored by: mahakali on Jan 21, '03 05:01:07PM

There's a way to set rule to run applescript (I'm using Jaguar 10.2.3). Haven't tried it myself though. It'll be cool.



[ Reply to This | # ]
Wow
Authored by: hombre on Jan 09, '05 04:31:00PM

Brilliant idea. You could even send your mac commands from your cell phone. Has anyone actually implemented this in a useful way?



[ Reply to This | # ]
Unadventurous
Authored by: barnabynorwood on Feb 04, '06 05:56:33AM

Seriously, I'm on a quest for this sort of thing.

I'm trying to work out how to control my iMac G5 remotely. Like from my Pocket PC or from a browser. Imagine the coolness of Phlink as well as some Automator goodness thrown in too.

The applescript rule feature of Mail.app is really exciting. But I just can't do anything clever enough to use it.

Could I get an applescript to launch an Automator workflow based on a particular rule in Mail.app?

This stuff excites me. I hope someone clever works something out..



[ Reply to This | # ]
applescript to color label messages
Authored by: deryl on Jan 17, '03 09:10:20AM

I have been searching far and wide for some means to use color
labels on my messages without having to use a rule. The rule
action for coloring messages is exactly what I want, but I want
to be able to do it manually. Does anyone know if this is possible
via AppleScript or some other means?

thanks.
-d.



[ Reply to This | # ]
applescript to color label messages
Authored by: russh on Jan 17, '03 09:29:51AM

I don't think there is a way to do this currently. The rumor is that the next major update of Mail.app (might be in 10.3, dunno) will include even more Applescript functionality.. plus a "Scripts" menu a la iTunes, BBEdit, Entourage.

Maybe then.



[ Reply to This | # ]
color labeling of messages
Authored by: garbanzito on Jan 22, '03 09:41:27PM

you can do this right in the UI by opening up the color panel.. select a color and any selected messages are "labeled".. improvement (such as sort by color) is needed to match Eudora's capabilities, but this is a start.. it doesn't appear you can label via AppleScript, though



[ Reply to This | # ]
Works perfect for me
Authored by: BLASTfemur on Jan 17, '03 10:11:02AM

Great hint, I'll deal with side effects (if any) later. For now, everything I need works in Safari.
Russh, you're a badass.



[ Reply to This | # ]
Super Script for speaking
Authored by: Helios on Jan 17, '03 03:08:18PM

Okay I've been busy and I have made a script that extracts the Name of the person sending you an e-mail and then speaks it to you... also it fades itunes volume down and back up so you can hear it. It is capable of speaking with built in finder speech but if you edit a few tags it is also setup for speaking with the unix script './say' and with a program called 'Voice Box'

If you don't run itunes then this script will probably open itunes every time you get mail...
if you know basic scripting its easy to fix.

Its also setup to replace the spoken name of the sender...
e.g. the sender is mac@guru.com and you want it to just say "Guru"

Id insist at least basic understanding of applescript if you are to use this as I'm not very good at making my scripts work on other peoples computers.

I hope it works for you.

goto http://helios66.mine.nu/scripts/mail.scpt.sit

you can grab the unix script './say' at 'http://www.cis.rit.edu/~jerry/Software/MacOSX/#say'

send any feedback to helios11@maniac66.idps.co.uk



[ Reply to This | # ]
Doesn't work for me
Authored by: Zeratul on Jan 19, '03 12:00:19AM

I've been trying to use these scripts to set up an automated mailing list system, that anyone sending to a certain address with "add to list" in the subject would be added to the Address Book group "List". Well, for some reason the script doesn't seem to be excecuting for me. I spent a few hours over the past few days, with an automated rule that runs the script every time a message is recieved (kept e-mailing myself), to test out the script. If I have it not do anything but display a Dialog, it does that fine. And if I have it follow your instructions as to how to extract the rule name, I can do that and display that in a second dialog. I can even add the line "set selectedMessages to |Selectedmessages| of info", and I still get both dialogs. However, once I add the line "set theSender to sender of Selectedmessages" or anything like that, it runs the script till that point and promptly ends operation of the script. I test this with a dialog saying "before", before that line of code, and a second one "after" after the line of code. If I simply do not have the line, I get 2 dialogs in a row, "before", "after". Once I enter the line, I get "before" and thats it. The strange thing is that even if I delete the strange line after entering it and failing, I have to restart Mail.app for the script to start working again.

I also tried directly copying and pasting in the sample code provided, with no seeming result. Another note, I am pretty sure that Script Editor registers properties of classes in blue, while variables in green. In the script, it registers the "name" of theRule in blue, but it registers "sender" or "subject" of eachMessage in green, not blue.

Any and all help will be greatly appreciated.



[ Reply to This | # ]
Doesn't work for me
Authored by: Helios on Jan 19, '03 03:29:01PM

I cant explain why your script isnt working i can only moan at the fact that if a script error occurs that mail will not notify you of the error... maybe try wrapping your script in a 'try' and set an error handler to display a dialog with the error message.

--example script
try
--your script here
on error
display dialog "Oops"
end try

You can set the dialog to display the error type and number aswell to debug what may have gone wrong. But you probably allready knew all this :/



[ Reply to This | # ]
Mail-Mark as unread
Authored by: MtnBiker on Jan 21, '03 01:33:09PM

My only problem with using Mail is that it marks things as read when I click on them. Most would say so what, that is what expected, but Entourage allows the option to leave as unread. I use this to scan may mail and leave messages that need action as Unread. The idea is that by default everything needs action and I don't have to do anything to leave it as unread.

Can I use an AppleScript to change Mail's behavior to leave things marked unread until I change it to Read?

TIA



[ Reply to This | # ]
I want to print via an AppleScript
Authored by: wplate on Feb 11, '03 09:48:52AM

So Mail.app can't automatically Print a message from within a rule, but it can run AppleScripts.

Can someone offer me a script that will print a message, so I can trigger the script from a rule and automatically print messages?

Thank you.



[ Reply to This | # ]
Attach AppleScripts to Mail.app rules
Authored by: patsdave on Mar 07, '03 11:18:28AM

I am trying to figure out how to use Applescript in Mail.app to transfer a message based on someone in my address book to two mailboxes AND open one of them upon arrival.

Please help!!!!



[ Reply to This | # ]
Attach AppleScripts to Mail.app rules
Authored by: morgion on Jun 19, '04 11:33:36AM
I might be wrong, but I think an AppleScript in a Mail rule actually run BEFORE other actions in the same Rule. This is VERY annoying, and I'd like to get around the limitation, if possible.

I've customized this script to say the mailbox name of the message; this is shorter and usually has more immediate relevance for me than the sender's name. Very easy to do using the following:

set thisMailbox to name of mailbox of thisMessage as string
say "[[volm 0.15]]" & thisMailbox using "Bruce"
The script is in a few different Rules I have set up for mailing lists, etc. These Rules have common Conditions and Actions:
1. Identify a message by sender name and/or subject line.
2. Move the message to a specific mailbox.
3. Run the customized script.

When a matching message arrives, the script runs, and always says "Inbox", THEN the new mail sound plays. When I check for the message, it's never in the Inbox; it's always been moved to the appropriate mailbox.

How can I get this script to run AFTER the message has been moved, and preferably after the new mail sound?



[ Reply to This | # ]

AppleScripts execute before other actions
Authored by: hombre on Jan 09, '05 03:51:03PM

I cannot find any way around AppleScripts executing before other actions, even if those actions come before the script in the rule.

This strikes me as an embarrassing state of affairs worthy of filing a bug with Apple.

If someone has found a workaround, please post it.



[ Reply to This | # ]
Scripting in Mail to Save Attachments
Authored by: LostOnline on Jan 21, '05 04:59:23PM
I am having a problem that I'm sure someone out there would consider common sense. Unfortunately my sense is just not that common. I am new to the applescript world so I don't know too much. I have programed in C, C+, basic, and G7, so I understand technobabble just fine. What I'm trying to do is get Mail.app on my MAC OSX v. 10.3.7 to save the attachments automatically. We get the same 2 E-Mails every day, from the same person, with a file attachment with the same filename. What I'm trying to do is write a script that will save the attachment (which I can't figure out) rename it (easy enough) with adding the date to the end of the file (still working on, with no success), and changing the file type (because for some reason it makes it a textedit document instead of a text document, which it was when we used AOL) So what I need to know is what tags to use to tell it to save the attachments. And then I need to know how to add todays date to the filename when I save it here's what I got
 tell application "Mail"
activate
open selected messages
save attachment as "TEXT"
end tell
tell application "Finder"
activate
set file type of document file "3456A.csv" of folder "Desktop" of folder "me" of folder "Users" of startup disk to "TEXT"
set name of document file "3456A.csv" of folder "Desktop" of folder "me" of folder "Users" of startup disk to "3456A.csv" {HERE I NEED TO INSERT THE DATE PREFERABLE IN M/D/YY FORMAT SO THAT IT LOOKS LIKE "3456A.csv1/21/05" or "3456A.csv 1/21/05"}
end tell 
 tell application "Finder"
activate
set file type of document file "3456A_up.csv" of folder "Desktop" of folder "me" of folder "Users" of startup disk to "TEXT"
set file type of document file "3456A.csv" of folder "Desktop" of folder "me" of folder "Users" of startup disk to "TEXT"
set name of document file "3456A_up.csv" of folder "Desktop" of folder "me" of folder "Users" of startup disk to "3456A_up.csv"
set name of document file "3456A.csv" of folder "Desktop" of folder "me" of folder "Users" of startup disk to "3456A.csv"
end tell 
This second one is the one I'm currently running just to changethe file type to TEXT so that I can import it into Filemaker Pro Any help would be appriciated Thanks, LostOnline

[ Reply to This | # ]