Outlook For Mac 2016 Set Applescript To Run

  • In Outlook, there is a Propose New Time feature, the ability to see calendars side by side, and a weather forecast in the calendar view. Outlook 2016 for Mac has very limited support for synchronization of collaboration services outside basic email. With version 15.25, Office for Mac transitioned from 32-bit to 64-bit by default.
  • Automated inbox cleansing with Outlook 2016 and AppleScript. Handy for catching an unexpected failure or knowing that a run has finished. Inbox based on a programmable set of filters that.
  • However for my work email I use Outlook 2016 for Mac. It does support rules but I have found them to be somewhat lacking. The UI for setting them up is hard work, as is testing that they are working as expected.

One of the great things about working in the world of the web is the amount of e-mails I receive. Nothing motivates in the morning like a 3 figure unread email count… No, not really

MacSet

Even when the brand-new Outlook 15 for Mac was released, they still lacked the Quick Actions functions. This is when I decided to take matters into my own hands. As I did some research to make sure I hadn't just simply overlooked a feature or missed an option some place, I stumbled upon the power of AppleScript and the Automator.

For my personal email I use Gmail with an intricate array of filters to siphon messages off into different folders as they arrive. For the most part this works OK though some of these folders are effectively proxies for the bin but I can’t quite bring myself to admit it. However for my work email I use Outlook 2016 for Mac. It does support rules but I have found them to be somewhat lacking. The UI for setting them up is hard work, as is testing that they are working as expected. As a result I’ve never gotten beyond setting up a handful of basic rules that do little to stem the tide of email.

Recently I decided to try and sort it out once and for all. Once I had done my obligatory “surely rules must be able to do this… oh god I remember this now… yeah these really are useless…” routine I stared hunting round for better solutions.

This time I also had a specific class of email that I wanted to deal with. I get a lot of notifications from apps that we use at work. For example notifications of Jira updates; passing and failing CI builds; deploy notifications and so on. These emails can be extremely helpful but also have a pretty short lifespan. For example a CI build notification email can be handy for catching an unexpected failure or knowing that a run has finished. I also try and avoid constantly checking emails and instead prefer to periodically review my inbox so the notifications are good there to see what has happened since I last checked in. But they grow stale very quickly and if I haven’t seen them a couple of hours after they arrived I am probably never going to read them. My goal was for these kinds of emails to be automatically archived or deleted once they had been read or when they were more than a couple of hours old, whichever came first.

Clearly there was no way I was ever going to get this to work using rules. I already use Automator to start my work environment at the start of the day so AppleScript seemed like a good place to start trying to implement my rules. After much googling, doc reading and an above average amount of swearing I managed to find the correct incantation that allowed me to manipulate my inbox with ease.

To start with you need to write your script. OSX has a built-in script editor for editing AppleScript — cunningly named Script Editor — which also gives you access to the super-handy dictionary so it is worth getting to grips with just for that. First fire up Script Editor. It will ask you to choose or create a script. I created a new blank script and called it outlook-cleanup.scpt. Now if you go to the file menu you will see an “Open dictionary…” menu item. Click this and you will see a list of your installed applications. Find Microsoft Outlook and select it. You will then be able to browse the dictionary that describes all the elements, classes and properties available for working with Microsoft Outlook. The one I found most handy was the message class in the Mail Suite as this described the properties of the message that I could test against when creating my rules.

Message reference for Outlook 2016

Before I could start writing individual rules though, it was necessary to write some initial code to iterate through the messages in my inbox. That looks something like this

I then extracted the properties I wanted to test against into separate variables. Since my key goal involved testing the age of the message I also stashed the current date in a variable along with 2 cut-off intervals that I would switch between depending on the type of message.

Run

Finally I added a bit of logging so I could see that things were happening as expected.

Put all together this looks like this. I’ve added a couple of sample rules so you can see what they look like.

With the script in place the next step was figuring out how to run it on a day to day basis. The solution I settled on was to run the script every 5 minutes using launchd.

To make this work, I first needed to create a Launch Agent or Daemon. According to the docs, Launch Agents are to be run when logged in as a specific user whereas Daemons are system level and run outside of a specific user session. In my case a Launch Agent seemed more appropriate since all these rules are specific to my Outlook account.

