First off, let my say that is not really so much of an Applescript issue, per se. It’s more of an Apple Mail issue, I think. So I have an applescript that I got from this forum for the purpose of downloading mail attachements from a particular sender. The script looks like this:
using terms from application "Mail"
on perform mail action with messages theMasterList
tell application "Finder" to set pathToAttachments to ("Macintosh HD:Users:tinpanalley:Data:Another Heavenly Morning:AHM Documents:Lake Tahoe Area Ski Forecasts:")
repeat with theMessage in theMasterList
set suffix to 0
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
set suffix to suffix + 1
set theFileName to theFileName & " - " & (suffix as string)
save theAttachment in theFileName
end try
end repeat
end repeat
end perform mail action with messages
end using terms from
I have a Mail rule set up such that the above script is triggered when we receive mail from a particular domain. (There will be a PDF attached to these that I wish to download.) However, this “rule” never seems to take effect. What I mean is when I go in and edit the script or the rule or whatever - then Mail asks me if I want to apply this rule to the mail ALREADY in the the inbox. If I say “yes” then boom boom boom all the apprpriate attachments are dumped into my reception folder. Great !
However, once I close up the Preferences window in Mail and wait - we continue to get emails from the sender in question but the mail rule never seems to be activated for some reason. When I go to my receiving folder - there’s nothing in it. Although I’ve got new mail with the respective attachements in tow.
Anyone got any ideas on why this mail rule is not REALLY doing it’s job ?? Thanks in advance for any insight !! KB
KB:
I have found that you need to set the folder location outside of the Mail tell block:
using terms from application "Mail"
on perform mail action with messages theMasterList
set pathToAttachments to SetPath()
repeat with theMessage in theMasterList
set suffix to 0
repeat with theAttachment in theMessage's mail attachments
set theFileName to (pathToAttachments & (theMessage's id as string) & space & theAttachment's name) as string
try
save theAttachment in theFileName
on error
set suffix to suffix + 1
set theFileName to theFileName & " - " & (suffix as string)
save theAttachment in theFileName
end try
end repeat
end repeat
end perform mail action with messages
end using terms from
-------------------------
on SetPath()
set a to ("Macintosh HD:Users:tinpanalley:Data:Another Heavenly Morning:AHM Documents:Lake Tahoe Area Ski Forecasts:")
return a
end SetPath
You also do not need the Finder for that job.
Craig,
Thanks for the reply. The problem still exists. And I guess it’s more of a Mail problem than an Applescript problem. The issue seems to be that the Mail “rules” are not automatically being applied.
If I receive mail that fits the rule - nothing happens. The attachent does NOT get downloaded. If I however, go to the mail in question, right-click and select “Apply Rules” then the applescript runs and the attachment is apprropriately downloaded.
What would stop Mail from triggering the rule? Do you know? To me, that seems to be the glitch - not the Applescript itself. BTW, your new script is the one I’m now testing with. It just won’t get triggered automatically either way…
Thanks again, KB
KB:
Respectfully, I do not believe that you have a Mail glitch. I use a similar Mail rule for my office that downloads PDF attachments and saves them properly automatically; the script has worked flawlessly for over a year. I went through your script and mine line by line and found one more thing that may help:
using terms from application "Mail"
on perform mail action with messages theMasterList
set pathToAttachments to SetPath()
tell application "Mail" to repeat with theMessage in theMasterList--This is what I use in my repeat loop
set suffix to 0
repeat with theAttachment in theMessage's mail attachments
set theFileName to (pathToAttachments & (theMessage's id as string) & space & theAttachment's name) as string
try
save theAttachment in theFileName
on error
set suffix to suffix + 1
set theFileName to theFileName & " - " & (suffix as string)
save theAttachment in theFileName
end try
end repeat
end repeat
end perform mail action with messages
end using terms from
-------------------------
on SetPath()
set a to ("Macintosh HD:Users:tinpanalley:Data:Another Heavenly Morning:AHM Documents:Lake Tahoe Area Ski Forecasts:")
return a
end SetPath
Good luck; please report back.
Well here’s the oddity that I’ve found. The rule & script will still not run on the machine where they are needed. However, I have moved all the assets to my powerbook to test. Works fine. Attachments are swiftly downloaded with no intervention. I don’t know what I’m missing on the other machine - a G4 450 running 10.4.8.
Odd. I’m not sure what to do to get it operational on the G4 where we need it…
I’m going to look into it further and will report back to you.
Thanks so far, KB
Yeah…I don’t get it. Stumped & frustrated. Tweaked some things and nothing seems to fix it. The whole process works clean as a whistle on my laptop. But the machine in question will NOT play nice. Both are running Mail 2.1 & OS 10.4.8.
I just don’t know. This type of troubleshooting is the most frustrating to me. When there seems to be no logic to the results…
lame …
Hi Kevin and Craig,
Kevin,
could it be that you have a rule before your rule that contains the command “Stop evaluating rules”?
Craig,
setting the path outside the Mail tell block or “using terms from” block is no needed.
Even
(((path to home folder) as Unicode text) & "Data:Another Heavenly Morning:AHM Documents:Lake Tahoe Area Ski Forecasts:")
should work. Important are the parentheses round path to home folder, otherwise it will be compiled to path to home folder rule type Unicode text
Thanks for that, Stefan. I usually use a lot of parentheses, but apparently did not when I wrote that script last year. I appreciate your input.
Hi Stefan,
Unfortunately, there are only 2 rules period. One is the stock “Apple rule” that has been deactivated. The rule in question is active and has been moved above the Apple rule.
Thanks, KB
PS: Still puzzled by this whole thing…
So I’m back to conclude this topic and post the findings and the “cure.”
Per the suggestion from David Gimeno Gost on Apple’s Mail Discussions I removed the phrase regarding the “messageID”. This fixed everything. It does NOT explain why the script ran flawlessly on my laptop while choking on the G4 - but that’s OK with me for now.
Fact is: it’s working correctly. Thanks for the help along the way!
I’m sure I’ll be back…