Below is a grown-up version of the script I posted before. It uses subroutines in place of the repetitive code I had before, and it permits specifying just the initial substrings of display names that you know signify junk. Thus, spammers who routinely modify their display names are more likely to continue to be caught by the script despite that tactic. Details that will allow you to adapt the script for your own use are specified in the comments. When I run this script now, it pops up statistics that indicate that there are no bad messages in the inboxes of my accounts, but lots of them in my junk mailbox. This tells me that the spam filter that populates the junk box is doing a good job, and that I can save effort checking the junk box for mail that was wrongly classified as spam by running this script. As I encounter increased numbers of spam messages with the same display name, I edit the script to add it to the list.
Here's the new version (I have replaced my account names with ***** and simplified the list displayNames):
(*
This script will look in the mailboxes specified in the
global variable sourceFolders, examining every message
or every unread message, as you specify. For each message
it will extract the display name of the "From:" header, if
there is one. For instance, if the message is from
"MUST-HAVE ITEM"
the display name is
MUST-HAVE ITEM
The script will compare this name to each item in the
global variable displayNames. If the display name begins with
any one of those items, ignoring case, the message will be moved
to the folder specified in the first line of code after
tell Application "Mail" below. For example, if one of the
items in displayNames is "Skin Tag", then messages
with display names such as "Skin Tag Removal", "Skin tags removed",
and "Skin tags gone forever" will be moved.
Note that it will probably take considerably longer to inspect
all messages in a mailbox, rather than just the unread ones.
At the end of the inspections, the script will pop up statistics,
which will look something like this:
Statistics:
transferred 19 messages from mailbox INBOX/spam of account *****
transferred 0 messages from mailbox INBOX of account *****
transferred 2 messages from mailbox INBOX of account *****
Total transferred: 21
*)
global displayNames, sourceFolders, targetFolder
set displayNames to {¬
"Body Therm", ¬
"KetoSlim", ¬
"Skin Tag"}
(*
Each item in the list sourceFolders is itself a list of three items:
1. The name of the mailbox
2. The name of the account (use "" if none)
3. The boolean true if you want to inspect every message
in the mailbox, false if you want to inspect only
unread messages
To get Mail's internal name for the junk box in one of your accounts
(in case that's where you want to look for the messages), open Mail
Preferences, choose the Accounts tab, select the account, and look on
the Mailbox Behaviors tab; the name is not necessarily "Junk".
*)
set sourceFolders to ¬
{¬
{"INBOX/spam", "*****", true}, ¬
{"INBOX", "*****", false}, ¬
{"INBOX", "*****", false}}
tell application "Mail"
set targetFolder to mailbox "@WorstSpam"
set transferTotal to 0
set reportString to "Statistics:"
repeat with sourceInfo in sourceFolders
set transferCount to my handleFolder(sourceInfo)
set reportString to reportString & "
" & "transferred " & transferCount ¬
& " messages from mailbox " & (item 1 of sourceInfo)
if (item 2 of sourceInfo) is not "" then
set reportString to reportString & " of account " & (item 2 of sourceInfo)
end if
set transferTotal to transferTotal + transferCount
end repeat
set reportString to reportString & "
Total transferred: " & transferTotal
display dialog reportString
end tell
on handleFolder(_sourceInfo)
tell application "Mail"
set folderName to item 1 of _sourceInfo
set accountName to item 2 of _sourceInfo
set readStatus to item 3 of _sourceInfo
set transferCount to 0
if accountName is "" then
set sourceFolder to mailbox folderName
else
set sourceFolder to mailbox folderName of account accountName
end if
if readStatus then
set everyMessage to every message of sourceFolder
else
set everyMessage to every message of sourceFolder where read status is false
end if
repeat with mssg in everyMessage
set transferCount to transferCount + (my handleMessage(mssg))
end repeat
return transferCount
end tell
end handleFolder
on handleMessage(_message)
tell application "Mail"
set theName to extract name from sender of _message
repeat with stem in displayNames
set strLength to length of stem
if length of theName ≥ strLength then
set trimmedName to characters 1 thru strLength of theName
if trimmedName as string = stem as string then
set mailbox of _message to targetFolder
return 1
end if
end if
end repeat
return 0
end tell
end handleMessage
[Email Edited by Moderator]