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

Posted on Sep 4, 2018 12:25 PM

Reply

Similar questions

7 replies

Sep 4, 2018 5:13 PM in response to momogabi

It's hard to be sure, but you don't appear to be using the same mailbox throughout your script:


You start off with the 'waiver@' account:

set inboxes to mailbox "INBOX" of account "waiver@"

and switch to the 'signup@' account:

set msg to message messageCount of mailbox "INBOX" of account "signup@"

which probably means you're not selecting the message you think you are.


Also, in case I'm missing something, nothing in your script selects the 'oldest' message.

Sep 5, 2018 7:44 AM in response to Camelot

Thank you for your response.


The mailbox is the same throughout. To try to make it more comprehensible to people reading it here, I changed it to "signup@" when I posted it. It's actually "waiver@", I just forgot to change that one reference when I posted it here.


This portion selects the oldest message:


set msg to message messageCount of mailbox "INBOX" of account "signup@"

I omitted parts of the script that extract data from the email, but it works when I extract data from the oldest message so I am confident this part is working.

By the way, I realized after posting that maybe the mac OSX High Sierra forum would have been more appropriate to post in, but it wouldn't let me edit to change the forum when I tried. I hope this post ins't too out of place here.

Thanks again. I really appreciate you taking the time to respond.

Sep 11, 2018 9:13 PM in response to rccharles

It's open source. All are free to use.


https://discussions.apple.com/docs/DOC-7501/edit?ID=7501&draftID=16236341


CSS adjustments

-1- "Combined" User Style sheet

-2- Use this "header" user stye sheet to reduce the space taken up by the ASC header. Suggested it be used with the above Combined user style sheet.

-3-Javascript to Move ASC community name.


Hiroto and I developed the css for Firefox. You will either need to use an older version of like 56.0.2 or Waterfox, a Firefox wannabe. I use Waterfox.


I was surprised to find that people were using the css with Safari. You will need to download the css off of CCC's page.


R

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Can I use AppleScript to sort Mail messages into mailboxes/folders?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.