save attachment mail script

HI folks,

I have a seemingly basic script that I want to write but I can’t figure out how to get started.

Basically in mail, I will create an action to execute a script. All I want the script to do is something like this:

Script 1 ) If this email has a .pdf attachment, save .pdf to this folder
a different script ) if this email has a .wav attachment, save .wav this folder, open attachment.

I don’t want the same in the same script, but if I can get the format of one, then I can execute a number of scripts that I want with different emails that consistently send me different emails and attachments (especially for my voicemails via callwave that get sent to my computer).

I have done a pretty complete search on the site and the other references I have found don’t really do what I want. Any help would be greatly appreciated. Cheers!

-i

Hi,

try this for the PDF files

set destinationFolder to choose folder with prompt "Choose a folder to save PDF attachments"

tell application "Mail"
	set theMessages to selection
	repeat with This_Message in theMessages
		repeat with oneAttachment in mail attachments of This_Message
			set {name:theName, MIME type:mimeType} to oneAttachment
			if mimeType is "application/pdf" or theName ends with ".pdf" then
				save ma in (destinationFolder as Unicode text) & theName
			end if
		end repeat
	end repeat
end tell

StefnK,

thanks for the code. I saved this in my mail scripts folder and ran a rule in mail – if FROM contains personsName run applescript savePDF, but every time I run the rule it crashed mail.

I forgot that all that mime type stuff is needed. Going to make it a bit more complicated for .wav files eh.

-i

for a rule the syntax is different.
Do you need the prompt for the destination folder, or can you “hard code” the path like the
“set destination folder” line? (the colon at the end is important)

using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		set destinationFolder to (((path to desktop) as Unicode text) & "saved_PDF:")
		tell application "Mail"
			repeat with This_Message in theMessages
				repeat with oneAttachment in mail attachments of This_Message
					set {name:theName, MIME type:mimeType} to oneAttachment
					if mimeType is "application/pdf" or theName ends with ".pdf" then
						save ma in (destinationFolder as Unicode text) & theName
					end if
				end repeat
			end repeat
		end tell
	end perform mail action with messages
end using terms from

for WAV change this line

if mimeType is "audio/wav" or theName ends with ".wav" then

yes, it can just be hardcoded. I tried you new script (after adding the folder “saved_PDF” to my desktop, and got no result. This time there was no crash though which is a big step up! Am I assuming correctly that variable names like theMessages and theRule are predefined variables by mail already? That I don’t need to declare them at the beginning? So, in theory, this email as is should work? Strange. Where can I find a reference for these terms so that I can break this apart a bit and see what is going wrong?

Thanks for trying to help.

-i

Yes, no futher variables have to be declared.

I usually test my mail rule scripts this way

-- using terms from application "Mail"
-- 	on perform mail action with messages theMessages for rule theRule
set destinationFolder to (((path to desktop) as Unicode text) & "saved_PDF:")
tell application "Mail" to set theMessages to selection -- this line is added
tell application "Mail"
	repeat with This_Message in theMessages
		repeat with oneAttachment in mail attachments of This_Message
			set {name:theName, MIME type:mimeType} to oneAttachment
			if mimeType is "application/pdf" or theName ends with ".pdf" then
				save ma in (destinationFolder as Unicode text) & theName
			end if
		end repeat
	end repeat
end tell
--end perform mail action with messages
-- end using terms from

select one or multiple messages an run the script

okay, now it says the variable “ma” is not defined. That’s near the middle of the script. I ran it as you suggested.

Sorry, my fault. I changed my short variable names to more meaningful ones and forgot one :wink:

Replace ma with oneAttachment

Stefan, It WORKS!

sorry, I should have been able to catch that if I knew better. Thanks so much for your help. Now I am going to build a million of these things and get better organized! Thanks bud, I really appreciate it.

-i