Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Save attachments Apple Script for Mail in Lion

Hello,


The script below has been superb for us as we need attachments that come in from specific people to be saved automatically to a folder. We then have a series of Folder Actions that take care of things thereafter.


However since upgrading to Lion this script doens't seem to work. I know a few others are having problems and wondered if this is a bug with Lion or if the changes in mail mean that this script needs re-worked. Any help would be great!


using terms from application "Mail" -----bij mail rule aanzetten

on perform mail action with messagestheMessagesfor ruletheRule-----bij mail rule aanzetten




--set theOutputFolder to (here's where you tell it the main folder to save in)

tell application "Finder"

set theOutputFolder to folder "MACHD:Users:Pinstripe:Desktop:Incoming"

end tell


tell application "Mail"


--comment the following line when running this script as a Mail.app rule function - uncomment it for testing


--set theMessages to selection --------bij testen aanzetten


repeat with theMessage in theMessages



--does the message have attachments? if not, then skip it

if (every mail attachment of theMessage) ≠ {} then



--save the attachment to the subfolder


set theAttachments to (every mail attachment of theMessage)

repeat with theAttachment in theAttachments

set thePath to (theOutputFolder as string) & (name of theAttachment)



--if same named file already exists in subfolder, skip it

if not (exists (alias thePath of application "Finder")) then


savetheAttachmentinthePath

end if

end repeat

set read status of theMessage to true

end if


end repeat

end tell

end perform mail action with messages

end using terms from

iMac, Mac OS X (10.7)

Posted on Aug 5, 2011 5:40 AM

Reply
15 replies

Aug 5, 2011 12:06 PM in response to jo flavour

However since upgrading to Lion this script doens't seem to work


uhhh, ok...?


How about telling us which bit doesn't work. Since there are multiple steps in your workflow that would be helpful.


For example you say your script saves the attachments to a folder and the Folder Action then kicks in. It would help to know whether the files are getting saved to the folder or not (which actually indicates the script IS working, and that it's the Folder Action that's amiss). Also any error messages (including messages in the system log) could help.

Aug 6, 2011 12:51 AM in response to jo flavour

well, after a short test-run as a normal script it turned out to be the "save theAttachment in thePath" line that throws out an error... several other people encountered this "bug" in Lion's mail.

Here they made a crazy workaround via GUI-Skripting, I could also imagine another one with a special ruby gem that is able to ready Mail's emlx-files but I would really say... wait for Apple to fix this bug... as usual the first version of the os is buggy and you should wait at least for the first update on a real productive system (sorry for this smart *** stuff, but this is what it's like)😉

Nov 20, 2011 11:10 AM in response to MagicUK

My script works now. I'm still using the line I referenced in an earlier post and it works now.


Distantly related: I have an Autormator workflow that attaches a file to a mail message and send it. That one works up until it's supposed to send. I had to add AppleScript to send the keystrokes to send it.


My point in mentioning this is that Apple has been really flaky over the years with AppleScript and Automator as they related to handling mail messages and attachments.


If you rely on this stuff you can't upgrade without testing or having a willingness to adapt.


Here's my entire script...



using terms from application "Mail"

on perform mail action with messagesMatchingMessages


--********Change the next line to reflect a folder location you want to save incoming images to ****

set PathToSaveAttachment to "Macintosh HD:Users:murphy:Documents:processes:rotation:in:"

tell application "Mail"

repeat with CurrentMsg in MatchingMessages

set AttachedFiles to mail attachments of CurrentMsg

repeat with EachFile in AttachedFiles

set SavedFileName to "inbound.jpg"


saveEachFileinPathToSaveAttachment & (SavedFileName)

end repeat

end repeat

end tell



--Perform Rotation

tell application "Image Events"


--*******The next line should point to the folder you referenced above *****

set theFile to "Macintosh HD:Users:murphy:Documents:processes:rotation:in:inbound.jpg"

set thisImage to opentheFile


rotatethisImageto angle 90


savethisImage

end tell



