Saving Mail attachments


(John Mitchell) #1

Hi all

OSX 10.11.6
The script below - in a previous version successfully saved all attachments from the emails in this (example) Mail folder.

tell application "Mail"
	activate
	try
		set emailCount to count of messages of mailbox mailBoxName of mailbox catName
		set attachmentCount to 0
		set errorCount to 0
		repeat with i from 1 to emailCount
			set theMessage to item i of messages of mailbox mailBoxName of mailbox catName
			set theAttachments to every mail attachment of theMessage
			repeat with a from 1 to length of theAttachments
				set theAttachment to item a of theAttachments
				try
					set theAttachmentName to name of theAttachment
					set theSavePath to attFolder & ":" & theAttachmentName
					**save theAttachment in theSavePath**
					set attachmentCount to attachmentCount + 1
				on error errMsg number errNum
					set errorCount to errorCount + 1
				end try
			end repeat
		end repeat
		display dialog "Saved " & attachmentCount & " files, with " & errorCount & " errors" with title "Attachment Report" buttons "OK"
	on error errMsg number errNum
		display dialog "No message selected or error " & return & errNum & space & errMsg ¬
			buttons {"Quit"} default button 1 with title emailApp
		return {false, "", ""}
	end try
end tell

Now I get “Mail got an error: AppleEvent handler failed.” on the statement save theAttachment in theSavePath.
All the variables for the mail folders and the save folder are defined.

here is an example of the output:
etc:etc:Master:Construction Research:Extension of IFC Standard for Victorian Cadastre:ATT Email Attachments:Report_ver0.3_7-10-2016.docx.

Any clues?


(koenigyvan) #2

Due to the couples of asterisks which enclose it, the instruction save theAttachment in theSavePath
refuse to compile.

You are facing a problem which I discovered only in Sierra.
It’s solved if you use:

close access (open for access theSavePath) save theAttachment in file theSavePath

Oops, I forgot to say that I had to edit the two instructions triggering the mailbox.
The working syntax was :

set emailCount to count of messages of mailbox(mailBoxName & "/" & catName) set theMessage to item i of messages of mailbox(mailBoxName & "/" & catName)


(John Mitchell) #3

Hi Yvan

I think the asterisks were from my highlighting the line in the example.
I’m on El Capitan, so not sure of implication.
the slash syntax I’ve seen but my original syntax works OK

I have found that I can make it work as:
save theAttachment in file theSavePath

does this work also for you?

is your close/open solution doing something similar?


(koenigyvan) #4

Hello John
Under El Capitan the problem doesn’t exists.
It was introduced with Sierra whose version 1, 2, 3 require the close access (open for access).
In 10.12.4 it’s no longer needed BUT the problem is not solved for every application.
I can’t test if it is for Merdosoft products which aren’t allowed to enter my home.
I’m sure that the problem continue to strike upon Notes under 10.12.4.
You may find an intersting script at : http://macscripter.net/viewtopic.php?id=45602