Last active
December 8, 2023 22:27
-
-
Save logic2design/145ab428fbc087477d3c306d0d4bd2e8 to your computer and use it in GitHub Desktop.
This workflow will move nominated Mail messages to a Mailbox, save attachments to a Finder Folder and cull Mail Messages older than the nominated date from the Mailbox
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
############################################## | |
# Title: Move Messages, Save Attachments & delete old Message | |
############################################## | |
# Iain Dunn | |
# Logic2Design | |
# www.logic2design.com | |
# logic2design@icloud.com | |
# Last update: 11 July 2022 | |
# Version: 2 - added check for attachment being downloaded | |
# Contributors and sources | |
# https://macscripter.net/viewtopic.php?pid=209404#p209404 | |
# Orpy - https://talk.automators.fm/t/deleting-emails-from-mail-on-a-schedule/14037 | |
############################################## | |
# Configuration | |
############################################## | |
set parentFolder to (path to desktop folder) as string -- The location you want to use to create the Finder Folder to store the Attachments, use path to "docs" to use the Documents folder | |
set folderName to "Security Driveway" -- The Mailbox and Finder Folder you want to create | |
set mailAccount to "Fastmail" -- The Mail account you want to use to store the Messages | |
set mailSubject to "Driveway" -- The keyword you are looking for in the Message Subject | |
set attachmentExt to ".jpg" -- The attachment type you want to process | |
set deleteAge to 7 -- After this many days the messages will be deleted | |
############################################## | |
# Code | |
############################################## | |
--Create Mailbox if it doesnt exist | |
tell application "Mail" | |
tell account mailAccount | |
set folderName to folderName | |
if not (exists mailbox folderName) then | |
make new mailbox with properties {name:folderName} | |
end if | |
end tell | |
end tell | |
--Move Message & Save Attachments | |
set attachmentsFolder to parentFolder & folderName & ":" | |
my makeNewAttachmentsFolder(parentFolder, folderName) | |
tell application "Mail" | |
set messageList to every message in inbox whose subject contains (mailSubject as list) | |
try | |
ignoring case | |
repeat with theMessage in messageList | |
repeat with theAttachment in ((theMessage's mail attachments) whose (its name ends with attachmentExt)) | |
if (downloaded of theAttachment) then -- check if file is already downloaded | |
set originalName to name of theAttachment | |
my makeNewEmptyFile(attachmentsFolder, originalName) | |
save theAttachment in file (attachmentsFolder & originalName) | |
end if | |
end repeat | |
set mailbox of theMessage to mailbox folderName of account mailAccount | |
delay 3 | |
end repeat | |
end ignoring | |
end try | |
end tell | |
--Delete old Messages | |
tell application "Mail" | |
tell account mailAccount | |
set mailBin to mailbox "Trash" | |
set selectMessages to messages of mailbox folderName whose date received < ((current date) - (days * deleteAge)) | |
repeat with m in selectMessages | |
move m to mailBin | |
end repeat | |
end tell | |
end tell | |
############################################## | |
# Functions | |
############################################## | |
on makeNewAttachmentsFolder(parentFolder, folderName) | |
try | |
tell application "Finder" to make new folder at folder parentFolder with properties {name:folderName} | |
end try | |
end makeNewAttachmentsFolder | |
on makeNewEmptyFile(attachmentsFolder, newFileName) | |
try | |
tell application "Finder" to make new file at folder attachmentsFolder with properties {name:newFileName} | |
end try | |
end makeNewEmptyFile |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment