Save Attachemnt Script Fails in 10.10 Yosemite

I have been running this script as a rule since 10.6 or 10.7. In 10.8 I had to make a change to it but it kept working. Now with 10.10 it fails again.
Can anyone please help me out?

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 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 "Computer:Users:me:Downloads:" -- replace quoted text with your desired path
		set theNextDelivery to "NextDelivery.pdf"
		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
						--display dialog ("Filename ")
							save theAttachment in theFileName
						on error errnum
						end try
					end repeat
				end if
			end repeat
		end tell
	end perform mail action with messages
	do shell script "/Users/me/Documents/Scripts/Fuel_Next_Delivery/"
end using terms from

Model: Mac Mini 4,1 16GB OS X 10.10
AppleScript: 2.4 (script editor 2.7)
Browser: Firefox 33.0
Operating System: Mac OS X (10.8)

What fails, specifically? The saving of the attachment to the Downloads folder, or the shell script?

I tested, to the best of my ability, both the shell script and the Applescript. I tested the shell script independently and it ran as expected. The Applescript never deposits the attachment. This script ran fine under 10.8 & 10.9 so I’m assuming Apple again changed something.

I have a script that does pretty much the same thing when emails arrive from a specific sender, and it continues to operate automatically in Yosemite 10.10.

The only reliable method that I have come up with is for a Mail rule to move the expected message to a specified mailbox (in my case, entitled Lab), and have a launchd Agent fire every 15 minutes to process the messages in that mailbox. It is a complete and total pain in the rear end, but it has worked on every OS upgrade since November 2012.

I have simply given up on firing AppleScripts as part of a Mail rule.

We have the same problem but not for all messages. The problem always raise for specific message without a reason.
Before Yosemite these problematic messages works fine!

Any idea ?


I believe there might be an issue with saving documents in Even the GUI fails. I sent a bug report to Apple about the GUI issue and the ticket was closed because of a duplicate entry. So I’m assuming the issue is with save attachments and not Applescript.

FWIW I was able to save single attachments using the GUI but not multiple attachments. At some point saving even single attachments started to fail. This may be similar to what you described where your attachments sometimes saved. now we wait for 10.1 and hope the fix is included there.


From its original conception, Mail app wasn’t very good at scriptability. The Mail team just didn’t know AppleScript. What you gotta do is find workarounds and we’ve been doing that for like 10 years or there abouts or maybe more.

If you were to have a single thing that you would like to fix, then what would that be?


It seems they fixed it in 10.10.3.


Here is a short script which I use from time to time. The last time I ran it upon a mailbox with 712 messages.
It saved 462 files with no problem.
They are : scripts (.scpt), zip archives, Numbers documents, Jpg, png, tiff, Numbers templates, PDF text files

As you see I didn’t saved folders or packages which weren’t well treated in the past but I had none of them recently.

It’s a subset from a long script written for a French user so, it is mostly commented in French and some variable names resemble to french words

# Étape 26 - Enregistrement des pièces jointes
# Défini le dossier de stockage des pièces jointes
set attachmentsFolder to (path to documents folder as text) & "Attachments" # ATTENTION, on ne met pas de caractère ":" final
do shell script "/bin/mkdir -p " & quoted form of (POSIX path of attachmentsFolder)

tell application "Mail"
	set theMessages to selection
	# Définit la variable mailSubFolder par défaut afin qu'elle existe même s'il n'y a pas de pièce jointe
	set mailSubFolder to false
	repeat with eachMessage in theMessages
		-- set the sub folder for the attachments to the first part of senders email before a period
		-- All future attachments from this sender will the be put here.
		set {senderName, sender_Mail, theTopicSubject_, mailDate_} to my getMailInfos() # instruction appelant un handler
		log {senderName, sender_Mail, theTopicSubject_, mailDate_}
		tell mailDate_ to set dateStamp to space & (((its year) * 10000 + (its month) * 100 + (its day)) as rich text) & "_" & rich text 2 thru -1 of ((1000000 + (its hours) * 10000 + (its minutes) * 100 + (its seconds)) as rich text)
		# ATTENTION, on n'avait pas mis de caractère ":" final
		set mailSubFolder to attachmentsFolder & ":" & senderName # ATTENTION, on ne met pas de caractère ":" final
		do shell script "/bin/mkdir -p " & quoted form of (POSIX path of mailSubFolder)
		# Enregistrement des pièces jointes
		# S'il existe déjà un fichier du même nom, ajoute la date-heure du mail pour différencier
			repeat with theAttachment in eachMessage's mail attachments
				--log (count theAttachment)
					# ATTENTION, on n'avait pas mis de caractère ":" final
					set attachmentName to name of theAttachment
					tell application "System Events"
						if exists disk item (mailSubFolder & ":" & attachmentName) then
							if attachmentName ends with ":" then set attachmentName to text 1 thru -2 of attachmentName
							if attachmentName contains "." then
								set nameAsList to my decoupe(attachmentName, ".")
								set attachmentName to my recolle(items 1 thru -2 of nameAsList, ".") & dateStamp & "." & item -1 of nameAsList
								set attachmentName to attachmentName & dateStamp
							end if
						end if
					end tell
					save theAttachment in file (mailSubFolder & ":" & attachmentName)
				on error errMsg number errNbr
					log "error #" & errNbr & ",  " & errMsg
				end try
			end repeat
		on error err_MSG number err_NBR
			log "error #" & err_NBR & ",  " & err_MSG
		end try
	end repeat # with eachMessage.
end tell # Mail

#===== Called from treatAmail
on getMailInfos()
	# define the local variables whose value will be send to the caller
	local sender_Name, sender_Mail, mail_Subject, mail_Date
	log return & "Entering the handler : getMailInfos"
	tell application "Mail"
		tell item 1 of (get selection) # extracts datas from the selected message
			set sender_Name to extract name from its sender
			set sender_Mail to extract address from its sender
			set mail_Subject to subject
			set mail_Date to date received # ATTENTION, on conserve la composante time
		end tell # the message
	end tell # Mail
	# send the extracted values to the caller code
	return {sender_Name, sender_Mail, mail_Subject, mail_Date}
end getMailInfos


on recolle(l, d)
	local oTids, t
	set {oTids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
	set t to l as text
	set AppleScript's text item delimiters to oTids
	return t
end recolle


on decoupe(t, d)
	local oTids, l
	set {oTids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
	set l to text items of t
	set AppleScript's text item delimiters to oTids
	return l
end decoupe


Yvan KOENIG (VALLAURIS, France) mardi 14 avril 2015 21:19:30