6 Replies Latest reply: Sep 13, 2011 9:57 AM by StephenM
StephenM Level 1 Level 1 (110 points)

I am trying to write a Applescript rule for Mail. The rule is triggered when a Mail with a specific title arrives. Here is the script that gets triggered.

 

 

<script>

using terms from application "Mail"

          on perform mail action with messages theMessages

                    tell application "Finder" to set pathToAttachments to (path to desktop folder as string)

                    tell application "Mail"

                              repeat with theMessage in theMessages

                                        if theMessage's mail attachments is not {} then

                                                  repeat with theAttachment in theMessage's mail attachments

                                                            set theFileName to pathToAttachments & (theMessage's id as string) & space & theAttachment's name

                                                            try

  save theAttachment in theFileName

                                                            on error errnum

                                                            end try

                                                  end repeat

                                        end if

                                        tell application "Preview" to open theFileName

                                        tell application "Finder" to delete theFileName

                              end repeat

                    end tell

          end perform mail action with messages

end using terms from

</script>

 

Okay, the actual script actually tells NeatWorks to open theFileName, but most people do not have NeatWorks, and the problem occurs with Preview also.

 

So, the rule works if the mail message is selected, run after the mail has arrived. I would like the rule to automatically run when the message first arrives. But the script doesn't trigger.

 

My other rules that are not based Applescript run when a message first arrives. Does anyone know why?


various, Mac OS X (10.6.2)
  • 1. Re: applescript mail rule
    twtwtw Level 5 Level 5 (4,690 points)

    What precisely do you mean by "doesn't trigger"?  The problem with this script, I suspect, has to do with the way mail handles rule actions.  As I understand it, when mail starts to arrive, it arrives (in computer terms) slowly.  so mail handles each email separately - it waits until it has enough of the headers to properly define a new message, triggers the script, and allows the script to run while the remainder of the message is downloading. This means that your script is trying to send something to Preview that hasn't actually arrived yet, which never works out well. There is, unfortunately, nook hook in the Mail scripting dictionary for telling when a particular message is fully received, so...

     

    If you could be more specific about precisely where the script is failing when run as a rule action (is it not downloading attachments, is it downloading them but not opening them?) that would help with a workaround.

  • 2. Re: applescript mail rule
    StephenM Level 1 Level 1 (110 points)

    Since I use tell application "Finder" to delete theFileName I get the trash sound if the rule is runs from a selected email. I do not get the trashing sound on inbound emails. You explanation is likely the problem.

     

    Something I tried kind of confirms that.

     

    I created a rule that if subject "foo" move to folder "goo".

     

    That runs on inbound mail.

     

    I change the rule to if subject "foo" execute Apple "found above"

     

    No trashing sound happens, So the script does not execute as expected.

  • 3. Re: applescript mail rule
    Gesualdos Level 1 Level 1 (0 points)

    I seem to be having the same problem as the original poster, and I would love a solution if anybody has one.

     

    I have a script which run perfectly by itself, whether I run it manually or set up an iCal event to do it at scheduled times. I'd like to have a mail rule search for incoming emails with a specific subject, and when found, to run this same Applescript. I set up the rule and send the appropriately-subjected message to the corresponding account, but the script never runs. No bounce of the script in the Dock, no outgoing message being sent (which is the ultimate goal of the AppleScript.)

     

    If I add to the rule's consequential clause something simple like "set color" to orange, that consequence is processed as it should, but still the AppleScript doesn't run.

     

    Anybody out there have an idea what might be causing this?

     

    This problem is happening for me on Mail 4.5 (1084) on OS 10.6.8.

  • 4. Re: applescript mail rule
    StephenM Level 1 Level 1 (110 points)

    Sorry I do not have an answer for you. One of the places I posted, a user postulated that my problem may be related to my rule being triggered by the header and my rule relying on the body of the email. My inbound mail rule would not trigger because the body for the email had not completely loaded. To me, that got the closest to an acceptable expatiation.

     

    Your problem seems the same but different. True the script does not trigger, but your rule relies on a header so the body would not need to load.

     

    I am beginning to believe the Applescript rules just don't run well on incoming mail messages.

  • 5. Re: applescript mail rule
    twtwtw Level 5 Level 5 (4,690 points)

    You might also be running into threading issues.  Mail gets incoming email on one thread and sends email on another, but applescript is not threaded, and doesn't work well across threads.

     

    You might try a workaround: rather than trying to send an email immediately, create an email and save it to the Outgoing Messages folder so that it can be sent at the next scheduled time.

  • 6. Re: applescript mail rule
    StephenM Level 1 Level 1 (110 points)

    It might be a threading issue, but I am not sure it is as you think.

     

    I do see a likely threading error with Applescript with  Services. I set up a Speak Service using Automator; it speaks the selected text. I have set that up on a Keyboard shortcut. If I invoke the Speak Service, it runs fine. If I invoke the Speak Service a second time while the first block of text is being read, The speak block continues to execute proprerly, but I get a Workflow Error for the second block of selected text. My guess, as you stated, is Applescript is not re-entrant and that generates the error. 

     

    If the mail problem were a threading issue, I would expect the rule to work the first time but fail the second time the rule got invoked. The rule does not even run properly the first time or the only time the rule gets invoked.

     

    It maybe a threading error in so far as the rule gets triggered as soon as the rule is detirmined to be true, but the piece of mail might not be completely loaded. With the imcoplete message, the Applescript cannot completely and correctly check the email.

     

    This is further supported by the fact, if the mail is executed on the e-mail as a lone piece of mail in the Inbox, The Applescript rule runs as expected.