Monday 6 May 2013

Apple Script: Auto Archive Email to Local Computer (Outlook Mac 2011)


This Apple script allows you to archive your emails that are older than 90 days. In order for this script to work, you only require to change the XXXXXXXXXX to the greyed out name of your Exchange Account in the main Outlook window holding all your folders (Inbox etc).

Note:
How to create an AppleScript
http://www.macinstruct.com/node/68



(*
===Auto Archive Email to Local Computer (Outlook Mac 2011)===
Description:
This Apple script will help you to archive your emails that are older than 90 days.
Details:
Mail:
-----
- Script to auto archive a full folder structure from the default (or nominated) exchange account to local 'on my computer' root folder
- Set parameteres in 'Global Settings' section below, before running script
- Script can be run manually from AppleScript Editor which is useful if you want to review the debug event log (click twice on the "Events" button above the logging window to see log output), however...
- It's also recommended you schedule the script from Outlook's 'Run Schedule' tool to execute on a regular basis (daily recommended)
Disclaimer: Free to use at your own risk and liability *)
tell application "Microsoft Outlook"
-- Global Settings which you can change if required ---------------------------------------------------------------------------------------------------------------------------
-- General set exchangeAccountDescription to "XXXXXXXXXX" -- the greyed out name of your Exchange Account in the main Outlook window holding all your folders (Inbox etc)
set runInSimulationMode to false -- when set to true no mail or calendar events will be archived. It will however create the appropriate folder structures under 'On My Computer' and the candidate items that will be archived will be logged to the events window for you to review. The script can be run repeatedly to test out the effect off different parameters below. You can also optionally delete the empty created folders from running in this mode
set processingDelay to 0.2 -- The number of milliseconds to wait between moving messages on Outlook. On slower machines Outlook can't handle the speed at which the script requests mail to be moved. Slowing this down can lesson the chance of a timeout but lengthens the time the script takes to run (escpecially on first run where it must process large amounts of mail)
-- Mail Archive parameters
set archiveMailItems to true -- no mail archiving will take place if set to false
set daysBeforeMailArchive to 90 -- number of days to keep mail in your exchange account before archiving
set localMailArchiveRootFolderName to "Archive Mail" -- name of the root archive mail folder to create under 'On My Computer'. If an existing archive mail folder is found it will use it, otherwise it will create the folder for you
set excludedMailFoldersList to {"Subscribed Public Folders", "Junk E-mail", "Deleted Items", "Sync Issues", "quarantine", "Conversation History"} -- list of mail folders in your exchange account to exclude (sub-folders will also be excluded).
set processSubFoldersofExcludedFolders to false -- By setting to true subfolders will be archived even though the parent folder is excluded for all excluded folders in above list. Note that in this mode, folders with the repeated same name in your folder tree hierarchy will be all excluded if included in the excluded list.
--set exchange account (if none specified then use the first account found if it's not a delegated or other users folder account)
if exchangeAccountDescription is "XXXXXXXXXX" then
set exchangeAccount to item 1 of exchange accounts
if exchange type of exchangeAccount is not primary account then
error "Please set an exchange account which is not delegated or another users folder account"
end if
log ("Processing " & name of exchangeAccount as text) & " - the primary exchange account"
else
set exchangeAccount to exchange account exchangeAccountDescription
log "Processing " & exchangeAccountDescription & " - the set exchange account"
end if
log "==================== Mail ===================="
-- Archive Mail if required
if archiveMailItems then
log "Processing mail folders"
-- Run archive process to local folders
my archiveExchangeFolders(mail folders of exchangeAccount, excludedMailFoldersList, my createMailArchiveFolder(localMailArchiveRootFolderName, on my computer), daysBeforeMailArchive)
end if
log "Done!"
end tell

(*================= Mail Archiving ================*)
-- Recursively archive the tree of exchange folders (but ignoring the excluded folders)
on archiveExchangeFolders(exchangeFolders, excludedFolders, archiveRootFolder, daysBeforeArchive)
tell application "Microsoft Outlook"
-- Calculate the earliest date of mail that must remain on exchange server
set earliestDate to ((current date) - (daysBeforeArchive * days))
log "Earliest Date - " & earliestDate
repeat with mailFolder in exchangeFolders
set mailFolderName to name of mailFolder as text
set mailFolderExcluded to (mailFolderName) is in excludedFolders
set subFoldersExist to my hasSubFolders(mailFolder)
set currentArchiveFolder to ""
-- Avoid excluded folders unless requested to process their sub-folders regardless
if not mailFolderExcluded or my processSubFoldersofExcludedFolders then
-- Only create the local folder if archiving will occur or sub-folders exist in the excluded folder
if subFoldersExist or not mailFolderExcluded then
-- create the destination folder locally if it doesn't exist already
set currentArchiveFolder to my createMailArchiveFolder(mailFolderName, archiveRootFolder)
end if
if not mailFolderExcluded then
-- archive mail in current folder
my archiveMail(mailFolder, currentArchiveFolder, earliestDate)
end if
if subFoldersExist then
log mailFolderName & " has sub-folders"
my archiveExchangeFolders(mail folders in mailFolder, excludedFolders, currentArchiveFolder, daysBeforeArchive)
end if
else
log mailFolderName & " and sub-folders excluded"
end if
end repeat
end tell
end archiveExchangeFolders
-- Create Local Mail Archive Folder unless it exists already
-- Returns the created/found folder
on createMailArchiveFolder(mailFolderName, archiveRootFolder)
tell application "Microsoft Outlook"
set foundItemList to every mail folder of archiveRootFolder where name is mailFolderName
set currentArchiveFolder to ""
if (count of foundItemList) is greater than 0 then
log "Found existing folder " & mailFolderName
set currentArchiveFolder to mail folder mailFolderName of archiveRootFolder
else
log "Creating folder " & mailFolderName
set currentArchiveFolder to make new mail folder in archiveRootFolder with properties {name:mailFolderName}
end if
return currentArchiveFolder
end tell
end createMailArchiveFolder
-- Archive mail from exchange folder to Mail Archive folder but only if older than earliestDate
on archiveMail(mailFolder, currentArchiveFolder, earliestDate)
tell application "Microsoft Outlook"
set exchangeMessages to messages of mailFolder
repeat with theIncrementValue from 1 to count of exchangeMessages
set theMessage to item theIncrementValue of exchangeMessages
if time sent of theMessage is less than earliestDate then
log "Archiving mail -  " & (subject of theMessage as text) & " -  " & (time sent of theMessage as text)
set todo flag of theMessage to not flagged
if not my runInSimulationMode then
move theMessage to currentArchiveFolder
delay my processingDelay
end if
else
log "Folder archive complete"
exit repeat
end if
end repeat
end tell
end archiveMail

(*=======================================================
-- Utility helper methods
*)
-- Determines whether a folder has sub-folders or not
on hasSubFolders(mailFolder)
tell application "Microsoft Outlook"
if (count of mail folders in mailFolder) is greater than 0 then
return true
else
return false
end if
end tell
end hasSubFolders
-- Determine if passed in folder is a root folder
on isRootFolder(mailFolder)
tell application "Microsoft Outlook"
if (name of container of mailFolder) is missing value then
return true
else
return false
end if
end tell
end isRootFolder

try
display dialog "Your email is archived successfully! Click OK to exit."
end try

1 comment:

  1. Thank you! I had the same written for PC, but hoping someone had done this for MAC

    ReplyDelete