Script no lonher working in Mojave and following

I ysed this sctipr until I had Sierra now it doesn’t work anymore.

using terms from application "Mail"
	on perform mail action with messages theMessages
		set attachmentsFolder to "Volumes:Macintosh HD:Users:kp:Documents:Attachments" as rich text
		tell application "Mail"
			set selectedMessages to theMessages
			try
				repeat with theMessage in selectedMessages
					set counter to 1 -- added
					repeat with theAttachment in theMessage's mail attachments
						set subjectText to (get theMessage's subject) as rich text
						set originalName to ((every word of subjectText) as rich text)
						set savePath to attachmentsFolder & originalName & (counter as rich text) --modified
						set counter to counter + 1 -- added
						save theAttachment in file savePath
					end repeat
				end repeat
			end try
		end tell
	end perform mail action with messages
end using terms from

Code posting tags added by NG. (See the guide here). Also spaces corrected in the second line of code.

Hi.

I assume you still have the script set as an active mail action in Mail, that your computer’s hard disk’s still called “Macintosh HD”, that the attachment folder specified in the second line of the script still exists, and that “doesn’t work any more” means that attachments aren’t being saved to that folder and there’s no error message.

I’m still getting to grips with Ventura (macOS 13.6) and the somewhat flaky Mail myself. But my first observation would be that the path to your downloads folder isn’t correctly specified. HFS paths (those with colon separators (“:”)) don’t begin with “Volumes:”. They begin with the disk name.

Secondly, the script isn’t inserting a colon between “Attachments” and the file name. It should do, otherwise it’s trying to save a file whose name begins with “Attachments” to your Documents folder.

Thirdly, the words of the message’s subject are being concatenated together without an explicitly set delimiter.

Fourthly the script’s not supplying a name extension for the saved file. I don’t know, but maybe the system isn’t happy with this.

Here’s a reworking of the script that’ll hopefully deal with these issues. The path creation parts work, but I’ve not tried an actual save or running the script as a mail action.

using terms from application "Mail"
	on perform mail action with messages theMessages
		using terms from scripting additions
			set attachmentsFolder to (path to documents folder as text) & "Attachments:" -- Colon after "Attachments" included here.
		end using terms from
		tell application "Mail"
			set selectedMessages to theMessages
			try
				repeat with theMessage in selectedMessages
					set subjectText to theMessage's subject
					set rootPath to attachmentsFolder & my compress(subjectText)
					set counter to 1 -- added
					repeat with theAttachment in theMessage's mail attachments
						set attachmentName to theAttachment's name
						set savePath to rootPath & (space & counter & my getNameExtension(attachmentName))
						save theAttachment in file savePath
						set counter to counter + 1 -- added
					end repeat
				end repeat
			end try
		end tell
	end perform mail action with messages
end using terms from

on compress(txt)
	set astid to AppleScript's text item delimiters
	set AppleScript's text item delimiters to ""
	set txt to txt's words as text
	set AppleScript's text item delimiters to astid
	return txt
end compress

on getNameExtension(fileName)
	if (fileName contains ".") then
		set astid to AppleScript's text item delimiters
		set AppleScript's text item delimiters to "."
		set extn to "." & fileName's last text item
		set AppleScript's text item delimiters to astid
	else
		set extn to ""
	end if
	return extn
end getNameExtension