You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Using applescript to save email attachments

Hi,


So I am trying to write some kind of applescript to run once a week on a Friday morning at 9am to check my email inbox from Monday of the same week until the Friday for any emails with the subject "Weekly Media" which would download the email attachment files and automatically place them in a folder I have in my downloads folder called "WeeklyMedia".


Could anyone help please?


Dave

MacBook Air 13″, macOS 12.3

Posted on Mar 22, 2022 1:26 AM

Reply
Question marked as Top-ranking reply

Posted on Mar 23, 2022 9:08 AM

> I'm not sure exactly where to put the path to the folder.


The answer to this question is in the line:


set attachmentsFolder to ((path to home folder as text) & "Weekly Media") as text


This looks for a folder called 'Weekly Media' in your home directory (e.g. /Users/<username>/Weekly Media)


If you want it somewhere else, amend this line as necessary. For example, if the 'Weekly Media' folder is on your desktop, you could say:


set attachmentsFolder to ((path to desktop folder as text) & "Weekly Media") as text


if you want it anywhere else, check out the path to command to see the other dynamic locations that AppleScript knows about (home folder, desktop folder, documents folder, etc.), or you can specify a full path ( set attachmentsFolder to "Macintosh HD: Users:username:some:other:folder:Weekly Media". Note that full paths are not recommended since they make the script less portable (e.g. may fail if run on a different machine, or by a different user - always use the dynamic path to whenever possible).


Similar questions

10 replies
Question marked as Top-ranking reply

Mar 23, 2022 9:08 AM in response to DMLaMiranda1990

> I'm not sure exactly where to put the path to the folder.


The answer to this question is in the line:


set attachmentsFolder to ((path to home folder as text) & "Weekly Media") as text


This looks for a folder called 'Weekly Media' in your home directory (e.g. /Users/<username>/Weekly Media)


If you want it somewhere else, amend this line as necessary. For example, if the 'Weekly Media' folder is on your desktop, you could say:


set attachmentsFolder to ((path to desktop folder as text) & "Weekly Media") as text


if you want it anywhere else, check out the path to command to see the other dynamic locations that AppleScript knows about (home folder, desktop folder, documents folder, etc.), or you can specify a full path ( set attachmentsFolder to "Macintosh HD: Users:username:some:other:folder:Weekly Media". Note that full paths are not recommended since they make the script less portable (e.g. may fail if run on a different machine, or by a different user - always use the dynamic path to whenever possible).


Mar 22, 2022 9:44 AM in response to DMLaMiranda1990

Is there a reason why you want to run this weekly, vs. have it automatically copy the attachments as they come in during the week?


The reason I ask is that it's trivial to have Mail.app do this progressively - it has built-in hooks to process messages as they come in, and it can save the attachments immediately. Conversely, to do it weekly you need a script scheduler to kick it off (unless you do it manually), and the script has to check all the last week's mail to find appropriate messages. Not impossible to do, for sure, but a lot easier to do on the fly.


Let me know which way you want and I'm sure we can craft something suitable.

Mar 22, 2022 3:21 PM in response to DMLaMiranda1990

So this is a two-part solution.


Here's an AppleScript you can paste into Script Editor and save it in ~/Library/Application Scripts/com.apple.mail/


Then, in Mail.app -> Preferences, add a new Rule.


Set the trigger to be Subject is 'Weekly Media' - this catches the messages that need to be processed.


For the actions, choose 'Run AppleScript' action and choose your script from the pop-up menu.


use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

set attachmentsFolder to ((path to home folder as text) & "Weekly Media") as text

using terms from application "Mail"
	on perform mail action with messages these_messages for rule this_rule
		tell application "Mail"
			repeat with each_message in these_messages
				repeat with theAttachment in each_message's mail attachments
					set originalName to name of theAttachment
					set savePath to attachmentsFolder & ":" & originalName
					try
						save theAttachment in savePath
					end try
				end repeat
			end repeat
		end tell
	end perform mail action with messages
end using terms from


Mar 28, 2022 11:18 AM in response to DMLaMiranda1990

There is no test within the script to check that the folder in question exists, and it doesn't try to create it if it's missing - basic error checks that are good to have, but I omitted in the interest of simplicity.


Off-hand, that's the most obvious cause I can think of why it might fail. Can you show your script? and confirm that the folder exists?

May 7, 2022 10:54 AM in response to Camelot

Hi! I am excited to get this to work, but no luck so far. I am on MacBook 2015 running OS Big Sur with the target folder ("Temp Folder") in the documents directory. The apple script file I created based on the above instructions is:


use AppleScript version "2.4" -- Yosemite (10.10) or later


use scripting additions




set attachmentsFolder to ((path to documents folder as text) & "Temp Folder") as text




using terms from application "Mail"


on perform mail action with messages these_messages for rule this_rule


tell application "Mail"


repeat with each_message in these_messages


repeat with theAttachment in each_message's mail attachments


set originalName to name of theAttachment


set savePath to attachmentsFolder & ":" & originalName


try


save theAttachment in savePath


end try


end repeat


end repeat


end tell


end perform mail action with messages


end using terms from

Jun 17, 2022 10:26 PM in response to DMLaMiranda1990

Hi, I've copied the script saved to library location, and jump into mail, setup the rule, etc, all good


I then go to apply the rule on an email to see if it will save the attachment, I also in the rules set up 'Flag' as a colour to see it working.


The flag works, but the attachment doesn't save the attached file to the folder 'Weekly Media'. As you can see by the script below I tried to set the exact path, and no joy here either??


Not sure what I'm missing here.

OS - Mac OS Monterey version 12.4



My script is:


use AppleScript version "2.4" -- Yosemite (10.10) or later

use scripting additions


set attachmentsFolder to ((path to home folder as text) & "Weekly Media") as text


(then replaced as 'set attachmentsFolder to "Macintosh HD: Users:myname:Weekly Media" as text) - Tried to remove the space between 'Macintosh HD: and Users', ie 'Macintosh HD:Users....' still no difference??


using terms from application "Mail"

on perform mail action with messages these_messages for rule this_rule

tell application "Mail"

repeat with each_message in these_messages

repeat with theAttachment in each_message's mail attachments

set originalName to name of theAttachment

set savePath to attachmentsFolder & ":" & originalName

try

save theAttachment in savePath

end try

end repeat

end repeat

end tell

end perform mail action with messages

end using terms from

Using applescript to save email attachments

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.