Mail Rule calls script

I am setting out to create an AppleScript that will make some changes to messages as identified in a rule. So I took the Sample as supplied in /Library/Scripts/Mail Scripts/Rule Actions and modified it for the first test. Result was not successful and then Mail crashed.
So I took the original Sample script and set that to run from the rule and ran Rules against half a dozen messages. According to this Sample script, I should have seen a dialog for each message with some info in it from that message. The first dialog looked ok but matched a message that was already in the Trash and hence not selected for the test, the second dialog was blank and then Mail crashed.
I've tried with various selections of messages and Mail almost always crashes.
Is there something I should know about as to why Apple's own Sample script fails so spectacularly?

Well, as I said it was the supplied sample rule action script in /Library/Scripts/Mail Scripts/Rule Actions and actually entitled Sample Rule Action Script.scpt, but here is the actual script:-
using terms from application "Mail"
on perform mail action with messages theMessages for rule theRule
tell application "Mail"
set theText to "This AppleScript is intended to be used as an AppleScript rule action, but is also an example of how to write scripts that act on a selection of messages or mailboxes." & return & return & "To view this script, hold down the option key and select it again from the Scripts menu."
repeat with eachMessage in theMessages
set theSubject to subject of eachMessage
try
-- If this is not being executed as a rule action,
-- getting the name of theRule variable will fail.
set theRuleName to name of theRule
set theText to "The rule named '" & theRuleName & "' matched this message:"
set theText to theText & return & return & "Subject: " & theSubject
display dialog theText
set theText to ""
end try
end repeat
if theText is not equal to "" then
display dialog theText buttons {"OK"} default button 1
end if
end tell
end perform mail action with messages
end using terms from
The above is a direct copy from the script (indentation lost though of course), as supplied by Apple as an example of the way to write
rule action scripts.
I tested with a copy of a rule that had already moved a dozen messages into a specific mailbox which was now open in Mail, so I could see those messages. I modified the copy to run the script instead of moving messages and then stop processing the rules and placed it at the top of the rule list so it would be the one and only rule to run. I then selected one or more messages in the mailbox and selected 'Apply Rules', either from the Message or contextual menu and I once saw the dialog with the subject line of a message that had already been moved to the Trash, then it was blank, then it crashed, but mostly there's just several seconds delay without anything happening before Mail simply crashes, i.e. terminates unexpectedly. It restarts ok without complaint, but will do it again as soon as I 'Apply' the rules again.
I guess I'm particularly peeved that Apple's own sample/guide, call it what you want seems to be so obviously flawed. Or am I missing something?

