I’m surprised. The follwing script runs properly only if I manually apply the Rule in Mail when I’d like it to be run automatically when a message gets in. Any idea why? Can’t I have it run as expected?
-- copyright Mark Hunte 2013
using terms from application "Mail"
on perform mail action with messages theMessages for rule theRule
-- set up the attachment folder path
tell application "Finder"
set folderName to "Documents: : : " -- full path obviously here
set homePath to (path to home folder as text) as text
set attachmentsFolder to (homePath & folderName) as text
end tell
tell application "Mail"
repeat with eachMessage in theMessages
try
-- Save the attachment
repeat with theAttachment in eachMessage's mail attachments
set originalName to name of theAttachment
set savePath to attachmentsFolder & ":" & originalName
try
save theAttachment in file (savePath)
end try
end repeat
end try
end repeat
end tell
end perform mail action with messages
end using terms from
Model: PowerMac
Browser: Safari 536.30.1
Operating System: Mac OS X (10.8)
This post may hold some useful information for you.
I have found that using a subfolder within your Downloads Folder is the only reliable method of extracting attachments and saving them via a Mail rule:
----------------------------------------------------------------------
on CheckOrMakeFolder(dateObject)
set a to dateObject's short date string
set aa to (my EnsureTwoDigitMonthString(a's word 1) & "." & a's word 2 & "." & a's word 3) as string
set dp to (path to downloads folder as text) & "Cathouse:" & aa & ":" --New Folder based on provided Date
tell application "Finder" to if not (exists folder dp) then do shell script "mkdir -p " & quoted form of POSIX path of dp
dp
end CheckOrMakeFolder
----------------------------------------------------------------------
For years, this particular handler did a fabulous job inside of a script that extracted PDF attachments and filed them in their final destination folder in one script. Now, I have to use the Downloads Folder, and then ANOTHER script (to move them to proper final location) to accomplish the same thing. I have provided Apple with all sorts of follow up data (at their request) for the bug report I filed last year. I continue to hope.
If you are interested, this post goes into more detail.
Hi Craig, thank you so much for your response. So I thought well: ML is not the best upgrade I’ve made. What a pity!
So if I get what you said, I’d better save the PDFs to the Download folder and then use another script (via Folder Action?) to moving the PDFs to their final location, right?
I’ve seen the way the messages in Mail are now nested so deep into sub-folders and thus their path are harder to work on. Clearly the attachments are always located in a folder named Attachments at the Messages level of a Inbox.mbox’s sub-sub-sub-sub-sub-folder. Wow! Then inside of the Attachments folder, there are more sub-folders to finally reach the file I need. Hard to have Finder move that one at once.
So I’ll give the DL folder + Folder Action a shot.
The odd -very odd- thing is that yesterday 1 out of 3 messages (3 different Inbox) has been processed fully and correctly. Also when I manually launch the rule (Apple-Alt-L) on a given message the whole rule is run correctly and the AS script along.
Thanks for your input.
Model: PowerMac
Browser: Safari 536.30.1
Operating System: Mac OS X (10.8)
Right. The spotty behavior is what drove me to a more reliable (albeit kludgy) solution. This is how I receive and organize laboratory test results for my patients, and I cannot miss them.
Since your script already works, have you considered setting up a launchd agent to see if you can just run the script every few minutes from outside Mail? I use that methodology to automatically delete the lab report emails 6 months after they arrive. The launchd agent runs once weekly and clears everything from that mailbox that is over 6 months old. Not a perfect solution, but perhaps a little less effort to get it to work closer to the way you want it.