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

Similar Messages

  • 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

  • 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)

  • 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" folder

    I 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.

  • 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

  • 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

  • How to creat Mail Rule with a script

    i can writ a script and runing is ok, but can't applied to mail rule !?

    Strangely, Mountain Lion requires that all Mail scripts reside in this location:
    ~/Library/Application Scripts/com.apple.mail/
    Put it there.

  • Applescript for mail rule

    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 from

    sorry 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.

  • Mail rules accidentally deleted all of my emails. How to restore?

    Alright, here goes.
    I tried to set up af number of rules in my Mail application (just the standard one), that would delete some "subscribtion emails" that I never read anyways. And no, it's not spam.
    BUT, I accidentally forgot to set the "any" criteria to "all", which means it all of a sudden deleted all mails with my email address as recipent. How can I restore them? They do NOT appear in my "Deleted messages"!
    Also, is it really true that such a simple user mistake, can cause the user to lose highly important emails? I mean... It's like one click away from utter and permanent removal of the emails. If that's the case, I am really disappointed in Apple. I know I made a mistake, but forgetting to set "any" to "all" is something I feel anyone can do on accident, since it's on "any" as default. And it just applies the rule to the whole mailbox! I had no chance to stop it, and now my mails are gone. Important ones from the government, tax-mails, receipts... all just gone.
    And no... I do not have a backup of those mails. I am without a time machine harddisk, and I honestly thought that deleted mails would appear in the "deleted mails" folder on iCloud, but they do not. The rules must have deleted those two, even though I have no idea why and how that should be possible.
    And yes, the emails are gone from ALL of my devices, and not just the Mac... even iCloud.com does not have any trace that they even existed.
    Please help, I know I haven't been smart about it, but permanently deleting such a large amount of emails, without warning or recovery method, simply should not be possible!
    Any support is highly appreciated. I am looking at a world of s*** if I can't recover some of these important emails. Thank you in advance!
    And here's a smiley, so you know that I am normally a very happy guy, and so you also know that I will be immensely joyful and appreciative of your help

    Geeken wrote:
    Also, is it really true that such a simple user mistake, can cause the user to lose highly important emails? I mean... It's like one click away from utter and permanent removal of the emails. If that's the case, I am really disappointed in Apple. I know I made a mistake, but forgetting to set "any" to "all" is something I feel anyone can do on accident, since it's on "any" as default. And it just applies the rule to the whole mailbox! I had no chance to stop it, and now my mails are gone. Important ones from the government, tax-mails, receipts... all just gone.
    And no... I do not have a backup of those mails. I am without a time machine harddisk, and I honestly thought that deleted mails would appear in the "deleted mails" folder on iCloud, but they do not. The rules must have deleted those two, even though I have no idea why and how that should be possible.
    Yeah, I don't think you should be disappointed with Apple. It's definitely not their fault. Only yours. Sorry to be frank, but "Any" and "All" conditionals are very obvious, and I have never made this mistake. I am not sure what you would want Apple to do to prevent this -- make an alert box come up saying "You are choosing Any which means when you do this your rule will apple to Any of the conditions you set". That would just annoy us other users that already understand what All and Any mean.
    My point is that you should study the manuals and very good knowledge before you attempt at anything with your mac especially creating Mail Rules that will automatically delete emails.
    You don't do backups which is also very strange and you should learn how to do them, as this is very important if anything happens to your computer at a later date, as you've just experienced.

  • 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.
    MD

    You 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

  • Invisible or buggy Mail Rule

    Oh boy, how to describe this? Let me try...
    I've been creating mail rules to shunt spam to the junk box. They work as expected. I don't like to click on a spam email, because if the images load, then the spammers get confirmation of a "live" email address. So I was creating a rule with my room mate's email selected instead, since it was next door to the spam. Of course, the rule was "born" populated with my room mate's email address in the "From Contains" field. So I deleted his address and put in the spam title. Now the rule works on the spam, but also all my room mate's emails keep getting sent to the junk box! Yet I completely eliminated his address from the rule. Just to make sure, I deleted and recreated the rule, to eliminate the possibility of contamination. My room mate's emails are still getting junked, and I can find no rule or reason for this in my mail account. This is truly annoying! It's like there is an invisible rule, or a corruption in my mail rules. Every time my rom mate sends me an email now, it shows up in my junk mail box. How to identify the problem & fix this? I have used the exact same method many times before to create mail rules without any problems!

    Nothing you will ever do or no amount of rules will catch all spam. I try to use basic thought on this.
    First off, you should have a good address book with trusted contacts and all their emails listed.
    Here's the 3 rules I use for spam: You might also add another for .gif attatchments.
    Depending on the spam you get, you might think of different ones to include. Any contact I have would be excluded from these rules. Any other ligitiment company or person wanting to contact me personally would not be send me anything under these conditions and would be spam to me. It won't stop the clever spamers sending messages specifically to me, but most will send to multiple addresses at one time and get picked up by these rules.

  • Why is the applescript action in a mail rule always pushed last?

    Hi,
    I try to create a rule in Mail such that an Applescript is executed and then other things are done to the message (such as move it to a folder, for example). I want this to be the preceise order of things: first, do script, next, do other things.
    I create my rule in the Mail preferences, and it all goes fine. But when I reopne the rule to examine it again, I see that the Applescript part of the actions has now been pushed to the end of the action list.
    I wonder if anyone can explain the logic of this? I didn't find any reference in the help for why is this happening. Is it because of the possible time-out associated with the applescript (so as not to hang the rest of the rule evaluation)?
    Thanks for any insights.
    -- ivan
    PM DC 2Ghz G5, PB 1.5GHz G4   Mac OS X (10.4.5)  

    I don't know exactly but it might be that Mail has no way of knowing that the Applescript has completed in order to continue. So, making it the last step ensures that the complete rules will work everytime.
    What are you trying to do with your messages? Perhaps we can figure out a workaround.
    Bronson

  • Why is an applescript always pushed to end  of action list in Mail rules?

    Hi,
    I try to create a rule in Mail such that an Applescript is executed and then other things are done to the message (such as move it to a folder, for example). I want this to be the preceise order of things: first, do script, next, do other things.
    I create my rule in the Mail preferences, and it all goes fine. But when I reopen the rule to examine it again, I see that the Applescript part of the actions has now been pushed to the end of the action list.
    I wonder if anyone can explain the logic of this? I didn't find any reference in the help for why is this happening. Is it because of the possible time-out associated with the applescript (so as not to hang the rest of the rule evaluation)?
    Thanks for any insights.
    -- ivan
    G5 DC 2GHz, G4 PB 1.5 GHz Mac OS X (10.4.1)

    I don't know exactly but it might be that Mail has no way of knowing that the Applescript has completed in order to continue. So, making it the last step ensures that the complete rules will work everytime.
    What are you trying to do with your messages? Perhaps we can figure out a workaround.
    Bronson

  • Applescript run from Mail rule does not run

    I am attempting to run an Applescript from a Mail rule. Basically, the Applescript does not run.
    Stripped to the bare essentials, the following script runs fine when Applying a rule in Mail:
    say "Hello"
    as well as this one:
    say "Hello"
    using terms from application "Mail"
    on perform mail action with messages theMessages for rule theRule
    tell application "Mail"
    -- statements to process each message in theMessages
    end tell
    end perform mail action with messages
    end using terms from
    But, when I remove the comments from the block lifted verbatim from the Applescript manual:
    say "Hello"
    using terms from application "Mail"
    on perform mail action with messages theMessages for rule theRule
    tell application "Mail"
    -- statements to process each message in theMessages
    end tell
    end perform mail action with messages
    end using terms from
    the script does not even say Hello.
    I am trying to go as far as having the Applescript create a new ToDo based on specific incoming emails, but I can't get any but the simplest Applescript to run.
    Can anyone help me?
    Tom

    The script works just fine - at least, as designed if not as expected.
    When the script runs, Mail and/or AppleScript looks for a perform mail action handler. If that is found, the commands within are executed.
    If there is no perform mail action handler then the script is run as-is (i.e. as if you clicked 'Run' in AppleScript Editor).
    And that's exactly what's happening here. In the first case (that works) the script is running because there's no (visible) perform mail action handler.
    In the second case where it apparently doesn't work, there is a handler, so that handler is being executed. Any code outside that handler is ignored (unless it's called from within the perform mail action handler.
    Since your 'say' command is outside the handler, it is ignored. However, if you move it inside the perform mail action handler you'll hear your speech.

  • Mail Rules window says "No Scripts Installed" next to "Run AppleScript"

    I'm trying to create a rule in Mail that will run an AppleScript. But when I choose "Run AppleScript" in the action section of the rule-creation window, the dropdown to the right says "No Scripts Installed".
    If I click on that dropdown, it has one option: "Open in Finder". When I choose that, a Finder window opens to the folder Library/Application Scripts/com.apple.mail/. I dropped my AppleScript file into that folder, but when I tried again to make a rule, I still got the "No Scripts Installed" dropdown.
    I'm running Mail version 6.3 in OS X 10.8.3.
    Any idea how to get this working properly? Thanks.

    When Finder opens the directory for your AppleScript (in the case of OS 10.9.1, "Library>Application Scripts>com.apple.mail"), make sure your script (not a shortcut to your script) is copied there. Once it is, you will see the script in the dropdown menu where you initially saw "No Scripts Installed".
    Hope this helps.

Maybe you are looking for