Speed up script for getting mailboxes of Outlook

I’ve got a super old AppleScript to get the mailboxes of Outlook:

global AllFolders
	set AllFolders to {}
	global ContainerName
	set ContainerName to ""
	tell application "/Applications/Microsoft Outlook.app"
		set allMailFolders to get mail folders
		repeat with currentFolder in allMailFolders
			set FolderName to name of currentFolder
			set PathSoFar to ""
			if FolderName is not missing value and (container of currentFolder is not missing value or account of currentFolder is not missing value) then
				set ContainerName to ":::"
				set theContainer to currentFolder
				repeat while ContainerName is not ""
					set ContainerName to ""
					try
						set theContainer to container of theContainer
						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 PathSoFar to (AccountName) & ":::" & PathSoFar
							end if
						else
							set PathSoFar to (ContainerName) & ":::" & PathSoFar
						end if
					end try
				end repeat
				set end of AllFolders to {PathSoFar & ":::" & (FolderName)}
			end if
		end repeat
		return AllFolders
	end tell

The script works fine and for me it was never slow. Today I did some troubleshooting over Zoom for a user of my app. He has about 500 mailboxes and the script took over a minute to run. Does anyone have an idea how to speed up the script?

One suggestion is: turn all if is not statements to if is statements, because there are faster. This way:


if something is missing value then
else
	-- some actions here
end if

Other suggestion is: replace set theContainer to currentFolder with set theContainer to contents of currentFolder.

Thanks for your reply. The language would be seriously foobared having “if is” faster than “if is not”. The contents did help a bit.

I’ve now come to a different solution: I only need the id of the mailbox, the name of the mailbox and the id of the parent mailbox. The rest I can do much more easily in my main development language:

tell application id "com.microsoft.outlook"
	set allMailFolderIDs to get mail folders
	
	set allMailFolderNames to {}
	repeat with theFolder in allMailFolderIDs
		set FolderName to name of theFolder
		if FolderName is missing value then
			set end of allMailFolderNames to "xxx"
		else
			set end of allMailFolderNames to name of theFolder
		end if
	end repeat
	
	set AllMailFolderContainers to {}
	repeat with theFolder in allMailFolderIDs
		set theContainer to container of theFolder
		if theContainer is missing value then
			set end of AllMailFolderContainers to "-1"
		else
			set end of AllMailFolderContainers to container of theFolder
		end if
	end repeat
	
	return allMailFolderIDs & "||" & allMailFolderNames & "||" & AllMailFolderContainers
end tell

The original script ran in 20 seconds. The new script runs in about 5 seconds.