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

You’re pretty close…

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

makes no sense. Specifically, activate is a system level command to bring an app frontmost. Including it here, with an and statement turns it into a boolean check.

Besides, you wanted to highlight the file(s) downloaded, not their folder. That’s easy if there’s only one file you already have what you need. Just change this line to:

tell application "Finder"
	activate
	reveal theFile
end tell

If you’re dealing with multiple files then you can maintain a list of files, and pass that to the Finder’s reveal command:

use scripting additions

tell application "Mail"
	set files2Reveal to {}
	
	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
		return
	end if
	
	try -- in case Cancel is pressed
		set theFolder to (choose folder default location path to downloads folder) as text
	on error
		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
				copy theFile to end of files2Reveal
			end repeat
		else --What happens if no attachment is present?
			display alert "This email message lacks attachment!"
		end if
	end repeat
end tell

if files2Reveal is not {} then
	tell application "Finder"
		activate
		reveal files2Reveal
	end tell
end if

note, I made a couple of edits for my own purposes, mostly nixing ‘display alert’ in favor of silent failures, but that’s your choice