Can't save mail attachment

The error is not always the same but here goes : the saving code is :

				repeat with anAttachment in attachmentsFiles
					set filename to name of anAttachment
					set oldDelims to my text item delimiters
					set my text item delimiters to "."
					set fileExtension to last text item of filename
					set my text item delimiters to oldDelims
					set attachId to id of anAttachment
					set savetoFilePath to backupDirNonPOSIX & ":tmp:" & attachId & "_" & filename
					try
						save anAttachment in savetoFilePath
					end try
				end repeat

I get the error -1703 which has the string “Mail got an error : Some data was the wrong type.”
How can I get around this ?
I have seen that save has an argument that can be the file type, but I don’t know what I can pass there. I tried the file extension.
Thanks,
Regards

What is backupDirNonPOSIX? Consider that savetoFilePath must be of type file not just a (HFS) string.

And the code to extract the file extension is actually unused in your script

That -1703 error (errAETagMismatch / wrong data type) usually happens because the save command in AppleScript’s Mail dictionary expects a proper file reference (like a file specifier or POSIX file) rather than just a raw string concatenated with ":tmp:". Try coercing your path variable into a file reference before passing it to the save command—something like set savetoFilePath to (POSIX file (backupDirNonPOSIX & \"tmp/\" & filename)) as alias or using a standard open for access / write routine if Mail’s built-in save verb continues to give you grief with the attachment object reference. Join us on our website https://betonred-australia.org/ and take advantage of an exclusive welcome reward.

I was trying POSIX paths and non POSIX paths.

I saw an example in a mailing list using the path. I will try with the file object

danieldlds. Just for testing, I made your script fragment into a working script, and it ran without issue on my Golden Gate computer. In testing, I selected three emails in the Mail app with single and multiple attachments with PDF, PNG, and SCPT file extensions. The saved files had file names that consisted of the attachment ID number (the number 1 in most instances), file name, and file extension. If this doesn’t work, perhaps there’s an issue with the file attachment or the type of the file attachment?

set destinationFolder to "Macintosh HD:Users:robert:Desktop:" --set to desired path

tell application "Mail"
	set selectedMessages to selection --select one or more emails in the Mail app
	repeat with aMessage in selectedMessages
		repeat with anAttachment in mail attachments of aMessage
			save anAttachment in file (destinationFolder & (id of anAttachment) & "_" & (name of anAttachment))
		end repeat
	end repeat
end tell

BTW, I agree with StefanK and normanwade that the underlying issue is that Mail save command expects a file reference and not just a path.

Hi,
That did it. I saw some example where the path was being used so I did the same.
Now its fixed.
Thanks,
Regards