Applescript Moving Email to Trash, But Not Deleting from the Server - applescript

I wrote an AppleScript that moves selected emails to the Trash. The script works fine in that regard. The problem is that the emails remain on the server. Is there additional code I need to add to get it to do that? Here's the code:
using terms from application "Mail"
on perform mail action with messages these_messages for rule this_rule
tell application "Mail"
set the message_count to the count of these_messages
repeat with i from message_count to 1 by -1
set this_message to item i of these_messages
set this_content to (every character of content of this_message) as Unicode text
if "bowles" is not in this_content and "patton" is not in this_content then
set theAccount to account of mailbox of this_message
set mailbox of this_message to mailbox "Trash" of theAccount
end if
end repeat
end tell
end perform mail action with messages
end using terms from

It is hard to troubleshoot without seeing the entire script and the other rules that may be causing a problem. Try adding the Stop evaluating rules action to your rule :
using terms from application "Mail"
on perform mail action with messages these_messages for rule this_rule
tell application "Mail"
repeat with this_message in these_messages
set this_content to content of this_message
if "bowles" is not in this_content and "patton" is not in this_content then
set theAccount to account of mailbox of this_message
set junk mail status of this_message to false
set mailbox of this_message to mailbox "Trash" of theAccount
end if
end repeat
end tell
end perform mail action with messages
end using terms from

Sadly the answer appears to be to create a separate rule for email source. In the end, I just unsubscribed from the mailing lists as I hadn't seen any useful information from them in years.

Related

Applescript Mail archive Actions

So, I finally got tired of manually sorting and archiving email on my Mac to get around some archaic mailbox size limits. I'm not a fan of the way Apple handles mail archiving, so I dug into a few examples that others had written to sort mail and slapped a short script together to filter through my email and archive everything older than 90 days. For the most part, easy stuff.
It is a tad annoying that Mail doesn't see "On My Mac" as a local account, so you need to adjust the code to blindly reference mailbox names.. but.. it works.
I think I'm just having an evaluation problem. I can sort the "Inbox" and archive mail from there quite easily.
on archive_email(target_account, target_mailbox, destination_account, destination_mailbox, oldemaildate)
tell application "Mail"
#Identify messages that need to be moved. If unread messages shouldn't be moved, change the end of the variable definition to read "is less than oldsentdate and read status is true)
set _msgs_to_capture to (every message of mailbox target_mailbox of account target_account whose date received is less than oldemaildate)
repeat with eachMessage in _msgs_to_capture
set archiveMailbox to (mailbox (destination_mailbox as string))
move eachMessage to archiveMailbox
end repeat
end archive_email
This doesn't work for the "Sent" folder, however. My guess is that the evaluation "whose date received is less than" is improper for the "sent" folder. Sadly, "whose date sent is..." doesn't seem to work either. I'm struggling to find the right variable and evaluation here.
The line bellow is perfectly working, assuming target_account and oldmaildate have correct values. To be sure, I replaced target_mailbox directly by "Sent Messages" :
set _msgs_to_capture to every message of mailbox "Sent Messages" of target_account whose date sent is less than oldemaildate
Please check values/types of your variables.
Also, I suggest you set the line below before the repeat loop and not inside, to increase speed :
set archiveMailbox to (mailbox (destination_mailbox as string))
You can do away with the repeat loop, which is inefficient, and achieve the move with a single command:
on archive_email(target_account as text, target_mailbox as text, ¬
destination_account as text, destination_mailbox as text, ¬
oldemaildate as date)
tell application "Mail" to ¬
move (every message ¬
of mailbox target_mailbox ¬
of account target_account ¬
whose date sent ¬
is less than oldemaildate) ¬
to mailbox destination_mailbox
end archive_email
Use date sent for Sent items, which I can confirm works.
System info: AppleScript version: "2.7", system version: "10.13.4"

Last sent e-mail from Mail.app with Applescript

