DEV Community

Cover image for Sort Your Apple Emails with Ollama AI
AIRabbit
AIRabbit

Posted on

Sort Your Apple Emails with Ollama AI

As a developer, my inbox is my lifeline – and a constant source of headaches. I'm drowning in emails, and frankly, I'm tired of manually sorting through the chaos. I rely heavily on the Apple ecosystem, and while Apple's AI features are slowly improving, they haven't made a dent in my Mail situation yet. It's been over two years since generative AI took the world by storm, and it's frustrating that something as basic as intelligent email categorization is still missing in action.

Plus, like many, I'm a bit hesitant to just hand over all my email data to big players like OpenAI. My privacy matters, and I'm not comfortable with the idea of every message being analyzed by a third-party black box.

So, I decided to leverage the power of Ollama, to automatically categorize and flag my incoming emails right within Apple Mail. This tutorial will walk you through how I created an Apple Mail rule that triggers a custom script, utilizing my own Ollama model to determine the appropriate category (flag) for each email. 

The result? A significantly more organized inbox, less time wasted on manual triage, and the peace of mind that comes with knowing my data stays under my control.

Let's dive in and reclaim our inboxes!

Prerequisites

  1. Install Ollama from ollama.ai
  2. Pull your preferred Ollama model (e.g., mistral, llama2)
ollama pull mistral
Enter fullscreen mode Exit fullscreen mode

Setup Instructions

1. Create the AppleScript File

Create a new file named flag-ollama.scpt in your Mail scripts directory:

~/Library/Application\ Scripts/com.apple.mail/flag-ollama.scpt
Enter fullscreen mode Exit fullscreen mode

2. Add the Script Content

Copy the following code into flag-ollama.scpt:

Note: The full source code for this project, including the AppleScript and detailed setup instructions, is available exclusively to subscribers of my blog, AiRabbit.blog. Subscribe here to gain access to this and other exciting AI-powered projects!

3. Customization Options

Change the Ollama Model

Replace mistral in the script with your preferred model:

set jsonData to "{\"model\": \"<YOUR MODEL HERE>\", \"prompt\": \"" & thePrompt & "\"}"
Enter fullscreen mode Exit fullscreen mode

Model Configuration

Configure Email Categories

Configure your email categories by modifying this line:

set colorCategories to { {"Blue", "<CATEGORY1>"}, {"Purple","<CATEGORY2>"}, {"Grey","<CATEGORY3>"}}
Enter fullscreen mode Exit fullscreen mode

Category Configuration

Make sure you rename the flags according to the categories you set up above. Here is how you can rename Flags names
[https://support.apple.com/en-au/guide/mail/mlhlp1052/mac

4. Configure Mail Rules

  1. Open Apple Mail
  2. Go to Mail > Settings > Rules
  3. Click "Add Rule"
  4. Configure the rule:
    1. Description: "Flag-Ollama"
    2. If: "any" of the following conditions are met
    3. Condition: "Every Message"
    4. Perform the following actions:
    5. Action: "Run AppleScript"
    6. Script: Select "flag-ollama"
  5. Click OK to save

Rule Configuration

Important: Testing and Application

  • Do NOT apply the rule to all existing emails
  • To test: Select a single email and click "Apply Rule"
  • The flagging will only be applied to the selected email
  • Check the log file to verify the response and categorization

If all goes well, a few seconds later you should be able to see the flag set for the selected email, if applicable. All new emails will be automatically flagged.

5. Logging

The script logs all analyses to:

~/Library/Logs/mail-ollama.log
Enter fullscreen mode Exit fullscreen mode

View the logs using:

tail -f ~/Library/Logs/mail-ollama.log
Enter fullscreen mode Exit fullscreen mode

Image description

Troubleshooting

If emails aren't being flagged:

  1. Ensure Ollama is running (ollama serve)
  2. Check running Ollama models (ollama list)
  3. Check the log file for errors
  4. Verify script execution permissions
  5. Test the rule manually on individual emails

Tip: Remember to restart Mail after making changes to the script or rules.

Please also note that if there is a syntax error in the script, Apple Mail will simply do nothing. To see potential errors, simply copy and paste the script into the "Script Editor" and run it.

Tips

  • Test the rule with individual sample emails first
  • Monitor the log file to understand AI categorization
  • Adjust the prompt if categorization needs tuning
  • Consider using different models for different analysis types

Wrap-UP
And there you have it! We've walked through a practical solution to a common problem: email overload. By harnessing the power of Ollama and a bit of AppleScript magic, we've built a system that intelligently categorizes and flags our emails directly within Apple Mail, all while keeping our data private and under our control.

Top comments (0)