I have an automation where, when I receive a receipt for my online grocery order, Mail filters the email into a particular folder, and with a combination of Automator and Hazel the pdf receipt is removed from the email and added to a folder where I keep electronic receipts for processing later.
The automation is incomplete because I cannot work out how to then delete the email that contained the receipt pdf without manually doing it. I run this automation on a schedule each Friday around 5pm.
Iāve been looking for a way to do something similar: I get a lot of emails to one account from merchants, most of which a rent receipts but marketing offers of some kind.
I usually unsubscribe, but some I want to keep around, just in case. But they all have limited lives (3-5 days usually). So they go into a smart folder names for the appropriate length⦠and there they sit until I clear out the folders, because I can figure out a way to automatically delete an email X days after receipt.
It seems like such a basic need, but I donāt think even Sanebox does that (though I could be wrong ā I prefer not to use services that require me to send email to someone elseās server).
You could use an AppleScript to move the messages in a specific mailbox to the deleted messages mailbox.
The script can be modified for tf2 so that only messages older than n days are selected using ādate received < tā, where t is current date - (days * n).
You can delete multiple messages (including selecting by date received) like this:
tell application "Mail"
set targetMailbox to mailbox "Receipts"
set mailBin to mailbox "Deleted Messages"
set selectMessages to messages of targetMailbox whose date received < ((current date) - (days * 365))
repeat with m in selectMessages
move m to mailBin
end repeat
end tell
As your automation will have already selected a single message, youād just a single line of AppleScript to move it to your Deleted Messages mailbox.
Sadly, I am not a programmer and although I can edit very simple code, I wasnāt sure what to do with the code you created!
Thanks so much though for responding to me. I am trying to create more āend to endā automations and this posting was about that. I had managed to create an automation that got me 90% there but couldnāt do the final part.
I couldnāt wait two days to see if a script works
If I were you, Iād create a test mailbox and drag a test message in to it so you can test your script.
If you run into problems you can always select the test message and run a single line, like:
tell application "Mail" to get selection
--> {message id 252052 of mailbox "Suppliers/AcmeCo" of application "Mail"}
So you can make sure that the script is targeting the correct messages/mailbox/account etc.
Adding more test messages with a range of dates to the test mailbox will then allow you to test if selectMessages date selection is working properly.
Just comment out the repeat part of the script while youāre testing so that the messages stay where they are.
Once youāre satisfied that the list of messages returned by the script is exactly as you want you can then let the repeat run with confidence that it isnāt going to do anything unexpected.
I updated the code to give a description and purpose of the configurations you need to set. The rest of the code you donāt need to touch.
To test the delete function just grab some Junk mail older then the number of days you set in the configuration and place them into the folder you are moving the mail to, then run the script.
I am running the Ventura Beta and I am finding the mail rules are not always being triggered. An option I will try is to create a repeating Calendar to trigger the Applescript.
If you are still stuck drop me a line and maybe we can jump on a Zoom and I can help you out.
Thank you very much for this script. I look forward to trying it out. One thing: Once I have detached an attachment from an eMail, I have no further need for that message. You have your purge set to 7 days, I am unsure how to set the purge to, say, immediately following detachment or less than 1 minute old. What syntax would I use to effect that change to line 5 of your script?