Mail script no longer runs

I’ve been using this script with Mail for a few months without any problems. All of a sudden it won’t run. I just get a message saying


tell application "Mail"
	set myMessages to selection
	set thisMessage to item 1 of myMessages
	set moduleTitle to subject of thisMessage
	set the clipboard to moduleTitle
	set filename to name of mail attachment of thisMessage as string
	
	set nameLength to length of filename
	set fileExtension to (rich text (nameLength - 3) thru (nameLength) of filename)
	set moduleName to "CPD " & the moduleTitle & fileExtension
	
	set savePath to ("Macintosh HD:Users:phantom99:Documents:Work:Appraisal & CPD:CPD:" & moduleName)
	
	save mail attachment of thisMessage in savePath
end tell
tell application "Finder"
	open file "Macintosh HD:Users:phantom99:Documents:Work:Appraisal & CPD:1516:CPD summary.xls"
	set AccreditingBody to choose from list {"BBA", "BBA Education", "ABA 2015", "CEA"} OK button name "Select" cancel button name "Other"
	
	set target of Finder window 1 to folder "Reflection" of folder "CPD" of folder "Appraisal & CPD" of folder "Work" of folder "Documents" of home
	set sourceFile to file "Reflective log.docx" of the front Finder window
	if AccreditingBody = {"BBA Education"} then
		set sourceFile to file "R BBA Education.docx" of the front Finder window
	else if AccreditingBody is {"BBA"} then
		set sourceFile to file "R BBA.docx" of the front Finder window
		
	else if AccreditingBody is {"ABA 2015"} then
		set sourceFile to file "R ABA 2015.docx" of the front Finder window
	else if AccreditingBody is {"CEA"} then
		set sourceFile to file "R CEA.docx" of the front Finder window
	end if
	set reflectiveFileName to "R " & moduleTitle & ".docx"
	set theNewReflection to duplicate sourceFile
	reveal theNewReflection
	set the label index of theNewReflection to 0
	set the name of theNewReflection to reflectiveFileName
	
end tell

tell application "Microsoft Excel"
	
	--get first empty row
	set row_number to 1
	
	repeat until string value of range ("B" & row_number) = ""
		set row_number to row_number + 1
	end repeat
	--paste module title into first empty slot
	set empty_cell to ("B" & row_number)
	set value of range (empty_cell) to moduleTitle
	--close active workbook with saving
end tell

tell application "Finder"
	set reflectiveFileName to "Macintosh HD:Users:phantom99:Documents:Work:Appraisal & CPD:CPD:Reflection:" & reflectiveFileName
	open file reflectiveFileName
end tell

Can anybody shed any light on this? When I launch it in Script Editor on error it gives the following replies:


tell application "Mail"
	get selection
		--> {message id 200872 of mailbox "INBOX" of account "iCloud"}
	get subject of message id 200872 of mailbox "INBOX" of account "iCloud"
		--> "My CPD module"
	set the clipboard to "My CPD module"
	get name of mail attachment of message id 200872 of mailbox "INBOX" of account "iCloud"
		--> error number -10000
Result:
error "Mail got an error: AppleEvent handler failed." number -10000

I’m using OS 10.11.3, not 10.10 (the latest version on the dropdown menu). While writing this I’ve realised that this may be the first time I’ve run this on my new iMac. It was previously run on a Mac Mini running 10.7.5.

Model: iMac (Retina 5K, 27-inch, Late 2015)
AppleScript: 2.5
Browser: Safari 601.4.4
Operating System: Mac OS X (10.10)

I’m no longer using 11.3 but 11.4.

I tested the beginning of your script :

tell application "Mail"
	set myMessages to selection
	set thisMessage to item 1 of myMessages
	set moduleTitle to subject of thisMessage
	set the clipboard to moduleTitle
	set filename to name of first mail attachment of thisMessage as string
	log result
	set nameLength to length of filename
	set fileExtension to last item of my decoupe(filename, ".")
end tell

#=====

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

#=====

