Keyboard Maestro: Adding TextExpander Snippet Expiry Dates

In my last post I described a method for tagging temporary TextExpander snippets with a date stamp and using some scripting to automatically remove expired snippets. In that post I mentioned that I use Keyboard Maestro to schedule the expired snippets clean-up script and that I also used it to help me in setting up those temporary snippets. In this post I’m going to provide some details on exactly how I’m using Keyboard Maestro to help with this.

The Irony of the Situation

TextExpander is a utility designed to help you enter text in a more efficient way by allowing you to insert a known block of (static or dynamic) content with a shorter trigger string. In my previous post I described a method of automating some of the management of temporary TextExpander snippets by appending a separator and date string to a snippet’s label.

e.g. Project X - Project Manager @@ 2016-04-29

The irony is that entering a separator followed by a date stamp is the sort of text entry I’d use a TextExpander snippet for … and TextExpander (for good reason) doesn’t carry out expansions when text is being entered into it.

Fortunately I have Keyboard Maestro. Whilst Keyboard Maestro can do the same sorts of text expansion that TextExpander can do, the structure and ease with which I can add and control snippets in TextExpander as well as the ability to use them on iOS devices (and sync to other expansion applications on Windows) means that Keyboard Maestro isn’t my best option for text expansion. However it does mean that I can utilise it for text expansion when I’m editing TextExpander snippets on my Mac.

The Macros

In TextExpander you have snippet groups containing snippets. In Keyboard Maestro you have macro groups containing macros. To support me in entering the expiry date stamps quickly I created a set of Keyboard Maestro macros and collected them into a macro group that I set to only be active when I’m working within the TextExpander application itself.

The Basic Macros

In the main the macros take the form of a text string being entered that triggers the macro. The macro then deletes the trigger string and replaces it with some text generated by Keyboard Maestro based on an internal variable that holds the current date. Using standard ICU date formatting options and Keyboard Maestro’s options for performing simple calculations on the date I’m able to produce a number of useful macros.

Description Abbreviation Text Content
Expire in 0 days (Today) @@0 @@ %ICUDateTime%yyyy-MM-dd%
Expire in 1 days (Tomorrow) @@1d @@ %ICUDateTimePlus%1%Days%yyyy-MM-dd%
Expire in 1 week @@1w @@ %ICUDateTimePlus%1%Weeks%yyyy-MM-dd%
Expire in 1 year @@1y @@ %ICUDateTimePlus%1%Years%yyyy-MM-dd%
Expire in 2 days @@2d @@ %ICUDateTimePlus%2%Days%yyyy-MM-dd%
Expire in 3 days @@3d @@ %ICUDateTimePlus%3%Days%yyyy-MM-dd%
Expire in 5 days @@5d @@ %ICUDateTimePlus%5%Days%yyyy-MM-dd%
Expire in 7 days @@7d @@ %ICUDateTimePlus%7%Days%yyyy-MM-dd%
Expire in 10 days @@10d @@ %ICUDateTimePlus%10%Days%yyyy-MM-dd%
Expire in 20 days @@20d @@ %ICUDateTimePlus%20%Days%yyyy-MM-dd%
Expire in 30 days @@30d @@ %ICUDateTimePlus%30%Days%yyyy-MM-dd%
Expire in 60 days @@60d @@ %ICUDateTimePlus%60%Days%yyyy-MM-dd%
Expire in 90 days @@90d @@ %ICUDateTimePlus%90%Days%yyyy-MM-dd%
Expire next month @@m @@ %ICUDateTimePlus%1%Months%yyyy-MM%-01
Expire next year @@y @@ %ICUDateTimePlus%1%Years%yyyy%-01-01

Note: The in 1 week and in 7 days macros are indeed equivalent, but I included them both so that regardless of which I might type I’ll get an appropriate result.

When viewed in the Keyboard Maestro editor, each macro therefore looks something like this:

As well as these simple macros that only require a single macro action to generate there are another several others that are multi-action macros.

The Expire in X Days/Weeks/Months/Years Macros

Whilst there are a lot of standard quick entry days in the list above maybe at some point I might need to enter 12, 15, 21 days, etc. Maybe I’ll need to do the same with weeks, months or (in highly unlikely circumstances) years.

For these macros I simply prompt for the number of days/weeks/months/years and then pass the entered value into the same sort of ICU based date calculations used in the single step macros.

For example this is how the Expire in X months macro looks in the Keyboard Maestro editor.

The Expire Next Week Macro

Whereas the expire next month and year macros can be easily specified (months always start on the 1st and years on the 1st of the 1st), weeks are slightly trickier to determine. For me, weeks start on a Monday which makes things relatively straight forward as ICU dates also assign numeric values to week days beginning with Monday (i.e. Monday = 1, Tuesday = 2, etc.)

As a result I can use the ICU date format to get the day of the week and subtract that from 8 to tell me after how many days the next Monday will be reached. I can then use that to calculate and return the appropriate expiry date string.

The Expire On Macro

The expire on macro is only two actions, but the first action is to call another Keyboard Maestro action - my Date Picker action.

I published a post about this ‘helper’ macro a little while ago so you’ll need a copy of that macro to make this one work and you may find that you’ll need to re-link to that macro if you have downloaded and moved it into another macro group.

The Date Picker macro allows you to select a date and it stores this in a variable in yyyy-MM-dd format. The Expire On macro then inserts “@@” followed buy the specified date.

The macro looks like this in the Keyboard Maestro editor:

Wrap-up

You can download the Keyboard Maestro macro group I use for this and are free to modify it to suit your own needs. If you have found this post useful please feel let me know in the comments below, or send me a Tweet.

Author: Stephen Millard
Tags: | textexpander |

Buy me a coffeeBuy me a coffee



Related posts that you may also like to read