Impossible to save an attachment as a rule

I have tried it 10000000 times with every permutation of the examples seem. Sometimes it has work, but never the definitive program, but only simpler tests with simpler paths, or so. The errors it gives are not always the same, the the latests ones are: AppleEvent management error.

using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		tell application "Mail"
			delay 1
			set mercadonaTicketsFolder to (((path to home folder) as string) & "Documents:documentos:Mercadona:")
			repeat with theMessage in theMessages
				repeat with theAttachment in theMessage's mail attachments
					try
						save theAttachment in (mercadonaTicketsFolder & (name of theAttachment))
					on error errMsg
						display dialog "Error: " & errMsg
					end try
				end repeat
			end repeat
		end tell
	end perform mail action with messages
end using terms from

Any help would be highly appreciated.

the save attachment line might need to have the file keyword like so…

save theAttachment in file (mercadonaTicketsFolder & (name of theAttachment))

But although it doesn’t complain with file, it doesn’t do anything either. The last reference I took was the this one, and it doesn’t use the file thing:

Funnily, it sometimes fails giving me the next attachment name:

MacBookAirHD:Users:juanfc:Library:Containers:com.apple.mail:Data:Documents:documentos:€:Mercadona:Maxwell eq.jpeg

with this script (the file Maxwell eq.jpeg is the real name of my test mail attachment):

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		tell application "Mail"
			set mercadonaTicketsFolder to (((path to home folder) as string) & "Documents:documentos:€:Mercadona:")
			repeat with theMessage in theMessages
				repeat with theAttachment in theMessage's mail attachments
					try
						display dialog (mercadonaTicketsFolder & (name of theAttachment))
						save theAttachment in file (mercadonaTicketsFolder & (name of theAttachment))
					on error errMsg
						display dialog "Error: " & errMsg
					end try
				end repeat
			end repeat
		end tell
	end perform mail action with messages
end using terms from

The final script I have “working” only does its job when the mail message is received alone. When the message arrives inside a bunch with others, it does the colouring and the other things, except the most important one I need, moving (or copying) the attachment to its folder.

using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		tell application "Mail"
			set mercadonaTicketsFolder to "MacBookAirHD:Users:me:Documents:documentos:€:Mercadona:"
			repeat with theMessage in theMessages
				repeat with theAttachment in theMessage's mail attachments
					set originalName to name of theAttachment
					set savePath to mercadonaTicketsFolder & originalName
					try
						save theAttachment in file savePath
					on error errMsg
						display dialog "Error: " & errMsg
					end try
				end repeat
			end repeat
		end tell
	end perform mail action with messages
end using terms from