Some months ago I showed you how to get a nicely formatted list of mailboxes out of Outlook. A Mail Archiver user contacted me and said he would get the beachball when archiving to Outlook. We did a screensharing session and I found out that he had 500 mailboxes in Outlook. The AppleScript to get the mailboxes took 2 minutes. Blech!

What is the problem with the AppleScript?

The core functionality of the Script is this line:
   
set allMailFolders to get mail folders

But this gives only the mailbox IDs and not their paths. To get the paths I need to get the name and then I need to go up in the hierarchy with a loop which starts with the following line:
     
repeat while ContainerName is not ""

This gets progressively slower the more mailboxes Outlook has.

How to speed the AppleScript up?

What data do I really need from Outlook? I need the ID of the mailbox, the name of the mailbox and the ID of the parent mailbox. The rest I can calculate myself.

So here is what I came up with for getting the mailboxes for Outlook with AppleScript:

with timeout of 10000 seconds
   
tell application id "com.microsoft.outlook"
       
set allMailFolderIDs to get mail folders
       
set allMailFolderData to {}
       
repeat with theFolder in allMailFolderIDs
           
--do the id
           
set theID to id of theFolder
           
set end of allMailFolderData to theID
           
--do the name
           
set FolderName to name of theFolder
           
if FolderName is missing value then
               
set end of allMailFolderData to "xxx"
           
else
               
set end of allMailFolderData to name of theFolder
           
end if
           
--do the container id
           
set theContainer to container of theFolder
           
if theContainer is missing value then
               
set end of allMailFolderData to "-1"
           
else
               
set ContainerName to name of theContainer
               
if ContainerName is missing value then
                   
set theAccount to account of theContainer
                   
if theAccount is not missing value then
                       
set AccountName to name of theAccount
                       
set end of allMailFolderData to AccountName
                   
end if
               
else
                   
set end of allMailFolderData to id of theContainer
               
end if
           
end if
       
end repeat
       
return allMailFolderData
   
end tell
end timeout

As in the old script I start by getting a list of mailbox IDs. Then I make one loop to get name, ID and parent ID. As always Outlook makes things special. If the parent is an account I use the name of the account instead of the parent ID.

The result is not pretty:

Three values always belong together. The first mailbox is 8, "Gespeicherte Nachrichten", "-1". With some brain power I was able to get the full mailbox paths for Mail Archiver.

So how fast is the new AppleScript now?

Speed of code is endlessly fascinating. On High Sierra there is barely a difference between the 2 AppleScripts. On the Apple Silicon computer that is now on macOS Monterey the slow script runs in 15 seconds while the fast script runs in under 5 seconds.

The general slow speed of getting the mailboxes is an Outlook problem. I can't speed this up any further.

So I had the idiot idea to complain about the lack of speed to Microsoft. I said that I have a slow AppleScript and asked if they could take a look to speed the script up. The reply was that this feature isn't supported. Head on desk!