Sunday, July 5, 2020

#1 2020-05-08 02:25:53 pm

cmayer
Member
Registered: 2020-05-08
Posts: 8

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!


Filed under: mail, Attachment

Offline

 

#2 2020-05-08 11:50:35 pm

KniazidisR
Member
Registered: 2019-03-03
Posts: 1259

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

This works on the Catalina as expected:

Applescript:


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


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#3 2020-05-09 02:45:15 am

cmayer
Member
Registered: 2020-05-08
Posts: 8

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

KniazidisR wrote:



Applescript:



       set downloadsHFSpath to (path to downloads folder) as string
       


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!

Offline

 

#4 2020-05-09 02:56:46 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 1259

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

cmayer wrote:
KniazidisR wrote:



Applescript:



       set downloadsHFSpath to (path to downloads folder) as string
       


Sorry for this question but how do I write (syntax) "path to the downloads folder"?



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.

Last edited by KniazidisR (2020-05-09 03:08:36 am)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#5 2020-05-09 05:50:22 am

cmayer
Member
Registered: 2020-05-08
Posts: 8

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

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?

Offline

 

#6 2020-05-09 06:20:30 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 1259

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

cmayer wrote:

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.

Last edited by KniazidisR (2020-05-09 06:34:53 am)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#7 2020-05-09 07:28:23 am

cmayer
Member
Registered: 2020-05-08
Posts: 8

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

..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?)

Offline

 

#8 2020-05-09 08:01:46 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 1259

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

cmayer wrote:

to test this i told Mail in the rule to move the email to another email folder


I sad you not to use the action Move the email to another email folder, but to use action Run AppleScript... 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.

Last edited by KniazidisR (2020-05-09 08:14:30 am)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#9 2020-05-09 09:31:31 am

cmayer
Member
Registered: 2020-05-08
Posts: 8

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

KniazidisR wrote:
cmayer wrote:

to test this i told Mail in the rule to move the email to another email folder


I sad you not to use the action Move the email to another email folder, but to use action Run AppleScript... 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 smile - no effect, no attachment stored in "Downloads".

Offline

 

#10 2020-05-09 09:44:24 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 1259

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

cmayer wrote:

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 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)

Last edited by KniazidisR (2020-05-09 10:04:37 am)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#11 2020-05-09 12:08:54 pm

cmayer
Member
Registered: 2020-05-08
Posts: 8

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

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:

Applescript:

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.

Offline

 

#12 2020-05-09 01:48:25 pm

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4534

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

Select messages with attachments then execute this simple script:

Applescript:

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

Offline

 

#13 2020-05-09 10:15:48 pm

KniazidisR
Member
Registered: 2019-03-03
Posts: 1259

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

cmayer wrote:

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.



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?

Applescript:

(path to downloads folder) as string

2) Exists at least 1 attachment in your mails, which contains your name? No attachments in your mails, no copying
3) Try the script of Yvan Koenig. To say us if it works as simple script.

Last edited by KniazidisR (2020-05-09 10:22:06 pm)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#14 2020-05-09 10:47:11 pm

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11688
Website

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

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

Applescript:

(path to downloads folder as string)


regards

Stefan

Offline

 

#15 2020-05-10 08:54:03 am

cmayer
Member
Registered: 2020-05-08
Posts: 8

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

@ 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?

Offline

 

#16 2020-05-10 10:07:32 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 1259

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

cmayer wrote:

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


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:

Applescript:


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

Last edited by KniazidisR (2020-05-10 11:08:29 am)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#17 2020-05-10 11:03:45 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11688
Website

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

cmayer wrote:


@ 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?



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

Applescript:

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

Last edited by StefanK (2020-05-10 11:04:14 am)


regards

Stefan

Offline

 

#18 2020-05-10 11:30:00 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4534

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

@cmayer

If I understand well, what you need is:

Applescript:

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

Last edited by Yvan Koenig (2020-05-10 11:34:39 am)

Offline

 

#19 2020-05-10 01:28:59 pm

KniazidisR
Member
Registered: 2019-03-03
Posts: 1259

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

Yvan Koenig wrote:

@cmayer
If I understand well, what you need is:


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.

Last edited by KniazidisR (2020-05-10 01:49:21 pm)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#20 2020-05-11 02:00:42 pm

cmayer
Member
Registered: 2020-05-08
Posts: 8

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

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. yikes

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.:D

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???

Offline

 

#21 2020-05-12 11:02:48 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 1259

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

cmayer wrote:

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???


The fact that you do not understand how it works does not mean that it does not work... The Apply action in the application settings means the persistent state of this action in the future too.

In your case, you have created only one condition - the receipt of a mail from yourself. Since you already sent yourself a mail (for the test), and you don’t send anything to yourself anymore, naturally, nothing else comes from you.

Now, you have to create some other condition, for example, the receipt of a email from some other sender. Which sends you mails, of course. And wait until he sends you a mail with an attachments inside.

But, I am not a primary school teacher. There are Mail.app guides to learn mail rules. I am always happy to help in Mail.app's scripting problems that are not described in books.

Last edited by KniazidisR (2020-05-12 12:31:34 pm)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#22 2020-05-12 02:59:24 pm

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4534

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

I missed the fact that compilation had modifier the passed script.
I edited it, saved it in "SSD 1000:Users**********:Library:Application Scripts:com.apple.mail:" and tested it so I know that it works.

Applescript:

using terms from application "Mail"
   on perform mail action with messages theMessages for rule theRule
       set downloadsHFSpath to my setPath()
       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
on setPath()
   return (path to downloads folder as string)
end setPath

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mardi 12 mai 2020 22:59:17

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)