sricpt: mail attachment to downloads folder - adjustment for Catalina?

Hi there,

a long time ago I found an applescript for simply downloading attachments to the „downloads“ folder. I’m using it in Mail with some rules.
This worked fine since macOS High Sierra, I think.
Now I tried to reincarnate this applescript for macOS Calatina but it didn’t work.
As I’m a beginner in applescript I don’t know the specific changes to apply for Catalina - can someone please help me?

Here ist the old applescript:

using terms from application “Mail”
on perform mail action with messages theMessages for rule theRule
tell application “Mail”
repeat with theMessage in theMessages
set {mail attachment:theAttachments} to oneMessage
repeat with oneAttachment in mail attachments of oneMessage

				save oneAttachment in ("Macintosh HD:Users:cmayer:Downloads:") & (name of oneAttachment)
				
			end repeat
		end repeat
	end tell
end perform mail action with messages

end using terms from

I already found specific applescripts for this topic here in the forum and elswhere but they all had some own specific code (I didn’t get along with)…

Thanks for the help in advance!

This works on the Catalina as expected:


using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		set downloadsHFSpath to (path to downloads folder) as string
		tell application "Mail"
			repeat with oneMessage in theMessages
				repeat with oneAttachment in (mail attachments of oneMessage)
					save oneAttachment in (downloadsHFSpath & name of oneAttachment)
				end repeat
			end repeat
		end tell
	end perform mail action with messages
end using terms from

Sorry for this question but how do I write (syntax) “path to the downloads folder”?
I tried (“Macintosh HD:Users:cmayer:Downloads:”) and (“/Users/cmayer/Downloads/”).

Both don’t work - can you once again help me out?

Thanks!

The script founds path to your downloads folder itself. Do not change nothing in this syntax. Run this code only in the Script Editor to see, that I’m not lying.

First: thanks for your help!

But even running the original script doesnt help. Also running the script in the script editor (as you mentioned) doesn’t lead to any action - just nothing happens, no attachment es being copied.

Could it be a problem that I’m handling 4 email accounts with Apple Mail?

To happen something, A) you should create some rule, B) should happen new Event in the Mail.app, for example, you receive new mail & the mail confirms the condition:

  1. Save my script in the location ~Library/Applicaton Scripts/com.apple.mail/
  2. Open Mail.app. Go Mail–>Preerences…–>Rules
  3. Click Add Rule button. Opens window containing 2 things: 1st) the condition on which should be performed action 2nd) action itself should which be performed.
  4. For testing choose From + Contains + Some substring usually contained by mails you send. One of them should be your name, because you are who sends this mails.
  5. For the action select Run AppleScript, then choose my script.

That is. You created one rule. Again, to happen something for first time, create new message with attachment and send it to yourself. Wait until Mail.app will receive this message. This is EVENT, which conforms to condition, so triggers the rule. Check the Downloads folder. You should found in it the extracted attachments.

NOTE: We made the condition this way only for testing. There are many other conditions you can choose. Remember - the rule is triggered only when happens some new Event in the Mail.app, for example, you receive new mail & the mail confirms the condition.

…tried all this.
I already tried your advices before and did it again now but the script doesn’t want to work with my macOS Catalina.

The rule I created is working (to test this i told Mail in the rule to move the email to another email folder) but the script doesn’t copy the attachment to the Downloads folder.

Do you have any other ideas on the origin that the script doesn’t work for me?
(Do I have to allow applescript to work e.g. somewhere in the security panel or something like that?)

I sad you not to use the action Move the email to another email folder, but to use action Run AppleScript:slight_smile: Don’t forget to choose the script itself too. Change the same rule’s settings to not confuse the things further. Or, delete previous rule and create new.

I tried the rule with “move email” in addition to the applescript just to see mail is acting on the rule and also a “blank” rule as you suggested above :slight_smile: - no effect, no attachment stored in “Downloads”.

I did not offer anything like this.
You should carefully read again, what I suggested: 1) how create a rule, 2) how test the rule (by sending a real message with an attacment to yourself).

To understand what an Event, Condition and Action is:

  1. Event: “a new mail has arrived”
  2. Condition: “From contains myName” (from cmayer?)
  3. If yes, then perform the Action: “Run AppleScript” (move the attachment to the Downloads folder)

Yes, I did exactly what you suggested in these steps (only!) for the rule. No effect.

Then - as next step to see if Mail acts on the rule anyway - I added the “move email to another folder” additionally. That worked.

To see if Mail works with applescript anyway I just wrote a script that puts out a text and added it to the rule instead of the other script:

display dialog "AppleScript ausgeführt!"

That worked too.

So there must be something in the script to save attachments in the Downloads folder that doesn’t work with my macOS Catalina.
I just can’t figure out what’s the problem exactly.

Select messages with attachments then execute this simple script:

tell application "Mail"
	set theMessages to the selection
end tell
-- Here is the active part of the original script

set downloadsHFSpath to (path to downloads folder) as string

