Advertisement
  1. Code
  2. Coding Fundamentals
  3. Workflow

Effortless Paperless Nirvana With Mail, Hazel and Evernote

Scroll to top
Read Time: 9 min
This post is part of a series called Going Paperless.
Use HelloSign to Get Documents Signed Online
How to Convert & Edit PDF Documents in Microsoft Word

With the relentless onslaught of the digital era, paperless systems have been gaining increasing traction and relevance. While the benefits are undeniable, there are times when workflows are far from frictionless and often tedious, making them less appealing.

There is hope however and in this screencast I'll teach you how to leverage apps such as Mail, Hazel and Evernote to automate at least one aspect of your paperless workflow.


Prerequisites

You'll need:


The Mail Rule



Effortless Paperless Nirvana With Mail, Hazel and Evernote

Step 1

The initial step is to set up a mail rule that will filter any email from your utility company. To do that, simply click on Mail > Preferences and then choose the Rules tab.

Click on Add Rule, give the rule a meaningful name and then choose some conditions. If you need to assign more than one condition, then take note if you want to match all, or any, since this will affect how emails are matched.

PaperlessNirvana-MaiRrulePaperlessNirvana-MaiRrulePaperlessNirvana-MaiRrule
When choosing more than one condition, take note if matching all or any.

Once you're satisfied with your conditions, it's time to assign some actions. For the first action, choose to Mark as Read. For the final action, choose Run Applescript and then from the drop down list of available scripts choose Open in Finder.

Step 2

Now that you have the finder open in the correct location, open AppleScript Editor. You can find AppleScript Editor in the Utilities folder or optionally summon spotlight and type AppleScript Editor.

Paste in the following code and then save the script in /Users/YOUR USERNAME/Library/Application Scripts/com.apple.mail/

1
2
-- Boilerplate code for Mail Rules
3
using terms from application "Mail"
4
	on perform mail action with messages theMessages for rule theRule
5
6
		-- Script Starts Here
7
		tell application "Mail"
8
9
			-- Set the downloads folder for attachments
10
			set _downloadsFolder to (path to downloads folder) as rich text
11
12
			-- Repeat the following actions for every email matched by rule
13
			repeat with eachMessage in theMessages
14
15
				-- Get the emails subject
16
				set theSubject to subject of eachMessage
17
18
				-- Repeat with each attachement found in email
19
				repeat with _attatchment in eachMessage's mail attachments
20
21
					-- Rename attachment with the following pattern:
22
					-- Utility Company (Tag) | Email Subject (Title) | Original Attatchment (Filename)
23
					-- and save in downaloads folder
24
					set originalName to name of _attatchment
25
					set _savePath to _downloadsFolder & "ZON|" & theSubject & "|" & originalName
26
					try
27
						save _attatchment in _savePath
28
					on error errmsg
29
						display dialog errmsg
30
					end try
31
				end repeat
32
33
				-- Move email to final destination
34
				move eachMessage to mailbox "Bills" of account "Gmail"
35
			end repeat
36
		end tell
37
38
		-- End script and close boilerplate section
39
	end perform mail action with messages
40
end using terms from

Code Breakdown

In order to adapt the code to suit your needs, it's essential you understand it, so let's break it down.

  • Lines 2-3 & 32-33 are simple boilerplate code. Each AppleScript that is to be run by a Mail rule needs this. The actual script that performs actions with the matched messages is placed inside this block;
  • Line 5 starts the tell block, instructing (telling) Mail app to perform some actions;
  • Line 7 assigns the Downloads folder to a variable. This will later be used as the location to which attachments are saved;
  • Lines 9-29 is a repeat block. Actions within will be repeated for every email message matched by the mail rule;
  • Line 11 assigns the emails subject to a variable, this will later be used to rename the attachment;
  • Lines 13-26 is another repeat block. Actions within will be repeated for every attachment found in each individual message;
  • Line 17 assigns the original attachment filename to another variable;
  • Line 18 renames the attachment with the following pattern: Utility Company | Emails Subject | Original attachment filename;
  • Lines 21-25, a try block, tries and saves the attachment in the Downloads folder. If it fails an error message will be triggered.
  • Line 28 moves the email to a final mailbox, in this case named Bills.

Step 3

After having saved the above script in the correct location, you should now be able to select it from the drop down list in the Run AppleScript action; do so and then click OK and finally click Apply.


Creating The Hazel Rule

Hazel is a preference pane utility that monitors folders of your choosing and performs actions on files and folders when criteria you defined are met.



Effortless Paperless Nirvana With Mail, Hazel and Evernote

Step 1

