Mac Mail Script Tweak Needed Please!

Windows IT had a functioning system that would detach PDF attachments from emails and upload them to destination folder on the network. Well its not working and they don’t know how to fix it.

I am using Mac Mail 9.3 and am able to use the Rules feature to activate a script and move the attachment to correct destination folder.

The issue is the script uploads all attachments and not just .pdf files. ex: uploads png signatures

how do i make it so it only uploads .pdf files??

script below


using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		set attachmentsFolder to "AdS:DHiRes:" as rich text
		tell application "Mail"
			set selectedMessages to theMessages
			try
				repeat with theMessage in selectedMessages
					repeat with theAttachment in theMessage's mail attachments
						set originalName to name of theAttachment
						set savePath to attachmentsFolder & originalName
						save theAttachment in file (savePath)
					end repeat
				end repeat
			end try
		end tell
	end perform mail action with messages
end using terms from

thank you in advance

You may try

using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		set attachmentsFolder to "AdS:DHiRes:" as string -- EDITED
		tell application "Mail"
			set selectedMessages to theMessages
			try
				repeat with theMessage in selectedMessages
					repeat with theAttachment in theMessage's mail attachments
						set originalName to name of theAttachment
						if originalName ends with ".pdf" then -- ADDED
							set savePath to attachmentsFolder & originalName
							save theAttachment in file (savePath)
						end if -- ADDED
					end repeat
				end repeat
			end try
		end tell
	end perform mail action with messages
end using terms from

It assumes that the attached pdf have the standard suffix “.pdf”

If I had time available I will write it.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) vendredi 17 avril 2020 19:50:19

thank you! i will try it out and see what happens.

i think i need to add “with replacing” to allow for overwriting (does that make sense?)


using terms from application "Mail"
   on perform mail action with messages theMessages for rule theRule
       set attachmentsFolder to "AdS:DHiRes:" as string -- EDITED
       tell application "Mail"
           set selectedMessages to theMessages
           try
               repeat with theMessage in selectedMessages
                   repeat with theAttachment in theMessage's mail attachments
                       set originalName to name of theAttachment
                       if originalName ends with ".pdf" then -- ADDED
                           set savePath to attachmentsFolder & originalName
                           save theAttachment in file (savePath) with replacing
                       end if -- ADDED
                   end repeat
               end repeat
           end try
       end tell
   end perform mail action with messages
end using terms from

Here is an enhanced version which no longer rely upon the availability of the suffix “.pdf”

using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		tell me to set p2t to (path to temporary items) as string
		set attachmentsFolder to "AdS:DHiRes:" as string
		tell application "Mail"
			set selectedMessages to theMessages
			try
				repeat with theMessage in selectedMessages
					repeat with theAttachment in theMessage's mail attachments
						set originalName to name of theAttachment
						set tempPath to p2t & originalName
						save theAttachment in file (tempPath) -- save it in the temporary folder
						tell application "System Events"
							set typeID to type identifier of disk item tempPath
							if typeID = "com.adobe.pdf" then -- if it's a PDF, move it
								move disk item tempPath to folder attachmentsFolder
							end if
						end tell
					end repeat
				end repeat
			end try
		end tell
	end perform mail action with messages
end using terms from

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) vendredi 17 avril 2020 21:12:41

No.
As I see, you need Mail.app to create new, non existing file each time you recieve new attacement. To do this you should write save command using correct syntax:

save theAttachment in (a reference to file savePath)