Is there any way to get the last sent e-mail from Mail?
It can only access the top level sent mailbox.
tell application "Mail"
set mabo to the sent mailbox
set selected to messages of mabo
return properties of item 1 of selected
end tell
The reason I need this is that I want to get information about the e-mail that is beeing written. My thought was to get the frontmost window.
tell application "Mail"
set the win to windows
set theMessage to item 1 of win
get name of theMessage
end tell
It is working for the name but I do not get the sender or receipient. Any ideas?
As said, you just have to loop through each account, and each time, compare the date of the email with previous email date from other account.
here is the script : (I also add a test in case the sent box of one account is empty !)
tell application "Mail"
set LastDate to date "samedi 1 janvier 2000 00:00:00"
set {myLastMail, myAccount, myDate} to {"", "", LastDate}
repeat with CAccount in every account
if (count of every message in mailbox "Sent Messages" of CAccount) > 0 then
set X to first message of mailbox "Sent Messages" of CAccount
set MDate to date sent of X
if MDate > LastDate then
set LastDate to MDate
set {myLastMail, myAccount, myDate} to {X, CAccount, LastDate}
end if
end if
end repeat
-- the last email from all your accounts is the variables Mylastemail, in account MyAccount and at the date MyDate
end tell
Unfortunately the messages in the sent mailbox are unsorted in the AppleScript array.
You can use this code, but it could take a while depending on the number of messages in the mailbox (ca. 30 sec for 1000 messages).
tell application "Mail"
set sentMessages to messages of sent mailbox
set lastestMessage to item 1 of sentMessages
set latestDate to date received of lastestMessage
repeat with aMessage in sentMessages
if date received of contents of aMessage comes after latestDate then
copy contents of aMessage to lastestMessage
set latestDate to date received of lastestMessage
end if
end repeat
tell lastestMessage
set theSender to sender
set theRecipient to 1st recipient
end tell
end tell
The code bellow uses to work for me in all my Mail versions up to 4.6 (Snow Leopard). To be tested on later versions. Strangely, "last message" gives the oldest message in the mailbox and the "first message" gives the last message sent or received ! if you have many accounts, just loop on accounts names :
tell application "Mail"
-- to get the last message sent
set X to first message of mailbox "Sent Messages" of account "your_account_name"
-- to get the oldest message received in receiving box
set X to last message of mailbox "INBOX" of account "your_account_name"
open X --if want to see it
end tell

Get e-mail address of selected mailboxes

I am trying to receive the mail addresse of the account that is active in mail.
So far I did the following
tell application "Mail"
set selected to selected mailboxes of message viewer 1
end tell
and
tell application "Mail"
email addresses of every account
end tell
But somehow i am not able do somethin like
tell application "Mail"
set selectedMailboxes to selection
set theAccount to account of selection
return theAccount
end tell
The Last applescript does not work. How can I retrieve the account of the mailbox in message vieview 1?
Try this
tell application "Mail"
try
set selectedMailbox to item 1 of (get selected mailboxes of message viewer 1)
set mailAddresses to email addresses of account of selectedMailbox
on error
display dialog "No Mailbox selected"
end try
end tell
As an account can have multiple mail addresses, the class of the variable mailAddresses is a list even there is only one address.
Get the address you want with an index number (1-based)
set myAddress to item 1 of mailAddresses
or
set myAddress to second item of mailAddresses

Applescript Purge Email Older than 2 Days

