I stand corrected.
My script with Mail.app rules does not work - all the time.
It doesn’t save the attachment as its supposed to. Its not stable.
Back to the drawing board
Niklas
I stand corrected.
My script with Mail.app rules does not work - all the time.
It doesn’t save the attachment as its supposed to. Its not stable.
Back to the drawing board
Niklas
The script does its duty when it is called from the Scripts Editor. It fails when it’s called by a Mail’s rule.
It’s not the only one behaving this way.
There is at least one other thread about this kind of problem.
My understanding is that it’s clearly an Apple problem.
The tip :
close access (open for access savePath)
solves only the oddity due to the inability to save in most of the folders.
This time the problem is that when triggered by a rule, Mail fails to execute several actions upon the attached files.
I’m afraid that we will have to live with that for “some” time.
Yvan KOENIG running Sierra 10.12.2 in French (VALLAURIS, France) mercredi 18 janvier 2017 11:30:26
I have solved this problem using an Automator script, running once every night (with the help of iCal / Calendar.app)
Until the guys at Apple fix this issue.
Niklas
With the current version of beta 10.12.4 the tip :
close access (open for access savePath)
is no longer needed (but it doesn’t hurt)
Yvan KOENIG running Sierra 10.12.3 in French (VALLAURIS, France) vendredi 17 février 2017 13:46:44
Nice… does that mean they are fixing this issue for the coming OS X release ?
Niklas
A part of the problem will be solved, the one forbidding to save the attachment without using the TIP.
At this time, the problem related to the rule which doesn’t pass the correct list continue to strike and I’m afraid that it will strike for long.
Yvan KOENIG running Sierra 10.12.3 in French (VALLAURIS, France) vendredi 17 février 2017 17:13:41
Hi,
I’ve tried implementing the solutions in this thread but can’t seem to make this work. Admittedly I don’t really understand applesctipt. I’ve tried inserting the tip to my script but no joy. It seems as though no matter what I do applecript will not recognize that an attachment exists. (I tried inserting a say statement before the “if theMessage’s mail attachments is not {} then” line but it never triggers. This is a script that I found here years ago and keep adjusting slightly with each release when it stops working.
I get an email every day with an attachment titled OPIS_Pricing.csv. I need to save that in my downloads directory and kick off a script. I wish apple would simply include a damn rule for saving attachments and save me this recurring headache. /rant
Any help would be much appreciated.
using terms from application "Mail"
on perform mail action with messages theMessages --for rule theRule
-- Only use hardcoded path! DO NOT USE choose folder. It will crash Mail if you do
-- Script modified for 10.8 on 09/12/13 MV
-- as of Mac OS X 10.8 Mail.app applescripts can only save to the downloads directory
-- the shell script below moves the file from downloads to its proper place for processing
set theOutputFolder to ("/Users/me/Downloads/") as rich text -- replace quoted text with your desired path
set theNextDelivery to "OPIS_Pricing.csv"
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 theOutputFolder & theNextDelivery
try
save theAttachment in theFileName
on error errnum
end try
end repeat
end if
end repeat
end tell
do shell script "/Users/me/Documents/Scripts/CurrentFuelCost/CurrentFuelCostScript_OPIS.sh"
end perform mail action with messages
end using terms from
It seems that you are facing what I wrote in message #26:
A part of the problem will be solved (is solved under 10.12.4), the one forbidding to save the attachment without using the TIP.
At this time, the problem related to the rule which doesn’t pass the correct list continue to strike and I’m afraid that it will strike for long.
Yvan KOENIG running Sierra 10.12.4 in French (VALLAURIS, France) samedi 15 avril 2017 12:24:10
I cobbled together a temporary workaround in this find command
/usr/bin/find "/Users/me/Library/Mail/V4/" -type f -name "opis_racks_6to6.csv"| sed 's/.*/"&"/' | xargs ls -lTU |sort -k 9,9n -k 6,6M -k 7,7n | awk '{ print $10,$11 }' |tail -1 | xargs -I{} cp "{}" /tmp/opis_racks_6to6.csv
The only weird thing is that it doesn’t seem to find the attachment right away. The email comes in at 7:07ish every day. Sometimes when I run the file at 8 it works flawlessly. Other days I am forced to re-run it the next morning. The timestamp on the file in the /Mail/V4/… directory indicates it wasn’t created till 10 PM
Now I have a LaunchDaemon set to make sure mail is running at 6PM so it never misses the email. The timestamp on the email is 7:07 PM. The timestamp on the opis_racks_6to6.csv file is 22:12. Can someone explain this behavior to me? I’d like to run the script at 8PM the latest. Currently I’m forced to run it at 5AM just to be certain the the correct file actually exists.
NickeZ28, could you let us know how you created the Automator work-around for this?
I have set up an Automator workflow that gets the selected messages in Mail and saves their attachments to the documents folder. The limitation is that the emails must first be selected in Mail. Is it possible for a Mail rule AppleScript to pass the received emails as input to the Automator workflow?
Hi all,
I managed to work out how to do this by actually reading what has been discussed above. Yvan’s TIP did the trick for me (thanks Yvan). The problem was that Mail was refusing to save the attachments due to some kind of permissions issue. The following code sorted it out:
set savePath to attachmentsFolder & originalName
close access (open for access savePath)
set savePath to savePath as «class furl»
save theAttachment in savePath