Assuming you've already downloaded and installed Hazel, you'll need to add the Downloads folder to the Folders list. To do so, open System Preferences > Hazel and then simply drag it in from a Finder window or alternatively, click + and select it.

Before going any further, it's important to pause rule processing. This will ensure that no rules are processed until you're confident the rules you set up are correct. To pause rule processing, right click (Ctrl + click) on Downloads from the folder list and select Pause "Downloads rules".

Step 2

Now that Hazel is watching the Downloads folder, you need to define some rules. In Hazel, rules are defined in a similar manner as they are in Mail. You set some conditions to match and then some actions to perform.

Under Rules, click + to add a new rule. Give it a name and then add your first condition. Choose Kind then is and finally select PDF. This will ensure that Hazel will only match PDF files. If your utility company sends you your bills in any other format, then change this appropriately.

For the second condition we want to make sure that Hazel will search for a file with a name that matches the pattern defined in the Mail rule above; so choose Name, then matches and finally in the next field, type the tag assigned above (Utility company) followed by a "|" character, then drag in the Anything token, followed by another "|" and finally another Anything token.

Step 3

Now that you have defined some conditions, you need to set up some actions. For the first action, choose Run AppleScript, leave the default embedded script selected and then click on edit script. Paste the following code in the modal window, click on compile and if no errors are present, click away from the modal window.

PaperlessNirvana-HazelCompilePaperlessNirvana-HazelCompilePaperlessNirvana-HazelCompile
The compile button in Hazel. If any error is detected, it will appear next to the button.
1
2
tell application "Finder"
3
4
	-- Store AppleScript's Text Item Delimiters
5
	set {ASTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "|"}
6
7
	set theListOfCustomTokens to name of theFile
8
	-- Since we changed text item delimiters to | we now have
9
	-- 3 text items in theListOfCustomTokens
10
	-- 1: ZON
11
	-- 2: The emails subject (which will be used for the Note Title)
12
	-- 3: The attachments original name.
13
14
	-- Rename the attachment to its original name
15
	set the name of theFile to (text item 3 of theListOfCustomTokens)
16
	try
17
18
		-- Open Evernote and wait 15 seconds for it to launch
19
		tell application id "com.evernote.evernote"
20
			activate
21
			with timeout of 15 seconds
22
23
				-- Create a new note in the notebook "Receipts" with:
24
				-- the emails subject as title
25
				-- the utility company as a tag
26
				create note title (text item 2 of theListOfCustomTokens as text) ¬
27
					from file theFile notebook {"Receipts"} ¬
28
					tags {(text item 1 of theListOfCustomTokens as text)}
29
30
			end timeout
31
		end tell
32
	on error errmsg
33
		display dialog errmsg
34
	end try
35
	set AppleScript's text item delimiters to ASTID
36
end tell

Code Breakdown

As before, let's analyse the code in order to get a better understanding of what is happening:

  • Line 1 starts the tell block which instructs Finder to perform some actions;
  • Line 3 performs 2 actions. It starts off by storing AppleScripts text item delimiters (by default it's a space) in a variable and then changing the text item delimiters to a "|";
  • Line 4 assigns the matched filename to a variable. Since text item delimiters were changed to a "|", and given the filenames pattern, this variable now holds 3 text items;
  • Line 11 changes the matched files name to the original attachment name. This value is extracted from text item 3;
  • Lines 12-27 is a try block. AppleScript tries to perform the actions contained within and in case of an error, displays a message;
  • Lines 14 and 15, tell Evernote to activate. This will launch Evernote and bring it to the front;
  • Line 16 creates a 15 second pause, giving Evernote time to launch properly before attempting to create a new note;
  • Lines 20-22 create a new note in the Receipts notebook. The notes title is the emails subject (text item 2) and the note is tagged with the utility companies name (text item 1). The attachment is added to the note too;
  • Line 28 resets AppleScripts text item delimiter to default.

Step 4

For the final action, choose Move to folder and then select Trash. This will ensure that once your bill base been successfully uploaded to Evernote, it doesn't stick around creating clutter on your Mac. Finally click OK.

Now that you've successfully created a rule to match your file, it's time to let Hazel perform its magic, so you'll need to unpause rule processing. Just as before, right click on Downloads in the folder list and this time, selected Resume "Downloads" Rules.

Since Hazel can take a while before running rules the first time, right click on Downloads once again and choose Run Rules Now. If all went according to plan, Hazel should upload the file to Evernote and then move it to the Trash.


Conclusion

In this tutorial we've covered a few basic automation concepts that will hopefully aid in streamlining your paperless workflow. These concepts however are not limited to this small scope, but rather can serve as building blocks for more complex and ambitious workflows.

Advertisement
Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.