Automated Outlook Cleanup with AppleScript and Launchd

29th November 2019 | Programming

Inbox zero. A lofty goal, but often difficult to regularly obtain, especially when new e-mail is continually rolling in.

I've had some jobs where the e-mail was minimal, and most of the day-to-day communication was reserved for daily stand ups and over instant messenger. In such cases, e-mail is not an issue when the signal-to-noise ratio is high since nearly all of the e-mail that was delivered was actually applicable to me.

But then there are jobs where one is deluged under a mountain of electronic messages covering notifications and alerts, on top of regular correspondence. Setting up rules in your e-mail client can help direct the flow, but it still doesn't cut down on the amount of messages, plus there is the daunting task of actually reviewing the messages.

Let's be honest — we aren't always going to read every single e-mail, especially if a majority of them do not require a response or are not directly applicable. To help mitigate the unnecessary amount of message I need to review, I have written an AppleScript which periodically scans through Microsoft Outlook and marks any old messages as read.

Setup

Script Editor (/Applications/Utilities/Script Editor.app) is the primary tool for writing, compiling, and running AppleScripts. However, it can also be useful to read and edit scripts from another text editor, such as the venerable BBEdit. I was surprised to see that BBEdit, a longtime Mac application, did not natively support AppleScript syntax highlighting. Fortunately, BBEdit (and its now-retired sister app TextWrangler) supports Codeless Language Modules to be able to provide syntax highlighting for additional languages. I went out and found an applicable AppleScript language module and installed the AppleScript.plist file in ~/Library/Application Support/BBEdit/Language Modules/. This was more for the purpose of reviewing and formatting code in BBEdit, but the brunt of the work to compile and test the script was done in Script Editor.

The AppleScript

The following script iterates through several Outlook subfolders and marks any old unread messages as read. The cutoff argument in the markUnreadMessages method is used as a threshold to determine how far back to mark old messages as read. This will keep more recent messages in an unread state to give more time to review the messages. For messages which do not pertain to me, I have those e-mails sent to the Other folder, and all of those messages are immediately marked as read when the script is run. Any e-mails which go directly to my primary Inbox are not touched by this script since I want to personally check such e-mails. For your own variant of this script, add the necessary mailbox folders and decide how much of a time threshold you need.

Launchd

Since e-mail needs continual maintenance, a good approach to ensure that this script is called at regular intervals is to use launchd on the Mac. To set up a launchd job, we create a property list file for the launch agent.

Writing: To set up a launch agent, a property list is created and then placed within the ~/Library/LaunchAgents/ directory. The following launch agent is fairly small and straightforward which calls the Mark Unread Emails AppleScript everyday at 9:00 a.m. Refer to the launchd website for further details on how to configure a launch agent for launchd. For this example, the property list is named local.markunreademails.plist.

Copying: Once the property list has been written, copy the file to ~/Library/LaunchAgents/local.markunreademails.plist.

Relaunching: Next relaunch launchd from the Terminal: launchctl load -w ~/Library/LaunchAgents/local.markunreademails.plist

Starting: To start and/or test the launch agent, run: launchctl start ~/Library/LaunchAgents/local.markunreademails

References