Can I use AppleScript to sort Mail messages into mailboxes/folders?
The big picture:
The nonprofit I work for runs youth wrestling programs. As kids sign up online, an email with the registration details is sent to an account. When this happens, I need to (1) copy the registration information into a spreadsheet, and (2) sort the email into a team mailbox so it no longer appears in the inbox.
Once a week, I want to use a script to: go into Mail, count the emails in the inbox, selects the oldest message, copy data from that message into a spreadsheet, put the message into a team-specific mailbox, then repeat the process until all emails in the inbox have been sorted into team-specific mailboxes and the inbox is empty.
Here is a portion of the script I'm using:
tell Application "Mail"
-- COUNT THE NUMBER OF MESSAGES IN THE INBOX (this works)
set inboxes to mailbox "INBOX" of account "waiver@"
set messageCount to 0
repeat with i from 1 to number of items in inboxes
set this_item to item i of inboxes
if this_item is not missing value then
set thisCount to (count of (messages of this_item))
set messageCount to thisCount + messageCount
log thisCount
end if
end repeat
-- GO TO THE OLDEST MESSAGE IN THE INBOX (this works)
set msg to message messageCount of mailbox "INBOX" of account "signup@"
-- COPY DATA FROM THE MESSAGE INTO A SPREADSHEET (script omitted)
-- MOVE THE MESSAGE TO TEAM-SPECIFIC MAILBOX SO IT IS NO LONGER IN THE INBOX
set mailbox of msg to mailbox "Team Name" of account "signup@"
end tell
The last step is where this fails. What happens is that the message gets copied to the team-specific folder but remains in the inbox as well. The consequence of this is that when the script repeats the oldest message in the inbox is always the same. I've also tried: move msg to mailbox "Team Name" of account "signup@". This also fails.
Without using AppleScript, I can physically click-and-drag the email into the team-specific mailbox and it disappears from the inbox. Or, I can select the message, go to the menu, click Message > Move to > and then select the team specific folder. Doing this also makes the message disappear from the inbox. But with the script the way I have it, the message stays in the inbox even though it also appears in the team-specific mailbox as well.
Any thoughts?
macOS High Sierra 10.13.06 Mail 11.5
iMac, macOS High Sierra (10.13.6), Mail 11.5