Recently, a user complained that Mail Archiver took 2 minutes to start. I was horrified. After making a special version for testing the speed I found the culprit: reading the preferences. 

The user had used version 5 where his 10k mailboxes for multiple archives were saved into the preferences of macOS. Even though the data wasn't used anymore reading the preferences was super slow. The user deleted the preferences and Mail Archiver's start was snappy again.

After making sure that nobody else would be affected by the problem I had the idea to create thousand mailboxes for testing. Most users have too many mailboxes. I have a dozen accounts but only hundred mailboxes for testing.

Of course, I wanted to use AppleScript to create the mailboxes. As always AppleScript is somewhat challenging. I'm going to make an AppleScript for both Mail and Outlook today because the scripts are more simple than what I do normally.

Creating Mailboxes for Mail with AppleScript

tell application "Mail"
   
repeat with i from 1 to 1000
       
set mailboxName to "testmailbox/mailbox" & i
       
set newMailbox to make new mailbox at the account "Mothsoftware" with properties {name:mailboxName}
   
end repeat
end tell

The script is super simple. I have a parent mailbox for the new mailboxes of "testmailbox" and the script makes 1000 mailboxes with the names of mailbox1, mailbox2 and so on in my account "Mothsoftware" The only thing I need to remember is not to use prohibited characters like / \ : and . in my mailbox names.

The problem with the script is that it is super slow. When testing I started with 100 mailboxes which took some minutes. The thousand mailboxes took over 30 minutes!

Creating Mailboxes for Outlook with AppleScript

As always Outlook is more fun than using Mail. First I tried to only change the tell target from Mail to Outlook. For Outlook I also need to use imap account and not account:

tell application "Microsoft Outlook"
   
repeat with i from 1 to 1
       
set folderName to "outlook test/mailbox" & i
       
set newFolder to make new mail folder at imap account "Mothsoftware" with properties {name:folderName}
   
end repeat
end tell

Instead of getting an error in AppleScript like "Error creating mailbox" I got 2 errors in Outlook.

The first error:

Name space error after creating a mailbox

Some accounts have all their mailboxes under the Inbox. My test account is such a case. Mail handles this automatically. For Outlook I need to be more specific for the mailbox creation.

And the second error:

Error invalid mailbox name

For Mail "mailbox/submailbox" is a mailbox path. Outlook thinks that "mailbox/submailbox" is a mailbox name which uses the prohibited character /. Again for Outlook I need to be more specific. I first need to create the parent mailbox or I need to get a reference to a mailbox and then create the mailboxes into the parent mailbox. A reference to a mailbox in Outlook is done in reverse order: mail folder "submailbox" of mail folder "parent mailbox" of IMAP account "some account". And I shouldn't forget to add the Inbox.

This gives me:

tell application "Microsoft Outlook"

   
set mailFolder to mail folder "Outlook Test" of inbox of imap account "Mothsoftware"
   
repeat with i from 1 to 1000
       
set folderName to "mailbox" & i
       
set newFolder to make new mail folder at mailFolder with properties {name:folderName}
   
end repeat
end tell

This script was really nicely fast! 

Why is AppleScript in Mail so super terribly slow? I really don't get it. Microsoft needs to improve error handling. But from the fiasco that is called New Outlook they seem to have different problems. Namely, getting AppleScript to work at all.