Insert The Last Date Of The Previous Month With TextExpander And Hazel

Insert The Last Date Of The Previous Month With TextExpander And Hazel

Calendar BlocksI often find myself typing the date of the last day of the previous month. Whether it is financial statements or other documents, it is just a date that comes up all the time.

I wanted a way to automate this, so I naturally turned to the Mac applications TextExpander and Hazel.

Both tools have great support for date math, which lets you add or subtract days, months, or years. This works well, but doesn’t quite give me what I want. If I am processing an electronic document on January 22, I want to get back December 31, not December 22.

Thankfully, both of these fine applications support shell scripting, so I was able to accomplish my goals. If you know an alternative way, feel free to let me know in the comments.

TextExpander

First, make sure that you set the Content settings to Shell Script.

Then paste in the following script:

#!/bin/bash
LAST_MONTH=`date -v-1m +'%m'`

case $LAST_MONTH in
    01|03|05|07|08|10|12)
        echo -n $(date -v-1m -v31d +'%Y_%m_%d');;
    02)
        echo -n $(date -v-1m -v28d  +'%Y_%m_%d');;
    *)
        echo -n $(date -v-1m -v30d  +'%Y_%m_%d');;
esac

Here’s a screenshot:

TextExpander
TextExpander

At the time of writing, for example, when I type in my snippet (in my case ; lmd but use what you want), it will return 2012_12_31.

Hazel

Wouldn’t it be great if your computer could do all this for you?

I use Hazel to automatically process as many of my documents as I can, and sometimes I want them to date-stamp them with the last day of the previous month.[1]

I am not going to go through the mechanics of setting up a Hazel rule. I did a whole webinar on the topic, and I have done many posts.

Set up the first half of the Hazel rule as you normally would, and then:

Get The Date

In the Do the following to the matched file or folder: section, set the first row to Run AppleScript. Then tap the Edit script button.

Paste in the following script:

return {hazelExportTokens:{LastMonth:do shell script "LAST_MONTH=`date -v-1m +'%m'`

case $LAST_MONTH in
    01|03|05|07|08|10|12)
        echo $(date -v-1m -v31d +'%Y_%m_%d');;
    02)
        echo $(date -v-1m -v28d  +'%Y_%m_%d');;
    *)
        echo $(date -v-1m -v30d  +'%Y_%m_%d');;
esac"}}

It looks similar to the TextExpander script, but it adds some Hazel-y goodness.

In that same window, tap the little i icon at the top right. We’re going to tell Hazel that our little script is returning a value that we want to use later.

Hazel Tap Info
Hazel Tap Info

Next, press the + button in the little window that pops up, and type in LastMonth. We’re telling Hazel that our script is returning a token called LastMonth.

Hazel Add Token
Hazel Add Token

Hit the little x buttons to get out of all these little popup windows. We’re now ready for the next step in the Hazel rule.

Use The Date

We are going to use that date that we just retrieved to rename our file. Click the + button beside the first line and set this newly created line to Rename.

Tap in the with pattern: box and you wills see a bunch of options that you can use to rename a file. Look down at the bottom: there is a new one called ·Last Month. Tap that.

Hazel Use Token
Hazel Use Token

Fill out the rest of the renaming pattern however you want to name your file.

Here is my resulting Hazel rule:

Hazel Rule
Hazel Rule

As per the rule, it will look for any document that has -cablexyz in the name, slap the date of the last day of the previous month at the beginning of the name, and rename it to, for example, 2012_12_31-Comcast.pdf.

My real rules actually look inside the contents of the PDF and move based on that, but you get the idea.

So far this has worked well for me. If you want to use a different date format, you can play around with the script. Let me know how it goes in the comments.

(Photo by VicPrinter)


  1. Yes, it would be easier to just do it year-month and forget the day.  ↩

About the Author

Brooks Duncan helps individuals and small businesses go paperless. He's been an accountant, a software developer, a manager in a very large corporation, and has run DocumentSnap since 2008. You can find Brooks on Twitter at @documentsnap or @brooksduncan. Thanks for stopping by.

Leave a Reply 7 comments

Mike Harahan - August 29, 2016 Reply

Really like this one. Can’t get either Safari or Chrome to show the contents of the Paste windows

Rolian - January 22, 2013 Reply

I couldn't get the TextExpander script to work until I took out the backslash in the second line – I don't know diddly about scripting but thought maybe that was an artifact of html?

    Brooks Duncan - January 22, 2013 Reply

    Whoops shoot! I fixed the post. Long story. Thank you so much.

dwfaust - January 22, 2013 Reply

Here's a much simpler, direct way to get that date… and it uses AppleScript

set theDate to (current date)
repeat until day of theDate = 1
set theDate to theDate – 1 * days
end repeat
set theDate to theDate – 1 * days
set theDate to year of theDate & "-" & (text -2 thru -1 of ("0" & (month of theDate as integer))) & "-" & (text -2 thru -1 of ("0" & (day of theDate as integer))) as string

    dwfaust - January 22, 2013 Reply

    And this is always accurate… where the script above does not account for leap year.

      Brooks Duncan - January 22, 2013 Reply

      Nice thanks! I knew if I posted this someone smarter than me would show up. 🙂

        dwfaust - January 22, 2013 Reply

        Thanks. Not copping to "smarter". Just trying to help out. We’re in this together. 🙂

Leave a Reply: