Revealing Mail attachments after save

Hi,

This script helps you open the enclosing folder of email attachments immediately after saving them in Mail.app. Tested on Sequoia, Big Sur, Catalina, on Intel or M1 CPUs.

How it works - you attach it to a keyboard short-key (in my case Ctrl + S) that you press inside the Mail.app. Works either to selected message in the mailbox/inbox window or to open message in the message view window.

What’s lacking, however, and what I cannot script sufficiently well is how to make the saved attachment not only being revealed in Finder, but also to be selected. Including multiple attachments selected.

Also, I’ve never tried integrating this script within a shortcut workflow (i.e. by using the Shortcuts.app).

use scripting additions

tell application "Mail"
	
	set theMessages to selection --in case no messages are selected in a mailbox
	if the (count of every item of theMessages) is 0 then --because Mail.app allows to select mailbox without selecting any messages
		display alert "No emails selected!"
		return
	end if
	
	try -- in case Cansel is pressed
		set theFolder to (choose folder default location path to downloads folder) as string
	on error
		display alert "No folder selected!"
		return
	end try
	
	--with repeating for each selected message, tus allowing selecting multiple messages
	repeat with oneMessage in theMessages
		set theAttachments to mail attachments of oneMessage as list
		
		if (count of every item of theAttachments) is not 0 then --only if attachment is present
			--with repeating for each attachment
			repeat with oneAttachment in theAttachments
				set fileName to the name of oneAttachment
				set theFile to (theFolder & fileName)
				save oneAttachment in theFile
			end repeat
		else --What happens if no attachment is present?
			display alert "This email message lacks attachment!"
		end if
	end repeat
end tell

tell application "Finder" to reveal folder theFolder and (activate)
return