I was unable to get the error #-10000.
If nothing was selected I got :
error “Il est impossible d’obtenir item 1 of {}.” number -1728 from item 1 of {}
If the selected message had no attachment I logically got : “”
If the selected message had one attachments i got the length of the filename.
If the selected message had several attachments I got their names concatenated in a single string.
Complementary detail. I saw that you extract the extension by taking the 3 characters at the end of the name.
It may be OK but name extensions may have less or more that 3 characters. jpeg, applescript, py are valid extensions.

Yvan KOENIG running El Capitan 10.11.4 in French (VALLAURIS, France) samedi 26 mars 2016 20:40:56

Yes, it’s a rather clumsy way to get the file extension and I could rewrite it to count back from the end of the string until it reaches “.”

Something to refine once the script is working again. It’s OK for now as almost all of the attachments are .pdf

I ran the code that you selected, with the same result - error -10000.

I hate this kind of answer but I guess that something is wrong on your system.
It may help to ask Mail to rebuild its mailboxes.

I edited the subscript with a piece of code extracting the extension.

Yvan KOENIG running El Capitan 10.11.4 in French (VALLAURIS, France) dimanche 27 mars 2016 10:09:31

Thanks for the re-write, much appreciated.

I think you’re right about the problem being local :(. I did a mailbox rebuild before I posted my original query but it didn’t help. I ran the script again, using a different mail message as the selection and it executed as far as

save mail attachment of message id 200879 of mailbox "INBOX" of account "iCloud" in "Macintosh HD:Users:phantom99:Documents:Work:Appraisal & CPD:CPD:CPD Just try it.pdf"
		--> error number -10000

Same error number, different action to trigger it.

Interestingly I was unable to execute

get properties of mail attachment of thisMessage

for the original message I was using, but for this one it returns

{{downloaded:true, MIME type:"application/pdf", name:"73873.pdf", file size:1195164, class:mail attachment, id:"2.2"}}

The behavior described at the end of your message is the normal one.

What are you getting if you open by hand the message which issue the error #-10000 ?
Is the attachment correctly described ?
If it is, you may apply this piece of code :

tell application "Mail"
	set myMessages to selection
	set thisMessage to item 1 of myMessages
	set moduleTitle to subject of thisMessage
	set the clipboard to moduleTitle
	try
		set fileName to name of first mail attachment of thisMessage as string
		log fileName
		set nameLength to length of fileName
		set fileExtension to last item of my decoupe(fileName, ".")
	on error
		set theSource to source of thisMessage
		if theSource contains "Content-Type:" then
			set aList to my decoupe(theSource, "Content-Type:")
			repeat with i from 2 to count aList
				set maybe to item i of aList
				if paragraph 2 of maybe contains "name=" & quote then
					set fileName to item 2 of my decoupe(paragraph 2 of maybe, {"name=" & quote, quote})
					log fileName
					set fileExtension to last item of my decoupe(fileName, ".")
					exit repeat
				end if
			end repeat
		end if
	end try
end tell

#=====

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

#=====

It will not give you the attached file itself but it will give you its name.

Yvan KOENIG running El Capitan 10.11.4 in French (VALLAURIS, France) dimanche 27 mars 2016 16:11:29

It returns the file extension, but when I go on to try and save the mail attachment it generates error -10000 again.

I’ll have to look for an alternative explanation as it doesn’t look as though the script syntax is the problem. It must be something else.

Update:

I found this - http://stackoverflow.com/questions/9204320/what-are-error-10004-and-error-10000-in-applescript

I tried adding “file” before “savePath” in the statement -

save mail attachment of thisMessage in savePath → save mail attachment of thisMessage in file savePath

It now works!

The original mail message still generates a -10000 error when it tries to get the name of the attachment but it works on other messages. I will have to keep digging to find out why that message in particular is problematic, but it works otherwise.

Thanks for your help.

Update:

The issue was, unfortunately, not fixed and has recurred with further messages. The good news is that this workaround has fixed the problem for me.

tell application "Mail"
	set myMessages to selection
	set selectedMessage to item 1 of myMessages
	move selectedMessage to mailbox "CPD" of account "iCloud"
	set thisMessage to the first message in mailbox "CPD" of account "iCloud"
	set moduleTitle to subject of thisMessage
	set the clipboard to moduleTitle
	get properties of thisMessage
	get name of mail attachment of thisMessage
	set filename to name of mail attachment of thisMessage as string
--truncated
end tell