Similar Messages

  • Mail rule or script

    I´m using Mail and OrganizePro on my Mac. I would like to add a script working like if I move an email to a specific folder it should automatic create a task in OrganizePro related to the email. Any ideas how this can be done ? It is possible to create OrganizePro tasks by forwarding the email to a specific email. My problem is that mail rules can not be triggered by new mail added to a folder.
    DA

    My initial thought was that you could run a script and then move the mail to the mail folder.  However, it seems that OrganizePro is not scriptable so it's not possible.
    You could:
    select an email
    run a script (from the Scripts menubar) that will forward the email to the OrganizePro address and then move the mail to the desired folder.

  • Rule or script to delete specific attachments in Mail?

    Some people send signature images with every email, wasting disk space and making it difficult to search for emails with useful attachments.
    I would like to have a rule or script that deletes PointlessLogo.gif from incoming mail, while leaving other attachments intact.
    A search of the archives turns up a thread from years ago asking the same thing, apparently it was not possible with OS 10.4 (https://discussions.apple.com/message/4133181#4133181).
    Does this capability exist in Mountain Lion?

    Some people send signature images with every email, wasting disk space and making it difficult to search for emails with useful attachments.
    I would like to have a rule or script that deletes PointlessLogo.gif from incoming mail, while leaving other attachments intact.
    A search of the archives turns up a thread from years ago asking the same thing, apparently it was not possible with OS 10.4 (https://discussions.apple.com/message/4133181#4133181).
    Does this capability exist in Mountain Lion?

  • Mail command in script hangs and does not release control to calling script

    Hello folks,
    I am running in a situation which I haven't seen before.
    Main script calls function which executes another script.
    No matter which statement is the last in called script, it hangs on the last line and does not return control to main script.
    I tried to call another script not within the function - it is the same result.
    Can you help please.
    Below example of code:
    --- Main script:
    function checksum
    set -x
    /sybdump/ukgoald/scripts/egrep_expr1
    ##MAIN --- call to script script execution which runs mail command
    checksum
    ---- trace execution of called script:
    + read A B RF_FULL_NAME
    + print
    + /usr/bin/mailx -s Checksum Report [email protected]
    + 0< /sybdump/ukgoald/dump1/egrep_expr1.log3
    + print
    + return 0
    Edited by: ge**** on Dec 14, 2010 11:29 PM

    Hello folks,
    I am running in a situation which I haven't seen before.
    Main script calls function which executes another script.
    No matter which statement is the last in called script, it hangs on the last line and does not return control to main script.
    I tried to call another script not within the function - it is the same result.
    Can you help please.
    Below example of code:
    --- Main script:
    function checksum
    set -x
    /sybdump/ukgoald/scripts/egrep_expr1
    ##MAIN --- call to script script execution which runs mail command
    checksum
    ---- trace execution of called script:
    + read A B RF_FULL_NAME
    + print
    + /usr/bin/mailx -s Checksum Report [email protected]
    + 0< /sybdump/ukgoald/dump1/egrep_expr1.log3
    + print
    + return 0
    Edited by: ge**** on Dec 14, 2010 11:29 PM

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

  • Sample Rule Action Script.scpt crashes mail app sometimes

    Hi, I know next to nothing about applescript, but today came across a post which seemed like it could be solved with a little rule triggered AppleScript action. So I ...
    1. dug up the 'Sample Rule Action Script.scpt', and copied it into ~/Library/Scripts/Applications/Mail/
    2. created a rule which applied to all email messages and triggered the script
    3. selected a message, and hit cmd option L to apply rules to the selection
    and up popped an alert panel. (expected behavior) But only the first time and only on one of my two machines. On the other Mail.app crashed. (unexpected behavior)
    But then I found I couldn't reliably run it repeatedly on the first machine after all. So I repaired permissions. Now I get the crashing behavior reliably.
    Any ideas what I'm doing wrong? The script is the one everyone should have under:
    /Library/Scripts/Mail Scripts/Rule Actions/Sample Rule Action Script.scpt

    hello Camelot. I hope this is not too far afield in this thread but its as close I can find in the current discussion topics. I've been attempting to write a script to filter flagged messages from the old messages (> 6 days old) I want to delete. I use the rule to send it to my script as you discuss. My script doesn't function but when I replace it with the Sample Rule Script, things run like a charm. (Although the text that Craig sites doesn't run.) My question is what's wrong with my script that it is not triggered. It is as follows, it is compiled, and I added a sound command to try and debug it. It didn't work.
    using terms from application "Mail"
    on perform mail action with messages theMessages
    tell application "Mail"
    play sound ..........+These 2 lines added in an attempt to ascertain if the messages where even passing+
    "Basso" ..................+through the script. No sound resulted; syntax?+
    set theflaggedstatus to flagged status of eachMessage
    if flagged status is false then
    delete message
    end if
    end tell
    end perform mail action with messages
    end using terms from
    The result is no result, nothing, nada. The old messages stay in the mailbox whether they are flagged or not. I inserted a standard change color of message rule above and below the "run Applescript" rule command and the messages are colored as they should be but the Applescript is not functioning. My script, at this point, seems overly complex for what it is, but it is the result of numerous attempts to get it working. I have a number of similar scripts written with basically the same consequence.
    If this post is outside the thread here, my apologies, if it is maybe you can redirect my inquiries. thanks tT

  • Getting a script to work in Mail rules

    I have a script that works fine in Script Editor or as a stand alone script. I would like to get it to work from within Mail rules but for some reason it doesn't want to. I have tried placing hte script within the brackets of
    using terms from application "Mail"
    on perform mail action with messages theMessages for rule theRule
    and
    end perform mail action with messages
    end using terms from
    which I was thought to believe enabled script to work with Mail rules. If aynone has any ideas how to resolve this I would love to hear from them.
    My script is as follows.
    tell application "Mail"
    set the_message to item 1 of (get selection)
    set theSubject to the subject of the_message
    if theSubject is "Nouvelle Réservation" then
    set the_message to item 1 of (get selection)
    set message_text to content of the_message as string
    set nom to paragraph 8 of message_text
    set prenom_script to paragraph 9 of message_text
    set spectacle_script to paragraph 2 of message_text
    set date_script to paragraph 3 of message_text
    set node_placesscript to paragraph 5 of message_text
    end if
    end tell
    tell application "FileMaker Pro Advanced"
    activate
    open file "xxx:Users:yyy:Documents:zzz:fichiers fp7:Import reservations script.fp7"
    try
    delete every record
    end try
    set newRecord to create record -- store the result as 'newRecord'
    tell newRecord -- now target that new record
    set cell "nom_script" to nom
    set cell "prenom_script" to prenom_script
    set cell "spectacle_script" to spectacle_script
    set cell "date_script" to date_script
    set cell "node_placesscript" to node_placesscript
    do script "whole import process into «•_reservations»"
    end tell
    end tell

    Hello
    You need to tell application "Mail" to do its tasks. Also your original if clause is closed too soon.
    So a possible fix would be as follows :
    --SCRIPT1
    using terms from application "Mail"
    on perform mail action with messages theMessages for rule theRule
    my initFilemaker()
    tell application "Mail" -- # required
    repeat with oneMessage in theMessages
    set theSubject to the subject of oneMessage
    if theSubject is "Nouvelle Réservation" then -- # should enclose the rest of this iteration
    set message_text to content of oneMessage
    set nom to paragraph 8 of message_text
    set prenom_script to paragraph 9 of message_text
    set spectacle_script to paragraph 2 of message_text
    set date_script to paragraph 3 of message_text
    set node_placesscript to paragraph 5 of message_text
    tell application "FileMaker Pro Advanced"
    set newRecord to create record -- store the result as 'newRecord'
    tell newRecord -- now target that new record
    set cell "nom_script" to nom
    set cell "prenom_script" to prenom_script
    set cell "spectacle_script" to spectacle_script
    set cell "date_script" to date_script
    set cell "node_placesscript" to node_placesscript
    do script "whole import process into «•_reservations»"
    end tell
    end tell
    end if
    end repeat
    end tell
    end perform mail action with messages
    end using terms from
    on initFilemaker()
    tell application "FileMaker Pro Advanced"
    open file "xxx:Users:yyy:Documents:zzz:fichiers fp7:Import reservations script.fp7"
    try
    delete every record
    end try
    end tell
    end initFilemaker
    --END OF SCRIPT1
    Or, since I'm not comfortable with nested tell structure (tell FMP in tell Mail), I'd write something like this (with other minor changes to my preferences) :
    --SCRIPT1a
    using terms from application "Mail"
    on perform mail action with messages theMessages for rule theRule
    my fmpclearrecords()
    tell application "Mail"
    repeat with m in theMessages
    set m to m's contents
    if m's subject = "Nouvelle Réservation" then
    my fmpnewrecord(m's content)
    end if
    end repeat
    end tell
    end perform mail action with messages
    end using terms from
    on fmpclearrecords()
    set a to "xxx:Users:yyy:Documents:zzz:fichiers fp7:Import reservations script.fp7" as alias
    tell application "FileMaker Pro Advanced"
    open a
    try
    delete records
    end try
    end tell
    end fmpclearrecords
    on fmpnewrecord(t)
    tell application "FileMaker Pro Advanced"
    tell (create new record)
    set cell "nom_script" to t's paragraph 8
    set cell "prenom_script" to t's paragraph 9
    set cell "spectacle_script" to t's paragraph 2
    set cell "date_script" to t's paragraph 3
    set cell "node_placesscript" to t's paragraph 5
    end tell
    do script "whole import process into «•_reservations»"
    end tell
    end fmpnewrecord
    --END OF SCRIPT1a
    Not tested, for I don't have FMP for OSX.
    But I see no specific reason for the above to fail except for the case that certain threading issue of script run via Mail rule interferes.
    *If it still fails, check console log for specific errors.
    Good luck,
    H
    EDIT: The object referencing in FMP seems incomplete though it might work in current form.
    It would be better to specifiy complete reference such as :
    -- delete records
    tell application "FMP"
    tell document 1
    tell database 1
    tell current layout
    delete records
    end tell
    end tell
    end tell
    end tell
    -- create new record
    tell application "FMP"
    tell document 1
    tell database 1
    tell current layout
    tell (create new record)
    -- omitted
    end tell
    end tell
    end tell
    end tell
    end tell
    --etc
    Message was edited by: Hiroto

  • Mail rule no longer works - how 2 debug?

    i have a mail rule which takes an attachment & sends it on to an fba for further processing (via a do shell call) then moves the mail to a done folder.
    my shell script works perfectly when invoked from the cli, but doesn't seem to even get called from the mail rule (which used to work under tiger)
    and since "perform mail action with messages" doesn't allow interaction, i can't salt my script with display dialogs to debug...
    so where does mail rule output go? is there an error log somewhere? console shows nothing...

    on a hunch i added a delay right after i saved the attachment...that fixed it.
    but i still don't have a way of seeing what's going on in a mail rule script.

  • Applescript process message body from mail rule help please

    Would someone be kind enough to help me figure out how to do this?  I've been reading for hours on other posts and I can't get this to work.
    Here is an example:
    I would like to set up a mail rule to run an applescript that searches the body of the email for "-Front Door- Open".  I then want to parse the body and sets myDevice= "-Front Door-" and myState="Open".  After I have these variables set, I want to make an call to something like https://foo.me.com/api/token/${state.accessToken}/smartapps/installations/${app.id}/Notify?device=${myDevice}&type=ContactSensor&state=${myState}
    I understand how to do the mail rule, I just can't figure out the applescript part of it.

    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.

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

  • Mail rules enacting an applescript

    I'm trying to get Mail to enact a script based on the subject of an email. The script I have works fine when I run it through script editor but Mail doesn't seem to want to invoke it. I set up a rule that if the From is equal to a specific name then run the script. Following is the Mail portion of the script. Is there something specific I need to include so it knows which email to act upon? I set a rule to invoke a simple script activating Safari and that worked fine.
    tell application "Mail"
    set theSel to (get selection)
    set theMessage to item 1 of theSel
    set theTicket to content of theMessage
    set AppleScript's text item delimiters to space
    set AppleScript's text item delimiters to {""}
    set I_StartHere to offset of "http" in theTicket
    set I_EndHere to offset of ".pdf" in theTicket
    set thisURL to text I_StartHere thru I_EndHere of theTicket
    set myURL to (thisURL) & "pdf"
    set myFile to word 7 of myURL & ".pdf"
    end tell

    Where am I defining "triggerMessages" and "theRule"?
    In the line:
    on perform mail action with messages triggerMessages for rule theRule
    When Mail.app calls your script it passes in two pieces of information - the rule that was triggered and a list of messages that triggered the rule. This line simply assigns those two pieces of information to two variables (theRule and triggerMessages) which you can use as a reference to these items in your script..
    In your revised script, you're now capturing this information, but completely ignoring it since you continue to act upon the 'selection'. As I said you need to replace any reference to the selection with the data that was passed into your script. e.g.:
    using terms from application "Mail"
      on perform mail action with messages triggerMessages for rule theRule
        say "Joe, Elliott, has sent a new order"
        tell application "Mail"
          repeat with theMessage in triggerMessages
            set theTicket to content of theMessage
            set I_StartHere to offset of "http" in theTicket
            set I_EndHere to offset of ".pdf" in theTicket
            set thisURL to text I_StartHere thru I_EndHere of theTicket
            set myURL to (thisURL) & "pdf"
            set myFile to word 7 of myURL & ".pdf"
          end repeat
        end tell
      end perform mail action with messages
    end using terms from
    Now you'll see that the script acts upon the messages that triggered the rule. Note that I'm using a repeat loop to iterate through the messages, meaning the script will work on multiple messages that trigger the rule (as opposed to only the first one).
    I also omitted the 'text item delimiter' statements since they didn't do anything in your code. I don't know if this was an error, or if there's other code you omitted from your post.
    I'm guessing there's more to what you want to do, though, because as written the script does nothing other than set a few variables.

  • 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 are not applied to IMAP subfolders

    Hi
    I've set up a mail rule to run for 'Every Message' which runs an applescript of mine.
    The issue I have is that the script is only called for mail in the root of IMAP accounts and not mail in subfolders.
    I've searched google and the forums but cannot find any explanation. Can anybody here help?
    Thanks,
    Jason

    I should point out that the new mails in the sub folders were put there by a sieve script on the server.

  • Mail rule to modify e-mail header (via external command line tool)

    Hello,
    Apple Mail lets me add AppleScripts which will be executed whenever this rule is being called.
    I have some command line tools that lets me add some information to the e-mail's header. For example, spamprobe (a anti spam tool, which works perfectly for me) would add an e-mail header like
    X-SpamProbe: SPAM 0.9999749 8aba943058c55bb20bd974fc534b71cc
    or clamdscan would add (in combination with the command line tool formail) add information about viruses into the e-mail header, such as
    X-Virus: [clamscan] Heuristics.Phishing.Email.SpoofedDomain FOUND
    After marking the e-mails, spam can be easily filtered out. Currently I'm using procmail to call all those external tools. They work much more accurate for me than Apple Mail's built-in spamfilter.
    Now, however, I want to keep my mail on an IMAP server, thus, procmail does not work anymore. (procmail works only on local e-mails.) Is there any way to "stamp" incoming mails with Apple Mail by an external command line tool, leaving the mail at the end on the IMAP server? It should be the following workflow:
    Apple Mail checks new mails and calls a rule on any e-mail which sends a copy of the mail to an external command line tool
    The command line tool adds information to the given e-mail (e.g., virus or spam information)
    Apple Mail "replaces" the e-mail on the IMAP server with the altered e-mail
    Apple Mail calls another rule which filters all detected spam e-mails
    Is Mail + AppleScript that flexible to include the use of external command line tools?
    Certainly, an even better alternative would be to have a tool like procmail which could work directly on messages on IMAP servers. However, I'm not aware of any that could do that.
    Thanks for any comment.
    Claus

    If you want to execute a command line as a mail rule you could define an apple script that executes the command line,
    for example
    tell application "Finder"
      do shell script "ls"
    end tell
    executes the shell command "ls"
    For "ls" substitute your command line command.

  • 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

  • Application Switching not working in Spaces

    I'm having some trouble with Spaces and Application switching. Randomly the feature that switches the space to the right Application when you click on the dock stops working. The menu bar will change to the right application but the window and space

  • How do I get youtube videos to play

    Hi I'm unable to play youtube videos on my phone. I get the message 'an error has occurred' I'm on the giffgaff network and its been suggested that it's because they can't send the service book for youtube. Is there a way around this please without m

  • Cannot find the Novell Connection Manager for LDAP

    Novell Connection Manger for Java/LDAP Cannot find the Novell Connection Manager for LDAP in download I am trying to connect through a Java client to the Apache Directory Studio, LDAP server....I have downloaded the classes from the download page...s

  • Access restriction in IM52 based on company code and investment reason

    Hi, How can we have access restriction in IM52 based on company code and investment reason? thanks Randeep

  • Reg:  Amount in Local currency in MIGO

    Hi  Experts, While after posting GR against PO we have observed that system has calculated wrong amount and posted wrong amount in amount in LC ( Local Currency) filed DMBTR. How can the value be corrected can anybody help me out.