Batch moving emails in Apple Mail

Hello Everyone

I am working on an elaborate script to replace my apple rules.

Trying to batch move emails based on a collection of either “id” or “message id”. Somehow I can’t get the syntax and/or classes right.

I have made this little test script to simplify the problem. Anybody have an idea ?

set fromAcc to "ACCOUNTNAME 1"
set fromMBox to "ATEST"
set toAcc to "ACCOUNTNAME 2"
set toMBox to "BTEST"
set listMoveIds to {}
set noOfMessagesInMailbox to 0

tell application "Mail"
	-- Read All emails from mailbox
	set usingMailbox to mailbox named fromMBox of account named fromAcc
	
	-- Refference to aevery Msg
	set refAllMessages to (a reference to every message in usingMailbox)
	set noOfMessagesInMailbox to count of refAllMessages
	
	-- catch id of every second Msg
	repeat with N from 1 to noOfMessagesInMailbox by 2
		
		set end of listMoveIds to id of item N in refAllMessages
		
	end repeat --with N from 1 to noOfMessagesInMailbox by 2		
end tell --Application "Mail"

display dialog ("No of mails in mailbox: " & noOfMessagesInMailbox & ", No of id's selected: " & (count of listMoveIds))

tell application "Mail" to move (every message of mailbox fromMBox of account fromAcc whose id is in listMoveIds) to mailbox toMBox of account toAcc

display dialog ("Finished")

Model: MacBook Pro
AppleScript: 2.7
Browser: Safari 605.1.15
Operating System: macOS 10.14

Hi,

Whose clause doesn’t work with lists. So, keyword is in doesn’t work in your code. You should use repeat loop with every message instead and if statement to determine, is the id of next processed message in the list or not.

Thanks KniazidisR,

Suspected something like that.

It seems if I try to loop through and move the messages 1 at a time, Mail app clogs up if you move a lot of messages. On the other hand if you flag each msg to move and then move all the flagged msgs in one “move” statement it goes rather quick, but flagging each msg takes a lot of time. Was trying an inventive way out of this dilemma…

Thanks again for pinpointing my mistake.