Sunday, May 9, 2021

#1 2021-04-26 09:19:11 am

theOtter
Member
Registered: 2008-11-21
Posts: 11

Efficiently Delete Emails

I’m trying to delete a large number of messages from Mail.app using AppleScript. I’ve been able to identify the messages and get them into a list, but I’m having trouble deleting them efficiently. Right now, I’m looping through the list as follows:

Applescript:


tell application "Mail"
    repeat with msg in toDelete --This is the list of messages.
        delete msg --or: move msg to mailbox "Trash"
    end repeat
end tell

This works perfectly (whether I delete or merely move to Trash), but it takes about 0.6s per message—fast enough if I’m deleting 10-20 messages, but not so much if I’m deleting 10,000-20,000.

I’ve read that AppleScript should be able to move a list of messages all at once, but I can’t seem to get it to work. Any ideas?

Model: MacBook Pro 16,1
AppleScript: 2.7
Browser: Safari 605.1.15
Operating System: macOS 11.2.3

Last edited by theOtter (2021-04-26 06:29:15 pm)


Filed under: list, mail, email, delete

Offline

 

#2 2021-04-26 10:58:30 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 703

Re: Efficiently Delete Emails

My approach to delete emails do not involve any AppleScript.
I have Smart Mailbox in my mail and let mail to filter the emails.

If I like to delete any or every one...
I select the Smart Mailbox and do shortcut command + a (select all) and delete... fast I think smile

I have more 20 Smart Mailbox... and its very easy to maintain.


if you are the expert, who will you call if its not your imagination.

Offline

 

#3 2021-04-26 04:36:13 pm

Marc Anthony
Member
From:: Dallas, TX
Registered: 2006-04-27
Posts: 1017

Re: Efficiently Delete Emails

Hi. Putting messages in a variabilized list dereferences them and necessitates looping, and iterating through 20K items takes however long it takes. You could use a whose clause—which has its own time overhead to filter the items—or just specify by range to affect multiples. This assumes the account is the only or the first:

Applescript:

tell application "Mail" to delete (account 1's mailbox "INBOX"'s messages whose subject contains "on sale!")

--or

Applescript:

tell application "Mail" to delete (account 1's mailbox "INBOX"'s messages 1 thru 20)

Edited for typo

Last edited by Marc Anthony (2021-04-27 06:26:49 am)

Offline

 

#4 2021-04-26 10:07:02 pm

theOtter
Member
Registered: 2008-11-21
Posts: 11

Re: Efficiently Delete Emails

Fredrik71 wrote:

My approach to delete emails do not involve any AppleScript.
I have Smart Mailbox in my mail and let mail to filter the emails.

If I like to delete any or every one...
I select the Smart Mailbox and do shortcut command + a (select all) and delete... fast I think smile

I have more 20 Smart Mailbox... and its very easy to maintain.


Thanks, Fredrik. I also love Smart Mailboxes, but it never occurred to me to use them in this way. Is there a way to create a smart mailbox that would show all but one of every set of duplicates?

Thanks!

Last edited by theOtter (2021-04-26 10:07:14 pm)

Offline

 

#5 2021-04-26 10:08:34 pm

theOtter
Member
Registered: 2008-11-21
Posts: 11

Re: Efficiently Delete Emails

Marc Anthony wrote:

Hi. Putting messages in a variabilized list deferences them and necessitates looping, and iterating through 20K items takes however long it takes. You could use a whose clause—which has its own time overhead to filter the items—or just specify by range to affect multiples. This assumes the account is the only or the first:

Applescript:

tell application "Mail" to delete (account 1's mailbox "INBOX"'s messages whose subject contains "on sale!")

--or

Applescript:

tell application "Mail" to delete (account 1's mailbox "INBOX"'s messages 1 thru 20)


Thanks, Marc. I know I saw someone saying it was possible to delete a list en masse, but perhaps that person was mistaken. I appreciate your feedback!

Offline

 

#6 2021-04-27 01:39:17 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 703

Re: Efficiently Delete Emails

theOtter wrote:

...Is there a way to create a smart mailbox that would show all but one of every set of duplicates?

This is not possible, but maybe a complex GUI Script could do it...

You could sort by the inbox with subject (if find same subject it will be next to each other)
and from there you could delete any copy that you have as duplicate.


if you are the expert, who will you call if its not your imagination.

Offline

 

#7 2021-04-27 03:58:12 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 1788

Re: Efficiently Delete Emails

It will not be possible to create a useful smart mailbox to display only originals.

It is much easier to let the script mark the duplicates with some colored flag. Then you can see with your own eyes in the message viewers which messages are flagged and delete them at your discretion. Most duplicates are usually found in the "Archive" mailbox.

The following script marks duplicates in Green. Then, you create manually smart mailbox with condition "Message has flag" <GreenColor>, where you can select all its contents and delete at once, or one by one.

Applescript:


set {IDs, Duplicates} to {{}, 0}

tell application "Mail"
   repeat with anAccount in (accounts)
       repeat with aMailbox in (mailboxes of anAccount)
           try
               set aMessages to messages of aMailbox
               repeat with aMessage in aMessages
                   set aMessageID to id of aMessage
                   if (IDs contains aMessageID) then
                       set flag index of aMessage to 3 -- Green color
                       set Duplicates to Duplicates + 1
                   else
                       set end of IDs to aMessageID
                   end if
               end repeat
           end try
       end repeat
   end repeat
   
end tell

display dialog "---- " & Duplicates & " Duplicate Messages Flagged with Green ----"

Last edited by KniazidisR (2021-04-27 05:09:00 am)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#8 2021-04-27 01:15:21 pm

theOtter
Member
Registered: 2008-11-21
Posts: 11

Re: Efficiently Delete Emails

KniazidisR wrote:

It is much easier to let the script mark the duplicates with some colored flag. Then you can see with your own eyes in the message viewers which messages are flagged and delete them at your discretion. Most duplicates are usually found in the "Archive" mailbox.


Oooh! I like that! Thanks for the awesome idea!

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)