Use date from statement as tag (SnapScan > Hazel > Evernote)

From your noodle to other noodles. Talk about ways to get the most from Hazel. Even exchange recipes for the cool rules you've thought up. DO NOT POST YOUR QUESTIONS HERE.

Moderators: Mr_Noodle, Moderators

This post shows how to create a Hazel rule that will figure out that an arbitrarily named file dropped in a folder is a bank statement and file it in a "Bank statements" notebook in Evernote, renamed to reflect its content, and tagged with the account and the year and month of the statement.

I downloaded Hazel today, and I've never used AppleScript before. But I'm a programmer, and I borrowed techniques from posts on Apple, Hazel, and Evernote forums. At a general level, the method illustrates how to match contents from a .pdf file, create custom tokens from .pdf file contents, pass the tokens in a modified form to an AppleScript, then use the tokens to create tags on the file sent to Evernote.

1. Set up your scanner to send its output to a particular folder. With SnapScan, I set the SnapScan Manager to send its scans to Documents/SnapScan. You can set this up for other scanners, and put things from other places into this folder, such as downloads of statements from the Internet. The important point is to set up the rules in Hazel to search this folder.

2. SnapScan names its files with the current date and time, which is not that useful for us. Thankfully, it does automatically process its scanned files so their text can be read by programs like Hazel. We can use text that is only found in statements for a particular bank account or credit card to figure out what to do with the file. Here's my rule overall - I'll go into the details below:

Code: Select all
If all of the following conditions are met
Kind is PDF
Contents contain xxxx xxx xxx xxx xxx
Contents contain match (*unnamed)(...)(*Date)
Do the following to the matched file or folder:
Rename with pattern Visa (*Date) (extension)
Run AppleScript embedded script
Move to folder Trash

The xxxx xxx xxx xxx xxx is the exact form of my Visa card number on the statement, including spaces.

There are several dates on the statement. I want to tag the file in Evernote with the statement date. On every month's statement, there is text

Code: Select all
Statement date         Apr 25, 2013

or whatever the date is for the statement. I match that in the third condition above, which has three elements in its pattern. First, which I left as "unnamed", has "Statement date" without the double quotes. Second is an anything sequence for the huge and varying amount of space before the date. Then the date. I made sure to click on the date token icon, then change the format of the date to match my statement's. This required entering the month token icon, a space, the day token, a comma and space, then the year token. I also needed to change the format of the month from its default 12 to Dec. I gave this token a name, Date, so I could use it later in my actions.

My first action was to give the file a different name. I wanted a better, more descriptive name than the scanner produces. And equally important, using the filename is apparently the easiest way to pass information into an AppleScript. So made the filename "Visa 2013-04 .pdf" so I could see that it was my Visa statement. I reformatted the date (click on it and play around) so that I could sort my tags and files in Evernote and have them appear in chronological order.

You need to note two important tricks used here. First, I'm using a space as a "delimiter" to separate text that I'm going to use later as tags. Normally one should use a weird, infrequently used character for this, eg |. But I didn't want to have rename the file. Also, I had to put this delimiter character between the date and the extension, since otherwise I ended up having the .pdf extension included as part of my second tag in Evernote.

Here's my AppleScript:
Code: Select all
tell application "Finder"
   set myTokens to name of theFile -- get the filename which includes the tokens I'm going to use as tags
      set oldDelims to AppleScript's text item delimiters -- save their current state
      set AppleScript's text item delimiters to {" "} -- declare new delimiters used in filename
      set tag1 to text item 1 of myTokens -- contains the account name
      set tag2 to text item 2 of myTokens -- contains the statement year-month
      tell application "Evernote"
         create note from file theFile notebook "Bank statements" tags {tag1, tag2}
      end tell
      set AppleScript's text item delimiters to oldDelims -- restore them
   on error
      set AppleScript's text item delimiters to oldDelims -- restore them in cases where something went wrong
   end try
end tell

If you want to put more tags on the file in Evernote, add them to the filename using the delimiter to separate them from the other tags, then pull them out in the script.

I'm going to create more rules to handle tagging and processing statements for my other accounts now using this same pattern.

I hope this helps others.
Posts: 1
Joined: Sat Jul 06, 2013 4:55 pm

Return to Tips & Tricks - DO NOT POST QUESTIONS