Mail attachments from Mail automate download to folder

still does not apply the rule automatically.
i am using your script verbatim and this rule setting:

any ideas would be appreciated.

i am running mojave, btw

A few ideas for things to consider/double check.

  1. Any chance you are reading the mails on an iOS device before the Mac Mail app loads them?
  2. Any chance you have a previously executing rule that stops this one from running? There’s notoriously a “News from Apple” one that as a final step stops subsequent rules.
  3. Are rules set as inactive in your Mail preferences and this rule set as active?

Change the line:

set desktopPath to (path to desktop) as string

to:

set desktopPath to POSIX file “[PUT THE PATH YOU WANT HERE IN QUOTATION MARKS, E.G.,” /Users/user_name/Documents/folder_name_you_want/" as string

You might want to rename the variables, but I did it this way just so you could identify the operative changes more easily.

1 Like

Still no luck. Running macOS Mojave version 10.14.3. Ran the rule part of the mail and had it just change the background color of the mail and that works fine. I cannot get the script to run and actually download an attachment. Here is the AppleScript code I’m using.

use AppleScript version "2.4"
use scripting additions

using terms from application "Mail"
 	on perform mail action with messages messageList in mailboxes mbox for rule aRule
	
		set destinationPath to POSIX file "/Users/user_name/Action" as string
		
		tell application "Mail"
			set selectedMessages to the selection
			repeat with aMessage in selectedMessages
    			repeat with anAttachment in mail attachments of aMessage
					set attachmentName to name of anAttachment
					save anAttachment in file (destinationPath & attachmentName)
				
				end repeat
			end repeat
		end tell
	
	end perform mail action with messages
end using terms from

It has to be something with the file path, as the original script going to the desktop works fine.

1 Like

It is.

You need to add a forward slash after the folder Action.
Should be set destinationPath to POSIX file “/Users/user_name/Action/”

I believe what is happening is this: Let’s assume your attachment is MyFile.txt.

  • Your attachment is being saved here: /Users/user_name/
  • and it’s being named ActionMyFile.txt.

Test it out and let us know the result.

Thank you everyone for your help. @iPersuade. I was missing the / as you advised, but there was one more part missing. Needed () around the POSIX file portion. That was the last piece to make it work.

set destinationPath to (POSIX file "/Users/user_name/Action/") as string

And just so my fellow automators do not have to peck around. Here is the script tested for one copy and paste.

-- Tested on macOS 10.14.4

use scripting additions

using terms from application "Mail"
	on perform mail action with messages messageList in mailboxes mbox for rule aRule
	
		set destinationPath to (POSIX file "/Users/user_name/your_folder/") as string
	
		tell application "Mail"
    		set selectedMessages to the selection
    		repeat with aMessage in selectedMessages
    			repeat with anAttachment in mail attachments of aMessage
    				set attachmentName to name of anAttachment
    				save anAttachment in file (destinationPath & attachmentName)
				
				end repeat
			end repeat
		end tell
	
	end perform mail action with messages
end using terms from

Excellent. Curious, though. You shouldn’t need parens around any of that, but if it works…don’t change it! Happy scripting. :slight_smile:

What you’re doing in this line is changing what starts out as a string (the part in quotes) into a file reference (by putting POSIX file in front of it) and then back into a string that uses HFS rather than Unix path format (by putting it in parentheses and then appending as string). You could simplify by changing it to:

set destinationPath to "drive_name:Users:user_name:Action:"

(Not sure you even need the final colon in this case.)

I’m struggling guys…I’ve tried the code as you guys have suggested, but no joy. No errors, but no files are saving. here is my code:

use scripting additions

using terms from application “Mail”

on perform mail action with messages

set destinationPath to “/Users/brock/Downloads/Attachments_For_Todoist/”

tell application “Mail”

set selectedMessages to the selection

repeat with aMessage in selectedMessages

repeat with anAttachment in mail attachments of aMessage

set attachmentName to name of anAttachment

save anAttachment in file (destinationPath & attachmentName)

end repeat

end repeat

end tell

end perform mail action with messages

end using terms from

Context: running a Mail rule on emails with the subject “invoice”. Wanting to save the attachments to a folder as listed in the code that is watched by Hazel

Tried this too:

use scripting additions

using terms from application “Mail”

on perform mail action with messages messageList in mailboxes mbox for rule Invoices

set destinationPath to “Macintosh HD:Users:brock:Downloads:Attachments_For_Todoist:” as string

tell application “Mail”

set selectedMessages to the selection

repeat with aMessage in selectedMessages

repeat with anAttachment in mail attachments of aMessage

set attachmentName to name of anAttachment

save anAttachment in file (destinationPath & attachmentName)

end repeat

end repeat

end tell

end perform mail action with messages

end using terms from

Please format your code with three ` above and below instead of as rich text.

I’ve been doing exactly the same thing for some time. Here’s my Mail rule:

And the script:

using terms from application "Mail"
	on perform mail action with messages theMessages
		set theOutputFolder to "/Users/Darrell/Downloads/" as rich text
		tell application "Mail"
			repeat with theMessage in theMessages
				if theMessage's mail attachments is not {} then
					repeat with theAttachment in theMessage's mail attachments
						set theFileName to theOutputFolder & theAttachment's name
						try
							save theAttachment in file theFileName
						on error errnum
						end try
					end repeat
				end if
			end repeat
		end tell
	end perform mail action with messages
end using terms from

It looks like I’ve repeated info from earlier in the thread. Sorry about that! I replied too quickly without looking back.

:upside_down_face:

@Ozziemaldonaldo, try the below.

-- Tested on macOS 10.14.4

use scripting additions

using terms from application "Mail"
    on perform mail action with messages messageList in mailboxes mbox for rule aRule

        set destinationPath to (POSIX file "/Users/brock/Downloads/Attachments_For_Todoist/") as string

        tell application "Mail"
            set selectedMessages to the selection
            repeat with aMessage in selectedMessages
                repeat with anAttachment in mail attachments of aMessage
                    set attachmentName to name of anAttachment
                    save anAttachment in file (destinationPath & attachmentName)
			
                end repeat
            end repeat
        end tell

    end perform mail action with messages
end using terms from 

That should work for what you want. Your mail rule will look something like the below image. You should be able to test the rule by right clicking on an email that meets the criteria and say apply rules. I was having an issue where the rule by itself worked but it wasn’t catching the emails as they came in. I had to add the Every Message part of the rule.

Thanks Creator_v2. It works, but not automatically…I need to right click “apply mail rules” for the script to run successfully…any ideas? I mapped the rule out as you did in your screenshot, including the “every message” condition

I am getting the same result i.e. does not function unless the rule is being applied.

In my instance I am setting my rule to recognize that the email has a PDF attachment and then to perform the following script (which is basically a variation of the current script in question but I am exporting to a folder on a network)

using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		set attachmentsFolder to "xxxx:yyyy:" as rich text
		tell application "Mail"
			set selectedMessages to theMessages
			try
				repeat with theMessage in selectedMessages
					repeat with theAttachment in theMessage's mail attachments
						set originalName to name of theAttachment
						set savePath to attachmentsFolder & originalName
						save theAttachment in file (savePath) with replacing
					end repeat
				end repeat
			end try
		end tell
	end perform mail action with messages
end using terms from

RR

Hi all,

Struggling to get this Apple script to work in Mail and wondering if anyone can help? Have changed the path only from script (above) that seems to be working for people on Mojave. My rule runs (it flags the message) but is not saving the attachment. Any assistance most appreciated.

    use scripting additions

    using terms from application "Mail"
    	on perform mail action with messages messageList in mailboxes mbox for rule aRule
    		
    		set destinationPath to (POSIX file "/Users/rorymiller/Downloads/") as string
    		
    		tell application "Mail"
    			set selectedMessages to the selection
    			repeat with aMessage in selectedMessages
    				repeat with anAttachment in mail attachments of aMessage
    					set attachmentName to name of anAttachment
    					save anAttachment in file (destinationPath & attachmentName)
    					
    				end repeat
    			end repeat
    		end tell
    		
    	end perform mail action with messages
    end using terms from