tell application "Mail"
	repeat with oneMessage in theMessages
		repeat with oneAttachment in (mail attachments of oneMessage)
			-- three added informative instructions
			set theName to (name of oneAttachment as string)
			set theSubject to subject of oneMessage
			tell current application to say (theName & " was attached to a message whose subject is " & theSubject)
			--
			save oneAttachment in (downloadsHFSpath & name of oneAttachment)
		end repeat
	end repeat
end tell

It’s supposed to save the file(s) attached to every selected messages and to tell you the name of every attachment + the subject of the message
This way you will know if it behaves well or were it fails.

You are supposed to retrieve the files in the Downloads folder.

I never tried to insert a tell instruction in a script triggered by a rule but you may try, it would not hurt. The worst behavior would be that it say nothing.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) samedi 9 mai 2020 21:37:19

OK, the script triggering works for you too, as I see now. Try now 3 things.

  1. Run only this code line in your script editor, and see if this is the location, which you consider as your downloads folder. That is, you search for the result in this location?
(path to downloads folder) as string
  1. Exists at least 1 attachment in your mails, which contains your name? No attachments in your mails, no copying
  2. Try the script of Yvan Koenig. To say us if it works as simple script.

path to has an as parameter which is preferable over the as coercion

(path to downloads folder as string)

@ KniazidisR:

Result on your questions:

  1. Result is “Macintosh HD:Users:cmayer:Downloads:” - this is my correct Downloads folder.

  2. there is no attachment in my mails with my name in it.

  3. The script from Yvan Koenig woerks in my Editor.

With all this I think the problem lies in choosing the email and performing the action (saving the attachment) on it. My problem is my scripting knowledge is far from being able to take the right steps…

@ StefanK:
If I try to put “(path to downloads folder as string)” instead of "(path to downloads folder) as string"
my actual scripting editor (script debugger 7 trial version) it works when I’m just using the single line.

But changing the line
set downloadsHFSpath to (path to downloads folder) as string” to
set downloadsHFSpath to (path to downloads folder as string)

will just give me an error like this in script debugger:
set downloadsHFSpath to (path to downloads folder rule type string)

What do you mean exactly?

So, what you want the script to extract, if there is no attachments? The rule we created selects the mails, which contains your account name (or, your real name, or you other name). They should contain in addition at least 1 attachment to extract. Other way, no attachments, no any result in the Downloads folder.

As I see, for now it’s hard for you to understand how the rules work in the Mail.app. And, you should know that “rule” scripts work only in the Mail.app, and only as rules, and not in the script editor, or as applet. Therefore, I suggest you use for now a regular script that finds all messages and extracts all attachments in the Downloads folder:


set downloadsHFSpath to path to downloads folder as string
set theMessages to {}

tell application "Mail"
	-- collect all messages
	repeat with anAccount in (accounts)
		repeat with aMailBox in (mailboxes of anAccount)
			set EM to every message of aMailBox
			if EM is not missing value then set theMessages to theMessages & EM
		end repeat
	end repeat
	-- extract attachments to Downloads folder
	repeat with oneMessage in theMessages
		try
			repeat with oneAttachment in (mail attachments of oneMessage)
				save oneAttachment in (downloadsHFSpath & name of oneAttachment)
			end repeat
		end try
	end repeat
end tell

That’s a terminology clash with the Mail dictionary, replace the line with

tell current application to set downloadsHFSpath to (path to downloads folder as string)

@cmayer

If I understand well, what you need is:

using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		tell me to set downloadsHFSpath to (path to downloads folder rule type string)
		tell application "Mail"
			repeat with oneMessage in theMessages
				repeat with oneAttachment in (mail attachments of oneMessage)
					save oneAttachment in (downloadsHFSpath & name of oneAttachment)
				end repeat
			end repeat
		end tell
	end perform mail action with messages
end using terms from

As KniazidisR wrote, such script can’t be tested in Apple’s Editor.
If I understand well you may use Script Debugger to debug such script.
Look in the Help, the chapter entitled : External Debugging
You must create a rule calling it when, what Mail receive, match some requirements.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) dimanche 10 mai 2020 19:29:54

This script will throw error on finding path to downloads folder. What proposed StefanK has zero benefit, but can create new problems when bounding tell me and tell current application with Mail rules. And needs testings.

As I sad, my 1st script in this topic works as expected and is tested on Catalina (and should work on older systems too). Unfortunately, the OP does not understand how to create an outgoing message in Mail.app and drag some file (attachment) into the contents, and then send it to himself.

So I discovered the following:

when I do as the natives here have told me (making specific rule with parameters & run script, then sending myself an email with an attachment) unfortunately nothing happens. :o

When I now go to mail, select the same email with an attachment and run the “apply rules” command (->email ->apply rule) it works and the attachment gets extracted to the Downloads folder.:smiley:

So what could be the difference here - the same rule doesn’t work by itself when an email ist arriving but it works when it’s triggered manually???