Mail.app script to save attachments in a new named folder...

I would like to save attachments from various emails in a new folder named after each email subject. ie If Subject is “[CDE] New Soccer Plays” and it has 5 jpgs as attachments, it would save all 5 of them in a folder named “[CDE] New Soccer Plays” inside a designated folder. I would also like to be able to select various emails and have them all go thru the script without error, like the Save Attachment command would, simply with the addition of it creating a new folder for every email.

Is it possible?

:P:P:P

I’m looking for the same type of script… but as you have no answear I think it is not the right place… let’s try once more!

I’m working with digital foto for a dental lab, so I receive e mail from dentists with 1-Pictures of the teeths to match & 2-a Pacient Name as Subject of e-mail. I have to keep pacients images organized during 3 to 6 month to achieve the work.

I don’t know script at all, but I’ll have to go into it to solve the problem I have…:

  1. Create a file according an e-mail adress.
  2. Name of the file = “Subject of the e-mail”
  3. Save images into file
  4. save e-mail as text format

Thank you for any help

Placing an image into a file is not easy. I suggest creating a folder with the patient’s name, putting the text of the email as a text document into the folder with the same name and a date perhaps, and including the attached image(s) as separate files in the folder with whatever names they had.

Thank you for making it more comprehensible, it is folder and not file, sorry.
Your suggestion about names and dates for the folders are rights.

Now tell us what mail application you use; there are quite a few of them. AppleScript is a means of communicating with applications, but we have to know where to aim the commands. The rest is easy.

I’m using Mail.

(I can use another one if it makes life easier, or better, for this purpose)

Very glad to see it looks possible to make it!

Hi,

try this

set destinationFolder to choose folder with prompt "Choose destination folder to save attachments in"

tell application "Mail"
	set theMessages to selection
	repeat with oneMessage in theMessages
		set {subject:theSubject, mail attachment:theAttachments, id:theID, content:theContent} to oneMessage
		if theSubject contains ":" then -- filter colons in subjects to avoid HFS path problems
			set {ASTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ":"}
			set theSubject to text items of theSubject
			set AppleScript's text item delimiters to "_"
			set theSubject to theSubject as Unicode text
			set AppleScript's text item delimiters to ASTID
		end if
		set folderName to (theID as Unicode text) & "_" & theSubject
		try
			tell application "Finder" to set attachmentFolder to make new folder at destinationFolder with properties {name:folderName}
		on error
			tell application "Finder" to set attachmentFolder to folder folderName
		end try
		repeat with oneAttachment in mail attachments of oneMessage
			save oneAttachment in (attachmentFolder as Unicode text) & (name of oneAttachment)
		end repeat
		write_to_disk of me from theContent into ((destinationFolder as Unicode text) & theSubject & ".txt")
	end repeat
end tell

on write_to_disk from theText into target
	try
		set ff to open for access file target with write permission
		write theText to ff
		close access ff
		return true
	on error
		try
			close access file target
		end try
		return false
	end try
end write_to_disk

Thanks a lot Adam, I tried it and works just fine.

Now I’ll work around it to learn a bit of script … and try to optimize a little bit more for my needs.
I’ll be away until middle of next week, but within 2 weeks I’ll probably place a copy of modified script if it works, to see your opinion.

Alex:D:D

1.- Little problem now using the script with Mail (without Mail rule it works fine)

In Mail pref. I created a simple rule: If mail object contain “foto”, then do the script.
I have to choose the destination and press enter.
If I press enter, Mail quit.
If I cancel, Mail stays open, so it should be someting at this moment. Choice box has a strange grafic arrangement also sometime.

2.- How can I modify this line in order to get the date instead of the ID into folder name?
set folderName to (theID as Unicode text) & “_” & theSubject

Can I pick something from this?
set item_info to info for theFile
set creation date of item_info to date “Saturday, January 1, 2000 12:00:00 AM”
apply catalog info item_info to theFile
end tell
info for theFile

Tried to find a few things seen on the net but without much sucess until now.

3.- The .txt isn’t saved into the folder.
The .txt has nothing in it.

Are these lines more usefull to format date of folder?.. (I try to understand script, and not just get the work done)

– format date for folder’s name (in yyyy-mm-dd format)
copy ((offset of (the month of (current date)) in ¬
"jan feb mar apr may jun jul aug sep oct nov dec ") + 3) / 4 ¬
as integer to mo
if mo < 10 then copy “0” & mo as text to mo
copy day of (current date) to da
if (day of (current date) < 10) then copy “0” & da as text to da
copy (year of (current date) as text) & “-” & mo & “-” & da to the_date

“hard-code” the path to the folder. Scripts triggered by a rule don’t like user interaction

I changed it in the script below

I don’t know why, but this can happen, when the text is not plain text

set destinationFolder to alias ((path to documents folder as Unicode text) & "attachments:") -- folder must exist!!
tell application "Mail"
	repeat with oneMessage in theMessages
		set {subject:theSubject, mail attachment:theAttachments, date received:theDate, content:theContent} to oneMessage
		if theSubject contains ":" then -- filter colons in subjects to avoid HFS path problems
			set {ASTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ":"}
			set theSubject to text items of theSubject
			set AppleScript's text item delimiters to "_"
			set theSubject to theSubject as Unicode text
			set AppleScript's text item delimiters to ASTID
		end if
		set folderName to (text 1 thru 10 of (theDate as «class isot» as string) as Unicode text) & "_" & theSubject
		try
			tell application "Finder" to set attachmentFolder to make new folder at destinationFolder with properties {name:folderName}
		on error
			tell application "Finder" to set attachmentFolder to folder folderName
		end try
		repeat with oneAttachment in mail attachments of oneMessage
			save oneAttachment in (attachmentFolder as Unicode text) & (name of oneAttachment)
		end repeat
		write_to_disk of me from (theContent as string) into ((attachmentFolder as Unicode text) & theSubject & ".txt")
	end repeat
end tell

on write_to_disk from theText into target
	try
		set ff to open for access file target with write permission
		write theText to ff
		close access ff
		return true
	on error
		try
			close access file target
		end try
		return false
	end try
end write_to_disk

1.- Script is not working without this second line “set theMessage…”, right?

tell application “Mail”
set theMessages to selection

2.- I’ve added that line to new script, & it works fine, but save the selected e-mail, (the second, the first or whatever) but never the incoming e-mails. How can I change the “set theMessages…” to incoming e-mails instead to selection?

set theMessages to selection

Note: still using the same rule in Mail.

(By the way, thank you for the help Stefank)

No, for a rule omit the selection line and wrap the script with (of course not including the handler)

using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		tell application "Mail"
			repeat with oneMessage in theMessages
			.
			end repeat
		end tell
	end perform mail action with messages
end using terms from