Help Modifying Mac Mail Script - select .PDF attachments only

Mail Version 12.4.
OS Mojave

i have a working script that uploads attachments from an email to specific location on a network. the issue is that i need the script to only upload .PDF’s and ignore other attachment extensions ex: .jpg, .png etc. currently it will upload all attachments.



using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		set attachmentsFolder to "my:path" 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

Try replacing “repeat with theAttachment in theMessage’s mail attachments” with
[format] repeat with theAttachment in theMessage’s mail attachments whose its name ends with “.pdf”
[/format]

there wasn’t any errors, but unfortunately the change to the script did not give any results
ie neither .pdf or .jpeg was uploaded to destination.

All your statements are inside try block. So, you script hide the errors anyway. I think, the problem of your script is general, not tided with extensions - the destination folder and destination file should exist before attempts to save to it. The following script shows workaround to avoid this issue, but it isn’t tested by me:


using terms from application "Mail"
	on perform mail action with messages selectedMessages for rule theRule
		set parentFolder to "my"
		set folderName to "path"
		my makeNewAttachmentsFolder(parentFolder, folderName)
		set attachmentsFolder to parentFolder & ":" & folderName & ":"
		tell application "Mail"
			try
				repeat with theMessage in selectedMessages
					repeat with theAttachment in (theMessage's mail attachments whose ((its name ends with ".pdf") or (its name ends with ".jpg")))
						set originalName to name of theAttachment
						my makeNewEmptyFile(attachmentsFolder, originalName)
						save theAttachment in file (attachmentsFolder & originalName)
					end repeat
				end repeat
			end try
		end tell
	end perform mail action with messages
end using terms from

on makeNewAttachmentsFolder(parentFolder, folderName)
	try
		tell application "Finder"
			make new folder at folder parentFolder with properties {name:folderName}
		end tell
	end try
end makeNewattachmentsFolder

on makeNewEmptyFile(attachmentsFolder, newFileName)
	try
		tell application "Finder"
			make new file at folder attachmentsFolder with properties {name:newFileName}
		end tell
	end try
end makeNewEmptyFile
 set parentFolder to "my"
set folderName to "path" 

i assume the above is just my original

 set attachmentsFolder to "my:path" as rich text 

my folder path is “Volume:Folder”
(a folder within a volume) perhaps i expressed it wrong in my original script??

test email consisted of (2) .JPG’s and (1) .PDF

the result of the script from the last post was:
no .PDF’s uploaded and (1) .JPG uploaded without file type (name only)

Hi. I can’t tell if you’re just withholding the true path for security or if it’s actually malformed. This works as a standalone. If you watch the event log, you should be able to fix whatever’s wrong in the rule after removing the try block.

set saveLocus to (path to desktop folder as text) & "TEST:" --assumes folder named 'TEST' on desktop
log saveLocus -- for verifying the HFS path
tell application "Mail"
	repeat with aMessage in (get selection) --assumes message selected
		repeat with isAttachment in (get aMessage's mail attachments whose its name ends with ".pdf") --assumes attachment exists, but won't error, if not
			tell isAttachment to save it in file (saveLocus & name)
		end repeat
	end repeat
end tell

Following script is tested by me, and works as expected. A added ignoring case for extensions filtering (to save only pdfs):


using terms from application "Mail"
	on perform mail action with messages selectedMessages for rule theRule
		set parentFolder to (path to desktop folder) as string
		set folderName to "AttachmentsFolder"
		my makeNewAttachmentsFolder(parentFolder, folderName)
		set attachmentsFolder to parentFolder & folderName & ":"
		tell application "Mail"
			try
				ignoring case
					repeat with theMessage in selectedMessages
						repeat with theAttachment in (theMessage's mail attachments whose (its name ends with ".pdf"))
							set originalName to name of theAttachment
							my makeNewEmptyFile(attachmentsFolder, originalName)
							save theAttachment in file (attachmentsFolder & originalName)
						end repeat
					end repeat
				end ignoring
			end try
		end tell
	end perform mail action with messages
end using terms from

on makeNewAttachmentsFolder(parentFolder, folderName)
	try
		tell application "Finder" to make new folder at folder parentFolder with properties {name:folderName}
	end try
end makeNewAttachmentsFolder

on makeNewEmptyFile(attachmentsFolder, newFileName)
	try
		tell application "Finder" to make new file at folder attachmentsFolder with properties {name:newFileName}
	end try
end makeNewEmptyFile

Thank you KniazidisR!!! That worked great!!! Perfect thank you so much!!!

Now i just have to figure out how make the PDF’s got to a folder on the network. LOL

this is what i came up with based on last script. it works. not sure its right but it does work lol. any improvements would be great appreciated


using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		set attachmentsFolder to "Volume:Folder:" as rich text
		tell application "Mail"
			set selectedMessages to theMessages
			try
				repeat with theMessage in selectedMessages
					repeat with theAttachment in (theMessage's mail attachments whose (its name ends with ".pdf"))
						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

@KniazidisR

I have been trying various scripts for a week and nothing worked. I suspected this was a Security issue and not a script issue and I was correct. Something in your script finally triggered the Security window to pop up! I am so grateful.

Model: iMac 19,1
AppleScript: 2.11
Browser: Safari 605.1.15
Operating System: macOS 10.15