Benches Uncategorized

1

Problem with Mail Rule

apple Verified

Imported from the Apple Support Forums via the Wayback Machine 🔗

I am trying to set up a mail rule that triggers an Applescript. The Applescript will open all attachments with Preview in this case. I know the rule is being triggered because I have two actions for the rule, run the Applescript and Make a sound, and I hear the sound.

Here is the 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
set theText to content of theMessage
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
tell application "Preview" to open theFileName
tell application "Finder" to delete theFileName
end repeat
end if
end repeat
end tell
end perform mail action with messages
end using terms from





(* tell application "Mail"
set myMessages to selection
tell me to perform mail action with messages myMessages
end tell
*)

If I comment out the stub at the bottom, and run the script, it runs perfectly all the time.

If I select the e-mail in question and trigger using *Apply Rules*, the script runs some of the time.

I have yet to get the script to trigger on newly arriving e-mails.

The rule is checked and I haven't found a +Apply Rules to Incoming Emails+ check box. I thought that happen automatically. Anyone have any ideas on what is going wrong or further ways I could debug it?

1 reply

Sign in or join Beachball Help to reply.

  1. 0

    Apple Support Community member

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Testing your script, I found that, in the few cases where it didn't work well, I could have it work properly with the following modification:

    *tell application "Preview" to open theFileName*
    *delay 1*
    *tell application "Finder" to delete theFileName*

    It should also be possible to replace *delay 1* with something better, like *repeat while … end repeat*.

    Hope it can help.