I then needed to create a property list file in ~/Library/LaunchAgents. Here is what that looks like. All the other files in ~/Library/LaunchAgents were scoped to a particular domain so I decided to do the same with mine.

I then loaded the script from ~/Library/LaunchAgents using

Once loaded I tested the script using

To tell if the script ran correctly, I used

The second column indicates the return code of the last run of the agent which all being well will be 0. If there was a problem then this will be non-zero and hopefully there will be an indicator in the log of what went wrong

Outlook For Mac 2016 Set Applescript To Run Faster

So there we have it. I now I have a self-tidying inbox based on a programmable set of filters that are flexible, easy to update, version controlled and run automatically.

Having got everything up and working I switched from using the Script Editor to using my normal Atom editor. Since AppleScript is a compiled language you need to decompile it before editing and recompile afterwards. Fortunately there is a handy Atom package that will take care of that for you.

Outlook For Mac 2016 Set Applescript To Run

I also moved the script and plist into my dotfiles repo and created a symlink for the plist from ~/Library/LaunchAgents to the file in my dotfiles. This way whenever I move to a new Mac my inbox cleansing will move along with all the rest of my configuration.

Automated inbox cleansing with Outlook 2016 and AppleScript was originally published in Hacker Noon on Medium, where people are continuing the conversation by highlighting and responding to this story.

Rules allow you to have incoming (or outgoing) messages sorted, filed, marked, or otherwise handled automatically.

We recommend that you use Outlook on the Web to create and manage your rules.

Why do we recommend Outlook on the Web?

Our Create Rules in Outlook on the Web article

The rest of this page is for those people who decide to set up their rules in Outlook 2016 for Mac anyway.

Rules are a way to have incoming (or outgoing) messages sorted, filed, marked, or otherwise handled automatically. You teach Outlook what to look for and what to do when it sees a message that meets the conditions you've set. For example:

  • move messages from a particular address into a specified folder
  • assign a category or flag to messages with a particular word in the subject
  • forward all messages with attachments to another address

Outlook For Mac 2016 Set Applescript To Runner

Here we'll talk about two ways to create rules:

Be aware that the order in which your rules are listed affects what happens. Notes on rule order below.

Creating a Rule Manually

  1. From the Tools menu choose Rules. A Rules dialog box will open.
  2. On the left, under Server Rules, click your account name (there may be only one account listed, and that's okay).
  3. Click the plus sign near the bottom of the dialog box. An Edit Rules dialog box will open.
  4. Give your rule a name (so you can find it later).
  5. Under When a new message arrives that meets all these conditions, use the drop-down lists to specify which messages meet the criteria for this rule. Start with the left-most drop-down list, as each selection determines the choices available in the other lists.
  6. If you have more than one criterion, click the plus sign at the right side of the criterion you just entered.
  7. Under Do the following, use the dropdown lists to set the action or actions you want taken on messages that match the criteria.
  8. If you have more than one action, click the plus sign at the right side of the action you just entered.
  9. If there are exceptions to the criteria, click the plus sign next to Except if, then use the drop-down lists to set the exception conditions.
  10. When finished defining the rule, click OK.
  11. Close the Rules dialog box.

Creating a Rule Based on a Message

You can create a rule that will look for messages that look like one you've already received, rather than starting with a blank slate.

First, select a message by highlighting it, then, in the Home ribbon, click Rules and select Create Rule.

The dialog box that opens will have three criteria pre-filled, based on the message you selected: From, Sent To, and Subject. Click the minus sign next to the conditions you do not want. You can also edit the conditions and the action as described in the Creating a Rule Manually section above.

Putting Your Rules in Order

The rules are checked in the order you have them listed. You can change the order of your rules by dragging and dropping them in the Rules dialog box.

The Do not apply other rules to messages that meet these criteria checkbox controls what happens when a message matches more than one rule. As a message is checked against each rule, the first time it matches a rule's criteria, Outlook looks at this checkbox on the rule that was matched.

  • If the box is checked (which is the default), Outlook will stop checking rules on this particular message and go on to the next message.
  • If the box is unchecked, Outlook will continue evaluating this message against the remaining rules.
Set

Some actions automatically check the box AND gray it out so that you cannot uncheck it. If you Delete a message or Move it to a folder, the message is no longer available to be evaluated against the remaining rules. (If you Copy a message to a folder, this doesn't happen.)