Applescript to save PDF to a folder

Joined
May 28, 2023
Messages
5
Reaction score
2
Hi All.

First, I would like to thank profusely Matt Swain who wrote the script in 2011. I am trying to get this script to work under Ventura 13.3.1. The script appears in the link below:

http://www.macosxtips.co.uk/index_files/automatically-save-mail-attachments-using-applescript.php

I have the script as this:

AppleScript:
on perform_mail_action(ruleData)
   
    -- From http://www.macosxtips.co.uk/index_files/automatically-save-mail-attachments-using-applescript.php
   
    -- The folder to save the attachments in (must already exist)
    set attachmentsFolder to (system attribute "HOME") & "/Mail Attachments/"
   
    -- Save in a sub-folder based on the name of the rule in Mail
    set subFolder to name of |Rule| of ruleData as text
    tell application "Finder"
        if not (exists folder subFolder of folder attachmentsFolder) then
            make new folder at attachmentsFolder with properties {name:subFolder}
        end if
    end tell
   
    -- Get incoming messages that match the rule
    tell application "Mail"
        set selectedMessages to |SelectedMessages| of ruleData
        repeat with theMessage in selectedMessages
           
            -- Get the date the message was sent
            set {year:y, month:m, day:d, hours:h, minutes:min} to theMessage's date sent
            set timeStamp to ("" & y & "-" & my pad(m as integer) & "-" & my pad(d) & "-" & my pad(h) & "-" & my pad(min))
           
            -- Save the attachment
            repeat with theAttachment in theMessage's mail attachments
                set originalName to name of theAttachment
                set savePath to attachmentsFolder & ":" & subFolder & ":" & timeStamp & " " & originalName
                try
                    save theAttachment in savePath
                end try
            end repeat
        end repeat
    end tell
   
end perform_mail_action

-- Adds leading zeros to date components
on pad(n)
    return text -2 thru -1 of ("00" & n)
end pad

My rule for running this script is as shown in the screenshot attached. The example.com address is a dummy address (Duh!)

No matter what I do, I cannot get this script to work. It doesn't work when the message first arrives in my inbox nor does it work if I select the message and select "Apply Rules". I have checked the spelling of the default saving folder name agains the name in the script. It is all correct. When I compile the script, there are NO errors.

Can someone PLEASE help me to get this to work. It would be IMMENSELY appreciated.
 

Attachments

  • Screenshot 2023-05-28 at 16.13.08.pdf
    65.3 KB · Views: 9
Joined
May 15, 2023
Messages
69
Reaction score
8
It's a bit of a late reply but here are some tips I can offer:
  1. Verify the script is stored in the right location: '~/Library/Application Scripts/com.apple.mail/'.
  2. Make sure your rule in Mail.app points to this script correctly.
  3. Check permissions - Mail.app should have Full Disk Access in System Preferences > Security & Privacy > Privacy.
  4. Finally, in Mail.app, under Preferences > General, check if 'Manage Plug-ins...' is enabled.
If it still doesn't work, we might need to debug this script.
 
Joined
May 28, 2023
Messages
5
Reaction score
2
Thanks for this! I did everything in points 1 to 3 and discovered that Mail did NOT have Full Disk Access so I fixed that. Thank you telling me about it.

I couldn't do point 4 because I couldn't find it. Is it part of Ventura 13.4?

Thanks again for your help!
 
Joined
May 15, 2023
Messages
69
Reaction score
8
I don't think it's specific to Ventura 13.4.

You should be able to see this option in Mail > Preferences > General. However, sometimes this option might be hidden due to some system configurations.

Here's a potential fix:

  1. Quit the Apple Mail application.
  2. Open Terminal from the /Applications/Utilities folder.
  3. Paste in the command 'defaults delete com.apple.mail EnableBundles' and press Return.
  4. Open Mail and quit it again.
  5. Back in Terminal, paste 'defaults write com.apple.mail EnableBundles -bool YES' and press Return.
  6. Open Mail again and check if the option is available under Mail > Preferences > General > Manage Plug-ins…
If this doesn't work, you might need to delete the DataVaults folder manually. To do this you need to temporarily turn off System Integrity Protection (SIP), delete the folder, and then turn SIP back on. I got a lot of useful info from this Apple discussion. Check it out, might be helpful.
 
Joined
May 28, 2023
Messages
5
Reaction score
2
Note 3 does not work. It says domain com.apple.mail does not exist. I went to the link you had. It is for Catalina. I am running Ventura 13.3.
 
Joined
May 15, 2023
Messages
69
Reaction score
8
Alright then. I think the 'domain does not exist' error might indicate that the specific preference you're trying to change doesn't exist or hasn't been created yet. This might be because the Mail app hasn't initiated this setting yet, or it could be specific to the macOS version you're running.
 
Joined
May 28, 2023
Messages
5
Reaction score
2
THANKS for all your help. Sometimes a script that was working at one stage, all of a sudden doesn't. I have plenty of time up my sleeve as I am retired and I think I will create a fresh account on my machine and see how I go. Thanks again.

If not, I might have to hire someone to write me a script. Am I allowed do pay someone on this forum or do I have to go to some other site.
 
Joined
May 28, 2023
Messages
5
Reaction score
2
True. However, I thought I would give ChatGPT a go. Mind you, my knowledge of AppleScript is limited to opening windows and closing them. Something I learned from a basic beginner’s course in AppleScript.

BRILLIANT!

After a few failed attempts, everything works great!

A word of warning to others. Apple Mail rules are not very reliable. I have ascertained this with Apple support after they have been helping me with a simple rule about forwarding messages. The support person was able to replicate the failure of this rule to be consistent on her machine, thus saving me the agony of reinstalling my system.

Using ChatGPT, I must have gone through 30 different different scrips that were given written for me by chatGPT. None of them worked. Why? For some reason, the scripts when applied in a rule did everything I wanted—get the name of the attachment, apply my preferred file naming convention—but they could not save attachments to my home folder, nor anywhere else on my external drives.

I solved the problem by asking ChatGPT to change one of the scrips to a script that ran in the Script editor, while a message was selected in Apple mail. Voila!

Not only was I able to save the attachments to my home folder, but anywhere else on any drive that is attached to my Mac Mini. I saved the script as a Service and applied a keyboard shortcut to it.

Now, when I receive an email message that contains a PDF I would like to keep, I simply select the message and press the keyboard shortcut. The attachment is saved to a particular folder that is monitored by Hazel, which then applies rules to help me file the attachments. Sure, not as automatic as a rule, but it works!

Another request to ChatGPT and three minutes later I had a new script to handle multiple email messages being selected and having their PDFs saved.

Thanks to all of you who helped me.

And thanks to ChatGPT.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top