I am new to Applescript. I have one specific email account that exists solely for the purpose receiving error reports with an attached image. The mailbox can fill up quickly.
I'd like to be able to run a script that will delete mail older than two days, so I tried my hand at the following script.
I'd like to correct what I have written so I can learn from my mistakes rather that use a different method. Looking for some constructive criticism:
set daysToPreserve to 2
tell application "Mail"
activate
set mailboxList to mailbox "INBOX" of account “MyAccount"
repeat with theCurrentMailbox in mailboxList
set emailList to (every message of (mailbox theCurrentMailbox of account “MyAccount") whose date received is less than or equal to ((current date) - daysToPreserve * days))
if (count mailboxList) is greater than 0 then
move mailboxList to mailbox "Trash" of account “MyAccount"
end if
end repeat
end tell
display dialog "Old Mail Messages Have Been Purged" buttons ["OK"]
Your edit worked great. I edited your script to bring the dialog to the front.
set daysToPreserve to 2
set myAcount to "MyAccount"
set dateReference to (current date) - (daysToPreserve * days)
tell application "Mail"
activate
set myMailbox to mailbox "INBOX" of account myAcount
set accountTrash to mailbox "Trash" of account myAcount
set messagesToDelete to messages of myMailbox whose date received ≤ dateReference
repeat with aMessage in messagesToDelete
move aMessage to accountTrash
end repeat
end tell
tell current application
activate
display dialog (count messagesToDelete) & " old Mail Messages Have Been Purged" as text buttons ["OK"]
end tell
You put 1 item into a repeat block with this:
set mailboxList to mailbox "INBOX" of account “MyAccount"
repeat with theCurrentMailbox in mailboxList
You can try something like this:
set daysToPreserve to 2
set myAcount to "MyAccount"
set dateReference to (current date) - (daysToPreserve * days)
tell application "Mail"
activate
set myMailbox to mailbox "INBOX" of account myAcount
set accountTrash to mailbox "Trash" of account myAcount
set messagesToDelete to messages of myMailbox whose date received ≤ dateReference
repeat with aMessage in messagesToDelete
move aMessage to accountTrash
end repeat
end tell
display dialog (count messagesToDelete) & " old Mail Messages Have Been Purged" as text buttons ["OK"]
this version tests number of versions to move before continuing
#Applescript Purge Email Older than 2 Days
set daysToPreserve to 7
set myAccount to "MyEmail Account"
set dateReference to (current date) - (daysToPreserve * days)
tell application "Mail"
activate
set myMailbox to mailbox "INBOX/7 days" of account myAccount
set accountTrash to mailbox "Trash" of account myAccount
set messagesToDelete to messages of myMailbox whose date received ≤ dateReference
if (count messagesToDelete) as number = 0 then
return
end if
repeat with aMessage in messagesToDelete
move aMessage to accountTrash
end repeat
end tell
tell current application
activate
display notification (count messagesToDelete) & " old messages have been purged from folder 7 days" as text
delay 1
end tell

How to create list of Mail messages to move together rather than moving them individually?

I've managed to make this AppleScript work. It basically searches my inboxes for all my accounts and then selects any messages from specific address that are older than 14 days. It then proceeds to move each one of those filtered messages to a specified mailbox.
set ExcludeList to {"Trash", "Sent", "Drafts", "Deleted Messages", "Archive", "Junk", "Notes"} -- mailboxes you don't want to search
set SenderList to {"events#goldstar.com", "hello#touchofmodern.com", "staples#e.staples.com", "deals#livingsocial.com"} -- email addresses of senders you want to remove old emails for
set DestinationFolderName to "Old_Newsletters" -- mailbox to move messages to. If you want to just delete them, leave it blank.
set StaleTime to 14 -- days old the message must be before moved or deleted
set ShowMailboxesProgress to true -- determines if you want the "Processing" box displayed for each mailbox
set current_date to current date
set _msgs_to_move to {}
tell application "Mail"
set everyAccount to every account where enabled is true
-- Get acount-specific mailboxes
repeat with eachAccount in everyAccount
set accountName to the name of eachAccount
set currentMailbox to mailbox "INBOX" of eachAccount
set mailboxName to the name of currentMailbox
if mailboxName is not in ExcludeList then
if ShowMailboxesProgress then
display dialog "Processing folder " & mailboxName & " in account " & accountName
end if
try
repeat with SenderToRemove in SenderList
set messages_list to (every message of currentMailbox whose sender ends with "<" & SenderToRemove & ">")
repeat with i from 1 to number of items in messages_list
set theMessage to item i of messages_list
set difference to ((current_date) - (date sent of theMessage)) div days
if difference is greater than StaleTime then
if DestinationFolderName is not equal to "" then
move theMessage to mailbox DestinationFolderName of account "BlueStar Studios"
else
delete theMessage
end if
end if
end repeat
end repeat
end try
end if
end repeat
display dialog "Finished!"
end tell
It seems to work nice. HOWEVER, it takes a long time to run. Because it moves each filtered message individually. Is there a way to make a list of messages to be moved while in the repeat and then move that entire list of messages to another folder in one go?
Also, I'm running on 10.7.5 if that makes any difference.
Try using a filter reference form like this:
set d to (current date) - 14 * days
tell application "Mail"
repeat with a in (get accounts where enabled is true)
move (messages of mailbox "INBOX" of account a where date sent < d and (sender ends with "<events#goldstar.com>" or sender ends with "<hello#touchofmodern.com>")) to mailbox "Old_Newsletters" of account "BlueStar Studios"
end repeat
end tell

Resources