Filter mail attachments

I have an applescipt triggered by mail rule that successfully saves my email attachments to a folder. I am tinkering with it b/c I want to add a filter so that only pdf attachments are saved. For instance, If I receive an email with 3 attachments but only one of them is a pdf then I only want the pdf saved to the desktop folder. I added some lines in my applescript in which i attempt to detect whether or not the attachment is pdf and then save it based off that, but it is not working. here is my script:

using terms from application "Mail"
	on perform mail action with messages these_messages for rule this_rule
		set the message_count to the count of these_messages
		repeat with i from 1 to the message_count
			set this_message to item i of these_messages
			set these_attachments to every mail attachment of this_message
			repeat with z from 1 to the count of these_attachments
				set this_attachment to item z of these_attachments
				set attachmentName to name of this_attachment
				using terms from scripting additions
					set savePath to (path to desktop as string) & "MailRuleAttachments:"
				end using terms from
				tell application "Finder"
					if (kind of this_attachment) is "PDF document" then
						set mailpdf to true
						set mailpdf to false
					end if
				end tell
				if mailpdf then
						set openForAccess to open for access file (savePath & attachmentName) with write permission
						save this_attachment in (savePath & attachmentName)
					on error
							close access openForAccess
						end try
					end try
				end if
			end repeat
		end repeat
	end perform mail action with messages
end using terms from

Take a look at Script: Import mail attachments filtered by MIME type. It’s late over here and I never really used the script but it will probably get you started

I am also trying to build a script that automatically saves attachments to a specified folder. I was able to build an AppleScript that worked, however, it had issues and I need to add two features: 1) 10 second delay from the time an email arrives in my inbox until the AppleScript engages with the attachments; and 2) an exclusion so that .gif, .png, and .jpeg attachments are ignored and not saved.

I tried searching the forum and found a few similar AppleScripts, however, I am not sufficiently skilled to edit my AppleScript successfully and thus am stuck. Here is the code, with my 10 second delay attempt included. Happy to receive insults if it also comes with help:

– Set the path to the folder where you want to save attachments

set saveFolderPath to “Drive:Users:UserName:Folder:Mail Attachments”

– Add a 10-second delay at the beginning

delay 10

– Get all messages in Apple Mail

tell application “Mail”

set allMessages to every message of inbox

repeat with currentMessage in allMessages

set messageAttachments to mail attachments of currentMessage

repeat with currentAttachment in messageAttachments

set attachmentName to name of currentAttachment

set savePath to saveFolderPath & “:” & attachmentName

save currentAttachment in file (savePath) – Save the attachment to the specified folder

end repeat

end repeat

end tell

I think you should setup 2 scritps. one to save the attachments, and then another one as a folder action that deletes the files you don’t want. my script above works fine for saving attachments, just get rid of the part that i have where it tries to detect pdf file type… that breaks it due to a bug in the from what i have been told.

Thank you. I will test this in the next few days and report back.

i altered the fredrick’s script to only save pdf files…woked like a charm!

I am interested in glenbrubin’s script, especially if it could also include Word, Excel and PowerPoint attachments along with PDF. That is really what I’m trying to achieve. I get sent attachments for work, but the sender’s email has logos, animations, business contact information, etc. which also show up as ‘attachments’, so I am trying not to save those.

I tried Fredrik71’s script. It worked once at most. Samples of the two error messages I receive are below, though I have inserted XYXY or an ALL CAPS REFERENCE to mask actual identifiers.

I also have another issue when I run the script, in that the attachments themselves sometimes never download (they say ‘downloading’ but never do download), so I have to go via webmail to grab the attachments. I thought this might be because the script was not allowing enough time to download the attachment from the server before running the script, but I am absolutely in way over my head and probably was silly of me to think I could somehow figure this out.

Here are the error log samples:

"Error: -1719. Can’t get «class attc» 1 of «class mssg» id 536567 of «class XYXY» “NAME OF MAIL FOLDER” of «class XYXY» id “EDBA8481-D1B3-4540-803C-1F4DB1F1B306”. Invalid index.


“Error: -10000. AppleEvent handler failed.”