--Strip exif and move file to outbox



-- ** Important Notes:


-- The do shell script section below runs three Terminal commands, separated by semicolons.


-- This first uses the jhead command to wipe out the exif rotation information.


-- The second moves the file to another folder.


-- The third kicks off an Automator workflow to mail the file back to yourself.


-- See the post for more information on the workflow.


-- jhead needs to be downloaded and placed in the folder you'll call it from.


-- In this case jhead was placed in a folder called /Users/murphy/lab/in


-- jhead also needs to be made executable. See the post for more details.


-- the move isn't really necessary. You could alter the script and the automator workflow to skip it.


-- it was helpful to have the move for troubleshooting while working out the script.


do shell script "/Users/murphy/Documents/processes/rotation/jhead -norot Users/murphy/Documents/processes/rotation/in/inbound.jpg; mv /Users/murphy/Documents/processes/rotation/in/inbound.jpg /Users/murphy/Documents/processes/rotation/out/outbound.jpg; automator /Users/murphy/Documents/processes/rotation/mail_exif_corrected_photos.workflow"


tell application "System Events"

tell application "Mail" to activate

delay 5


keystroke "d" using {command down, shift down}

end tell


end perform mail action with messages


end using terms from

Jan 29, 2013 4:39 AM in response to jo flavour

I'll wade into this and put my two cents in for what it is worth.


I've been using the Mail save attachment script, pretty much the same as that which "jo flavour" describes, for a long time and it worked flawlessley until I upgraded to Mountain Lion. At the time I didn't make the connection that something had changed in ML. I wasn't getting any error messages... the script just did nothing or so it seemed.


I have a rule in Mail that says "If the message is from a person who is a member of such and such group AND the attachment is an XLS sheet then then run the script and save the attachment to the Desktop and then play a sound to alert me"


This was the orginal script.


(* save attachments of selected messages

AK IT Carlow April 2006

will crash if file already exits

*)

using terms from application "Mail"

on perform mail action with messagestheMsges

set theAttachmentPath to "Macintosh HD:Users:username:Desktop:"

tell application "Mail"

repeat with ThisMessage in theMsges

set Attached to mail attachments of ThisMessage

set FileIndex to 0

repeat with ThisAttach in Attached

saveThisAttachintheAttachmentPath & (name of ThisAttach)

end repeat

end repeat

end tell

end perform mail action with messages

end using terms from


using terms from application "Mail"

on run

tell application "Mail" to set sel to selection

tell me to perform mail action with messages (sel)

end run

end using terms from

Well that was all spot on before ML came along.


On doing a bit of research I found another script that included an instruction to tell Finder to set the path rather than the statement above which implies to tell Finder to set the path.


So I edited the script as follows:


using terms from application "Mail"

on perform mail action with messagestheMsges

tellapplication "Finder"

settheAttachmentPathto "Macintosh HD:Users:username:Desktop:"

end tell

tellapplication "Mail"

repeat with ThisMessage in theMsges

set Attached to mail attachments of ThisMessage

set FileIndex to 0

repeat with ThisAttach in Attached

saveThisAttachintheAttachmentPath & (name of ThisAttach)

end repeat

end repeat

end tell

end perform mail action with messages

endusing terms from.


using terms from application "Mail"

on run

tell application "Mail" to set sel to selection

tell me to perform mail action with messages (sel)

end run

end using terms from


It now works flawlesly again. I don't know why it has made the difference although the only thing I can think of is that there is a bit more rigidity in coding now in that you have to be more explicit with anything that is passed from one application to another.


Hope that helps someone.


Chrispy

Jan 29, 2013 5:54 AM in response to MagicUK

I was going to suggest closing Mail and then trashing the com.apple.Mail.plist file but I see that the Preferences folder is a bit different these days with Lion and Mountain Lion and that file no longer exists.


There are a bunch of hidden plist files in the ~/Library/Mail/....... subfolder structure but I wouldn't know where to start there.

Save attachments Apple Script for Mail in Lion

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.