Why a Separate Script?

The MsgFiler Filing Script for MsgFiler 4 is the spiritual successor to the MsgFiler Engine Mail Plug-In used by MsgFiler 3 to bring fast-filing capabilities to Mail. The release of macOS Sonoma marked the end of Mail Plug-Ins, and for a time, it was thought to herald MsgFiler's passing as well.

But like the proverbial Phoenix rising from the ashes, MsgFiler 4 has been rewritten from the ground up as a native SwiftUI application for macOS Sonoma. Here are details on the MsgFiler Engine Filing script.

Developing applications for the Mac App Store requires that application adhere to certain App Sandbox rules. One of them is that apps cannot communicate with programs outside of what they are allowed to do so. While MsgFiler 4 has a temporary entitlement to communicate with the Mail application, there are certain actions that require additional assistance:

  • Filing messages to Gmail accounts.

  • Selecting the next message in the message viewer after filing non-Gmail messages.

  • Archiving messages from MsgFiler 4.

  • Running certain Shortcut actions.

Understanding the App Sandbox

The App Sandbox is a security feature of macOS designed to prevent damage to the system and user data if an app is compromised or poorly written. It restricts an app's access to certain system resources, user files, and other applications. Each app operates in its own environment, and it can only perform certain actions if it has permission to do so.

Let's take a look at each of the actions above to see why and how the App Sandbox prevents MsgFiler 4 from performing them out of the box:

Filing to Gmail

Gmail is one of the most popular email providers on the internet. Filing to Gmail accounts, however requires that MsgFiler 4 perform two things in succession:

  1. Archive the email message.

  2. Move the email message.

If MsgFiler 4 did not archive the message first and instead just instructed Mail to file the message, Mail would apply the mailbox label to the message but would not remove the Inbox label. As a result, the email message would appear in two places, the desired mailbox and the Inbox. Deleting the message from the Inbox would also delete it from the destination mailbox.

In order to archive the message, MsgFiler 4 must call the System Events application to programmatically instruct Mail to select the Archive menu item under the Message menu. Apps on the Mac App Store, however, are prevented from calling System Events directly.

Selecting the Next Message

When you use MsgFiler 4 to file non-Gmail messages, Mail loses its selection in the frontmost message viewer. As a result, users will have to use their mouse or trackpad to select the next message for filing. This is not an optimal experience.

In order to instruct Mail to move to the next (or previous) message, it needs to call the System Events application to send a key code to Mail.

  • Key Code 125 - Down Arrow

  • Key Code 126 - Up Arrow

Again, because System Events is not an application that can be directly controlled by an App Store app, MsgFiler 4 alone cannot perform this function out of the box.

Archiving Messages

MsgFiler 4 has a menu and keyboard shortcut to archive selected messages in Mail. Like with filing Gmail messages, there is no way to programmatically choose the Archive menu item in Mail in a sandboxed application like MsgFiler 4.

Shortcut Actions

One of the exciting new features in MsgFiler 4 is Apple Shortcuts support. There are actions to file the currently selected messages in Mail, create new mailboxes, find mailboxes, and more. If you want to use Shortcuts to file Gmail messages, however, the app needs to work around the App Sandbox.

The Right App Store Approved Way to Run Scripts Outside of the Sandbox

Fortunately, Apple has provided a solution for app developers to run scripts outside of the App Sandbox. Users can install scripts within an application's entry in the Application Scripts folder residing in the user's Library. For MsgFiler 4, this location is at:

~/Library/Application Scripts/com.atow.MsgFiler4

The key thing is that the user has to install the scripts in this directory. The app cannot automatically copy files to this directory.

Last updated