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
- Install Ollama from ollama.ai
- Pull your preferred Ollama model (e.g., mistral, llama2)
ollama pull mistral
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
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 & "\"}"
Configure Email Categories
Configure your email categories by modifying this line:
set colorCategories to { {"Blue", "<CATEGORY1>"}, {"Purple","<CATEGORY2>"}, {"Grey","<CATEGORY3>"}}
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
- Open Apple Mail
- Go to Mail > Settings > Rules
- Click "Add Rule"
- Configure the rule:
- Description: "Flag-Ollama"
- If: "any" of the following conditions are met
- Condition: "Every Message"
- Perform the following actions:
- Action: "Run AppleScript"
- Script: Select "flag-ollama"
- Click OK to save
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
View the logs using:
tail -f ~/Library/Logs/mail-ollama.log
Troubleshooting
If emails aren't being flagged:
- Ensure Ollama is running (
ollama serve
) - Check running Ollama models (
ollama list
) - Check the log file for errors
- Verify script execution permissions
- 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)