Applescript for Saving .TXT Files from Mac Mail

itsjustkatie

macrumors newbie
Original poster
Jun 20, 2008
16
0
Hi everyone,

I've seen a similar request posted a couple times, but have had no luck on those threads finding the solution to my problem. Any help is appreciated!

I would like to write an AppleScript that would save an attachment (a .txt file) from Mail to my desktop. (Then in turn, I will make a rule in Mail that will run this script when certain conditions are met in the email).

I'm sure it's probably very simple, but I have been playing around with the script editor for a few hours now and still can't figure it out. Needless to say, I am new to scripting.

Please let me know if any other information is needed and thanks again for your wisdom!

-katie
 

mysterytramp

macrumors 65816
Jul 17, 2008
1,335
3
Maryland
There's a script at the end of this thread that might answer some basic questions on working with Mail and attachments. Scripting Mail can be a major pain, so don't be surprised if your project comes with a dose of frustration.

mt
 
Comment

bilboa

macrumors regular
Jan 16, 2008
212
0
Here's a sample bit of Applescript that will save all text attachments of the currently selected message in /tmp.

Code:
tell application "Mail"
	set msg to first item of (get selection)
	set theAttachments to mail attachments of msg whose MIME type is "text/plain"
	repeat with theAttachment in theAttachments
		set theName to name of theAttachment
		save theAttachment in ("/tmp/" & theName)
	end repeat
end tell
 
Comment

itsjustkatie

macrumors newbie
Original poster
Jun 20, 2008
16
0
Definitely feeling the frustration, MT! Hopefully it will all be worth it and make my life easier once it gets worked out. I tried implementing the script in the thread that you linked to and it keeps crashing Mail. I then tried changing the file path so it would go to my desktop and it doesn't crash it anymore, but it also does nothing :(

Bilboa- thanks for that script, that is exactly what I'm needing, but I don't think it's doing anything either...unless I just can't find the "tmp" folder!

I really appreciate all of your help so far and would love it if you could offer up anything else that may help.
 
Comment

itsjustkatie

macrumors newbie
Original poster
Jun 20, 2008
16
0
Stop the presses!! Victory! :) bilboa, using the script you posted, I just figured out where it was saving the attachments and once I understood that, I also understood how to write where I want something saved. It just "clicked" I guess!

My only small "problem" now is that when I moved the saved attachments to make it happen again, the script mined my entire email inbox and saved out every attachment I had received.

Here is what I used:

set theFolder to "Macintosh HD:Users:katie:Desktop:" as string

tell application "Mail"
set frontViewer to message viewer 1
set theMsgs to messages of frontViewer
repeat with msg in theMsgs
if the mail attachment of msg ≠ {} then
repeat with n from 1 to (count of mail attachments of msg)
set theName to name of (mail attachment n of msg)
set fullPath to theFolder & theName
save (mail attachment n of msg) in file fullPath
end repeat
end if
end repeat

end tell

As for the rule I am using in Mail, it says that if any email has an attachment ending in ".txt" it will run this script.

I'm trying some variations of removing the "repeat" commands since I assume that may be what is causing it to mine everything? Or could it be the rule in Mail? If you can, let me know if I'm heading in the right direction! Thank you again.
 
Comment

bilboa

macrumors regular
Jan 16, 2008
212
0
Your outer loop is looping over all messages in the message viewer, which is why it's "mining everything".
 
Comment

itsjustkatie

macrumors newbie
Original poster
Jun 20, 2008
16
0
Thanks much! I found that this one also works:

tell application "Mail"
set msg to first item of (get selection)
set theAttachments to mail attachments of msg whose MIME type is "text/plain"
repeat with theAttachment in theAttachments
set theName to name of theAttachment
save theAttachment in ("Macintosh HD:Users:katie:Desktop:" & theName)
end repeat
end tell
 
Comment

Similar threads

Register on MacRumors! This sidebar will go away, and you'll see fewer ads.