Applescript and mail rules:
I'm trying to do something with rules that I can't easily do in preferences.
I want to create a rule that does does the following:
If from equals [email protected] OR [email protected]
AND
message contains "lawn" or "Lawn" or "LAWN" or "XYZ" or "xyz" or "ABC"
move message to mailbox M
Or, do I just have to create a ton of rules?
Also, assuming the answer to the above is true, I guess I could write a single apple script that did all my mail processing.
I've searched and searched for such a script. Can anyone point me to something?
I'm an applescript beginer. I just ordered a book, but not here until next week.
Thanks!
Dave
do it this way with an applescript (attach this to a rule action that checks all messages - will probably need some tweaking):
set senderList to {"[email protected]", "[email protected]"}
set keywordList to {"lawn", "Lawn", "LAWN", "XYZ", "xyz", "ABC"}
using terms from application "Mail"
on perform mail action with messages theMessages for rule theRule
tell application "Mail"
repeat with thisMessage in theMessages
if sender of thisMessage is in senderList then
repeat with thisKey in keywordList
if thisKey is in content of thisMessage then
move thisMessage to mailbox "M"
end if
end repeat
end if
end repeat
end tell
end perform mail action with messages
end using terms from
Similar Messages
-
Wrong password in Vacation notice and Mail rules in web interface
I have just set up a new Snow Leopard server to try it out. I especially wanted to test the calendarserver and mail and corresponding web interfaces.
I can access the My Page without any problem, but when I try to access the Settings->Vacation Notice and Mail Rules, I am presented a login dialog. When I enter my credentials, it just shakes its head like the password is wrong. I tried with different accounts, and when I used the system admin login I could access those pages, but not for the standard account. What's wrong?Yes, the problem is the accented caracters !
I had the same problem until I remove the accent in the user information (Workgroup Manager > Users Tab > "user" > Info Tab > Firstname + Lastname).
So it's an encoding error in WPSession python module, and this problem drive the script EmailRules.py around line 162 (wpUser = wpSession.userNamed(username)) to go on the hasLocalEmailErrback def, and so give an error and refuse the authentication, even if we have a local email account.
It's boring that USA developers do not write code that manage well international strings !
It's like this forum : because I have an "é" in my lastname, I was unable to create an account for this forum, until I create a new Apple ID without the "é"
Or like the iPhone Developer certificates at the beginning... -
Using AppleScript in Mail Rules
I can't figure out how to edit an AppleScript for Mail to perform the following:
1) After moving a filtered incoming message to a prescribed folder (can do this easily), open the message in a new window (or go to the folder and show message).
2) Before sending a message, specify a folder to store the outgoing message. This could be done via bcc in Eudora, and can be done separately in Entourage, but I don't know enough to figure out how to do it in Mail.
Thanks for any suggestions.gauthier,
1) After moving a filtered incoming message to a prescribed folder (can do this easily), open the message in a new window (or go to the folder and show message).
You might want to take a look at the Open Message and Open Mailbox scripts available from Mail Scripts - they should do exactly what you are looking for:
http://homepage.mac.com/aamann/
2) Before sending a message, specify a folder to store the outgoing message. [...]
This is not possible in Mail since you cannot hook into the sending action to perform actions before sending (and rules per se don't apply to outgoing messages). However, you might check out the Filter Sent Messages script from the above referenced Mail Scripts as somewhat of a work-around.
Andreas -
Running AppleScript from Mail rules
Hi.
I'm pretty new to this, so sure it's a basic question....
I've created An AppleScript that calls an Automator application. This runs fine if I open the script in the editor and click run.
I've now created a mail rule which should run this script if a mail is received with defined subject, but it doesn't....
Whatever I try, it's just not running the script.
Any help?Yes.
tell application "iTunes"
quit
end tell
delay 60
tell application "iTunes"
activate
end tell
Change 60 seconds in delay to the delay you need.
(still might not work as a Mail Rule, but give it a try) -
Applescript: creating mail rules
Hi Community,
I can't find the syntax for creating a mail rule in applescript anywhere on the web. If someone could just post all the different options in one snippet (Move Message, Play Sound, Set Color, Delete Message, and run applescript) that would be awesome. Thanks!I agree with twtwtw. Most of the rules I use simply organize the inbox into separate mailboxes. So with a list of 150 or so students each semester, it would be great to have this script work..it creates the rules, one for each student number, but then the rule won't move the incoming messages. Hmm...
So if anyone figues out why, it would be great to let us know!
tell application "Microsoft Excel"
set hpath to "Macintosh-HD:Users:bg:20f11:Hmwk:graded:"
set theGrades to open workbook workbook file name "Macintosh-HD:Users:bg:20f11:20f11_grades.xlsx" update links do not update links
set email_list to value of every cell of range "e_mail"
set theMailbox to "20f11"
activate
set sn to value of every cell of range "sn" --sn is student number
end tell
tell application "Mail"
repeat with j from 1 to length of sn
set newRule to make new rule at end of rules with properties {name:item j of sn), enabled:false}
tell newRule
make new rule condition at end of rule conditions with properties {rule type:from header, expression:item j of email_list), qualifier:does contain value, move message:mailbox theMailbox}
end tell
end repeat
end tell -
Hi,
I am trying to write an Apple-Mail rule and AppleScript combination to print the content of email messages that meet the criterion specified by the rule.
When an email arrives that meet the criterion, the rule runs and in-turn runs the AppleScript. However, the script does not seem to get the contents of the message.
Here is the script:
on run
-- For debugging to indicate that the script is running
display dialog "Running script"
tell application "Mail"
set theText to content of message
-- Display the text for debugging purposes.
display dialog theText
-- The remainder of the script passes the text to a
-- "do shell script" for printing.
end tell
end run
When the script runs, nothing happens. The text is not displayed and
there is no error message.
If I rewrite the script to run manually with a selected message, it works ok.
on run
tell application "Mail"
set theMessage to item 1 of selectedMessages
set theText to content of theMessage
display dialog theText
end tell
end run
Any ideas or help would be appreciated.
SyBThis works for me:
using terms from application "Mail"
on perform mail action with messages theMessages for rule theRule
tell application "Mail"
repeat with eachMessage in theMessages
set theText to content of eachMessage
display dialog theText
end repeat
end tell
end perform mail action with messages
end using terms from
did you forget the on perform mail action with messages bit? WIthout that, the script won't be able to get the message info from Mail.
I'l add (just pro forma) that printing from the command line is circuitous. Most likely you can skip extracting the content and sending it to unix and just say:
repeat with eachMessage in theMessages
print eachMessage
end repeat -
Export mail message as PDF Applescript for mail rules
I have looked everywhere for this and found nothing. I don't have 40 hours to learn applescript myself but from the little I do know this should be possible. Probably easy.
I want to created an applescript to run as a mail rule. I want the script to export the message as a PDF and move that PDF to a specified folder. Hazel can take it from there.
for instance:
Fedex sends me a email message that my shipment has been delivered.
mail rule kicks in and runs the script
script creates a PDF of the message
message is moved to "fedex delivered" folderI found a workaround, though there must be something neater.
I added this before my script
tell application "System Events"
tell process "Mail"
tell menu bar 1
tell menu bar item "View"
tell menu "View"
tell menu item "Sort By"
tell menu "Sort By"
click menu item "Subject"
end tell
end tell
end tell
end tell
end tell
end tell
end tell
then the same except "Date" instead of "Subject".
My script does what i want now. -
I have set up my computer (2.66 GHz iMac core 2 duo, OSX 10.9.4) with two users to separate my fishing club activities from the rest of my life. I would like to use different MAIL RULES to sort mail on each of the user accounts. I would also like to sync some of my activities such as Calendar, Contacts, Reminders, Notes between the accounts. If I link both accounts over iCloud, the MAIL RULES are synced period!! Unselecting mail for either or both accounts using the iCloud preference has no effect. To prevent syncing RULES requires signing one or the other account out of iCloud. This means, of course, that I am unable to sync Calendars, Contacts, etc. Does anyone know of a work-around for this dilemma?
Thanks in advance.
TomJFI'm using the same Apple ID for both accounts to allow syncing.
I use QUICKEN to track finances. The main reasons I set up two users is to separate fishing club finances from my personal finances and to separate documents. At this point I have decided to combine the two accounts; sort documents; and open separate QUICKEN accounts as needed. I do think that Apple Mail needs attention to correct inconsistencies such as the one I pointed out.
Thank you for your Comments
TomJF -
Cant send mail via applescript with mail rule
I created a script that would work as follows:
I send myself an email. The mail rule opens up an apple script that will take a picture using the isight camera and then email it back to me. It works fine in script editor. When i run it via mail.app it takes the picture but it wont email it back to me.
Here is my script:
do shell script "~/Documents/EmailScripts/isightcapture ~/Documents/EmailScripts/snap.jpg"
tell application "Mail"
set addrVar to "[email protected]"
set subjectvar to "Macbook Pic"
tell application "Finder"
set the target_file to alias "Macintosh HD:Users:ak406:Documents:EmailScripts:snap.jpg"
set bodyvar to "Picture from Macbook"
end tell
set composeMessage to (make new outgoing message)
tell composeMessage
make new recipient at beginning of to recipients with properties {address:addrVar}
set the subject to subjectvar
set the content to bodyvar
tell content
make new attachment with properties {file name:target_file} at after the last word of the last paragraph
end tell
end tell
send composeMessage
end tell
Thanks for the help.Hi ak406
Just tested your script here, less the do shell script part and it worked perfectly from my mail app.
Budgie -
I found this on Mactech. All I did was add the line "make new outgoing........", well, it does NOT work. What do you have to do to make Mail trigger an applescript. All I want to do is when I an email reminder, to just make a new email message with Happy Birthday in the Subject. I'll click the stationary and add a birthday background. Any help would be appreciated.
using terms from application "Mail"
on perform mail action with messages theSelectedMessages for rule theRule
repeat with a from 1 to count theSelectedMessages
make new outgoing message with properties {visible:true, subject:"Happy Birthday", content:"Happy Birthday "}
end repeat
end perform mail action with messages
end using terms fromsorry but your suggestion did NOT work. Here is what I wrote and it did NOT make a difference the script will NOT generate an outgoing message.
using terms from application "Mail"
on perform mail action with messages newMessages
repeat with newMessage in newMessages
tell application "Mail"
set senderName to (extract name from sender of newMessage)
say "You've just received an email from " & senderName
say (subject of newMessage) as string
if (subject of newMessage) contains "birthday" then
tell application "Mail"
activate
make new outgoing message with properties {visible:true, subject:"Happy Birthday", content:"Happy Birthday "}
end tell
end if
end tell
end repeat
What I was able to do was embed a line of code to call another script that does successfully create an out going message. Here is the successful script.
using terms from application "Mail"
on perform mail action with messages newMessages
repeat with newMessage in newMessages
tell application "Mail"
set senderName to (extract name from sender of newMessage)
say "You've just received an email from " & senderName
say (subject of newMessage) as string
end tell
end repeat
tell application "Finder"
open "Macintosh HD:Users:Shawn:Documents:Applescripts:Mail scripts:make outgoing email happy birthday5.app"
end tell
end perform mail action with messages
end using terms from
here is the make birthday5 app
tell application "Mail"
activate
make new outgoing message with properties {visible:true, subject:"Happy Birthday", content:"Happy Birthday "}
end tell
Now when I get my birthday reminder, according to the "Rule" set up in preferences, if the subject contains "birthday" the script will read the name and subject and generate an outgoing message with Happy Birthday in the Subject and Body.....all I have to do is add some birthday stationary, type in a sappy line and viola, done. See why I love Macs so much. -
Applescript and Mail 2.x?
I'm currently using Mail 2.x in Tiger, and not having much luck figuring out how to create an Applescript or assign keyboard shortcuts that will let me select a specific signature without taking my fingers off the keyboard. I can use the mouse to select a signature, but that means I've got to take my hands off the keyboard--slow given the number of messages I send every day. Alternatively, I can enable "full keyboard access" to "all controls" in the Keyboard preferences pane, but then I burn out the Tab key having to move to and past the Signatures dropdown menu for each message, plus I still have to select the correct signature using the arrows. A single keystroke would be so much nicer!
Is there any way to assign a keyboard shortcut to an individual signature? I couldn't see anyway to do this via the Keyboard preferences panel, since the signatures aren't a menu choice.
Applescript seems to be the next most likely suspect, but I couldn't see any Applescript commands that would do this--again, they seem to relate primarily to menu commands and buttons rather than mouse clicks. Nothing obvious about selecting signatures.
I'd prefer not to invest in QuicKeys or similar software if I can learn to program it myself using the tools that Apple provides, so advice on scripting this would be my preference.
Thanks for any suggestions!
--GeoffGL proposed the following Applescript to add a signature:
tell application "Mail"
activate
set om to first outgoing message
set message signature of om to signature "Signature #2"
end tell
Thanks for the suggestion. Good start, but we're not there yet. As a complete innocent with Applescript, I'm not sure how to tweak this to make it work. Here's some data to help you help me:
With a message open and ready to send before I try running the script, running the script produces the error message "The variable om is not defined." Is there a "currently open message" equivalent to "first outgoing message" that could be substituted in the script?
When I tried running that same script from the Script editor with a message displayed but not ready to send, there's possibly no outgoing message, so I got a different error. Should've had more coffee before trying this, but I'm passing this along in case it's useful troubleshooting info beyond "PLBKAC"*.
* Problem lies between keyboard and chair. <g>
Also, does "Signature 2" refer to a logical pointer (i.e., the second signature in the dropdown menu), or should the quoted text be replaced with the actual signature I want to insert? If the signature contains multiple lines, how would I format that in the script editor?
Last but not least, how do I bind the resulting script to a keyboard shortcut so I can run it in Mail without using my mouse? I didn't spot anything in the online help for AppleScript. Possibly from the Keyboard preferences panel?
Thanks for any additional thoughts.
--Geoff -
Regular expressions or "wild cards" and Mail rules
I'm working on building several rules to deal with various emails types i get during the average day at work. some of them are automatically generated by our servers, others are from customers etc. Some of the messages generated by our servers have a blank subject line. Mail won't let me build a rule with a blank subject line (or anything blank for that matter)...is there a way around this?
Also, does mail allow for use of regular expressions? I really don't know anything about regular expressions, but was hoping to learn enough to accomplish some more complex rules if it's possible. If not, i won't bother trying to tackle such an obscure language
Thanks in advance!No, there is no provision for this in Mail's rules.
However, Mail's Junk Filter is based on a much more sophisticated method (Latent Semantic Analysis) that typically is more effective at detecting this sort of thing, in part because methods based on Regular Expressions usually have an unacceptably high false positive rate if they are made general enough to catch all the possible misspellings. -
I am experiencing some bizarre troubles with Mail. Specifically, I have a complex set of rules in Mail; some of them, automatically move an email from the inbox folder to another folder I created if a specific name or subject is met. Now, when an email that match a rule arrives it is automatically moved according to the rule but it is not notified. An email is notified only if it is not subjected to a rule. Is there a way to get notified whenever an email arrives?
madHelloKitty wrote:
just cutting/pasting the symbol into the rule doesn't work. Ideas?
The symbol may be representing something else in the underlying text. You could try View > Message > Raw Source to see what that is. -
Mail Rules don't work in Mavericks
It seems with the update to 10.9 Mavericks Mail rules no longer work. My email address was harvested by spamers and I have been using rules to weed throught the 100's of emails that I receive each day.
Russian Brides, Vigra, Learn a new language get moved into the trash when delievered. Now nothing happens.
I just tried to write a new rule and when I clicked the Prefs pannel I got the spinning cursor. After waiting for some time I did a force quit only to find that Mail was not responding.
Anyone have any ideas? I did not see any major changes with Mail and Mavericks so I'm not sure why it's been effected.
MDYou might condider using an Applescript with Mail Rules.
Here's one that I use (Use whitelist to not move to spam):
(Just edit to your junk or whitelist)
Note: The Applescripit must be saved to: ~/Library/Application Scripts/com.apple.mail/
The rule:
The AppleScript:
using terms from application "Mail"
on perform mail action with messages theMessages
tell application "Mail"
repeat with theMessage in theMessages
set theSender to (sender of theMessage)
set theReplyto to (reply to of theMessage)
set theSubject to (subject of theMessage)
set theHeader to (all headers of theMessage)
set theContent to (source of theMessage as string)
set theAccount to (account of mailbox of theMessage)
if my blacklist(theSender, theReplyto, theSubject, theHeader, theContent) ¬
and not my whitelist(theSender, theHeader) then
my moveToJunkFolder(theMessage, theAccount)
end if
end repeat
end tell
end perform mail action with messages
end using terms from
on blacklist(f, r, s, h, c)
if h contains "X-YahooFilteredBulk" or ¬
h contains "Received-SPF: fail" or ¬
h contains "Received-SPF: softfail" or ¬
h contains "Received-SPF: error" or ¬
h contains "Received-SPF: permerror" or ¬
h contains "Received-SPF: temperror" or ¬
h contains "streamsendbouncer@" or ¬
s contains "[Bulk]" or ¬
f contains "Vigra.candianmeds.com" or ¬
f contains "[email protected]" or ¬
f contains ".cz" or ¬
r contains ".ma" then
return true
end if
end blacklist
on whitelist(f, h)
if f contains "aicpa" or ¬
f contains "eWeek.com" or ¬
h contains "from macbook-air.local" then
return true
else
return false
end if
end whitelist
on moveToJunkFolder(m, a)
tell application "Mail"
try
set the read status of m to true
set the junk mail status of m to true
set theAccount to (account of mailbox of m)
set mailbox of m to (mailbox "Junk" of a) -- Move to "Junk"
end try
end tell
end moveToJunkFolder -
Lost mail rules when upgrading to ML, can't restore from time machine
Hi,
I've installed mountain lion and restored from time machine and mail rules were not there. There's only news from apple.
I tried to restore the rules myself (MessageRules.plist and MessageRules.plist.backup) but when opening mail rules were not there yet. Nevertheless, i can add rules manually, so if there was a way of looking at the past rules I could just copy them (though it's not the best way).My rules are still there, so perhaps Apple support speak with resource forked tongue.
Comparing an old (pre ML) rules file from 20th June with the ML one, the differences are:
The file name was: MessageRules.plist, the ML one is SynchedRules.plist. Both are in my V2 folder, but the old Rules have been copied (by Mail, not me) into the new SynchedRules file.
In the old file, for each item (Rule) the first line was Active (Key) String (Type) 1 (Value). In the ML file, this line is no longer there, so the first line is AllCriteriaMustBeSatisfied.
You can see these differnces in the "preview" part of Finder Show Info, if you have the two files to compare.
Thus, just copying and renaming the old Rules file probably won't work, because of this difference. Mail probably just makes a new file because the copied file isn't as expected. It should be possible to edit the old plist file to remove the "Active" line, and rename it to SynchedRules.plist (but see below). The top is slightly different, just below <dict>, as this relates to the Active line. There are a couple of lines at the bottom that aren't in the new file.
The "Active" line that has gone is now in a file called RulesActiveState.plist. This file is just a list of RuleIds from the SynchedRules.plist (like this:
AC6F6611-FF61-4AA6-9682-BA100EFAC233)
Doing this does sound rather tedious, but might be quicker than manually re-entering blocked email addresses etc. to replicate old rules. As plist files are in xml format, it should be possible to use a text editor to delete the unwanted "Active" line from the message rules list by Find & Replace All (with nothing). All this could then be copied to the "SynchedRules.plist. You would need to copy the RuleIds into the RulesActiveState.plist as well.
Make a copy of the files first just in case...
Maybe you are looking for
-
Help! I somehow deleted my safari search line, how do i get it back?
I deleted my searchline in safari, the one you type www.something.com in. I dont know how but i cant get it back. Hehe. Please help?
-
Hi, I am getting error "\\Invalid Rules File, which was created using a newer version" in the error file while trying to load dimensions into my outline .
-
Mac Mini server OS 10.9.5 won't connect to Bose Bluetooth Audio Adapter
although my iPhone 5C running iOS 8.1 sees the Bose device just fine. Any thoughts? The suggestions from the Bose site are not helpful, as they assume their device will of course be seen by the Mac Mini Server. Thanks.
-
Ibook won't boot, after having DVD stuck
I had a problem ejecting a DVD, as it had crashed. So after restarting, the grey screen appears with the apple, and the laptop did not come back on. I managed to eject the CD by holding the trackpad button, but OS X is still not loading. Any idea's w
-
HT201328 Unlocking iphone 3gs from Apple store
Vodafone won't unlock my iphone 3gs because the IMEI number doesn't match their records, because my original iphone broke and was replaced by Apple store. They said try Apple. Is this right? Any help gratefully received.