I 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:
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.
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.
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.
Fill out the rest of the renaming pattern however you want to name your file.
Here is my resulting 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)
-
Yes, it would be easier to just do it year-month and forget the day. ↩