Applescript for a Mail rule : extracting attached PDF(s)

Hello,

Thanks for reading my post. I am new here, and also new on the Mac World.

I receive my posts and documents via a scanner, that e-mails them in an appended non-searchable PDF.

I want to create a rule in Mail, for each incoming message that has one or more PDF attachment(s).

When that rule is triggered, I need an AppleScript to run, which would extract the appended PDF file(s) and copy it/them into a directory on the Desktop called ‘Incoming PDF’, so that I can process them later with Adobe Pro and its OCR feature and make them PDF searcheable.

IMPORTANT : As there might be different PDF files with the same name coming into this directory (this stupid scanner names each file as Scan.PDF), the AppleScript should make sure that each PDF gets a unique name.

Does anyone want to help me ?

Thanks in advance.

Hi,

welcome to MacScripter.

Try this, the folder “Incoming PDF:” must exist on desktop, there is no error handling


using terms from application "Mail"
	on perform mail action with messages The_Messages
		set dt to path to desktop
		set Save_folder to (dt as text) & "Incoming PDF:"
		repeat with This_Message in The_Messages
			repeat with oneAttachment in mail attachments of This_Message
				set Nm to name of oneAttachment
				if Nm ends with ".PDF" then
					set fName to my checkFileName(Save_folder, Nm)
					save oneAttachment in fName
				end if
			end repeat
		end repeat
	end perform mail action with messages
end using terms from

on checkFileName(fDir, fName)
	try
		set f to (fDir & fName) as alias
		set {name:Nm, name extension:Ex} to info for f
		if Ex is missing value then set Ex to ""
		if Ex is not "" then set Nm to text 1 thru ((count Nm) - (count Ex) - 1) of Nm
		set idx to 0
		repeat
			set idx to idx + 1
			set checkName to (fDir & Nm & "_" & (idx as string) & "." & Ex)
			try
				checkName as alias
			on error
				return checkName
			end try
		end repeat
	on error
		return (fDir & fName)
	end try
end checkFileName

Try something like this:

-- This line is for manual testing
-- tell application "com.apple.mail" to set theseMessages to (get selection)

on perform_mail_action(theData)
	set outputPath to (path to desktop as Unicode text) & "Incoming PDF:"
	set theseMessages to |SelectedMessages| of theData
	
	tell application id "com.apple.mail"
		repeat with thisMessage in theseMessages
			repeat with thisItem in mail attachments of thisMessage
				try
					if MIME type of thisItem is "application/pdf" then
						save thisItem in (outputPath & "Scan-" & id of thisMessage & "-" & id of thisItem & ".pdf")
					end if
				end try
			end repeat
		end repeat
	end tell
end perform_mail_action

Thanks, this is pretty clever :smiley:

Many thanks, this works very nice. Congratulations !

The name arrives as e.g. Scan-177202-2.pdf

Is there a way that the name of the created .PDF file(s) includes the date/time of the received e-mail ?

YYYYMMDDHHmmss-MessageID-ItemID.pdf where YYYYMMDDHHmmss is the arrival time of the e-mail in my mailbox ?

example:

20081021101845-177202-2.pdf ?

Try something like this:

on perform_mail_action(theData)
	set outputPath to (path to desktop as Unicode text) & "Incoming PDF:"
	set theseMessages to |SelectedMessages| of theData
	
	tell application id "com.apple.mail"
		repeat with thisMessage in theseMessages
			repeat with thisItem in mail attachments of thisMessage
				try
					if MIME type of thisItem is "application/pdf" then
						tell ((date received of thisMessage) as «class isot» as string) to ¬
							text 1 thru 4 & text 6 thru 7 & text 9 thru 10 & ¬
							text 12 thru 13 & text 15 thru 16 & text 18 thru 19
						
						save thisItem in (outputPath & "Scan-" & result & "-" & id of thisMessage & "-" & id of thisItem & ".pdf")
					end if
				end try
			end repeat
		end repeat
	end tell
end perform_mail_action

Wonderful, this works perfectly. thanks bruce