
Tips and Tricks for Microsoft Outlook
- How to get all mailboxes with AppleScript[+]
My email archiving application Mail Archiver X uses AppleScript extensively. AppleScript is the basis for getting data from the different mail clients. I'll show how to get all mailfolders from Outlook. What is relatively simple in AppleMail is quite complicated in Outlook.
The Beginning:
Getting a first list of mailboxes is deceptively simple:
set allMailFolders to get mail folders
But we need their paths and not a reference to the mailfolders! Therefore, we need to loop over the mailfolders
repeat with currentFolder in allMailFolders
--do something
end repeat
Getting the name of a mailfolder again is easy:
set FolderName to name of currentFolder
The Main Part:
Now comes the nasty part, which took me quite some time: If a property of an object doesn't exist we need to test for "missing value" and not for an empty string. In our case this can be either the name of the mailfolder itself or the parent. The parent can be either another mailfolder or an account. This gives us for testing:
if FolderName is not missing value and (container of currentFolder is not missing value or account of currentFolder is not missing value) then
Then we need to go upwards in the hierarchy until the condition above isn't true anymore:
repeat with currentFolder in allMailFolders
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
In the middle we need to check again if the parent is a mailbox or an account:
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
This completes the main repeat.
Everything complete:
Finally the script is finished.
global AllFolders
set AllFolders to {}
global ContainerName
set ContainerName to ""
tell application "Microsoft Outlook"
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
Beatrix Willius
Walterslebener Strasse 23a
55262 Heidesheim
Germany
Phone: +49-(0)6132-509659
Contact - Imprint