Keyboard Maestro: Date Picker Macro29 Mar 2016
Keyboard Maestro is one of my favourite Mac utilities. It’s like having a Swiss Army knife that includes not only a simple blade but also a scalpel, chainsaw, electron microscope and a suit of armour. If I can’t easily do something in another utility chances are there’s a way to use Keyboard Maestro to get the job done.
One of the things I wanted to do with it recently (more on that in an upcoming blog post) was to pop-up a simple interface to allow me to select and return a date in a particular format.
Keyboard Maestro has three core levels of content. At the lowest level are actions. Actions do something. By chaining together actions together you can do more than one thing and typically a sequence of related things. These chains are known as macros. Macros can then be bundled into collections (which can be enabled/disabled based on a set of rules).
What I’m sharing here is a single macro. It isn’t actually intended to be standalone macro but rather something to be called from another macro (via an Execute Macro action). The idea is that it simply provides a simple way for a user to select a date (it defaults to the current date) and the result is stored in a Keyboard Maestro variable in the format yyyy-MM-dd (e.g. 2016–03–29).
The macro consists of two actions. The first action displays a window (shown above) that allows the user to specify the three elements that make up the date - the day, the month and the year. The second action then takes those selections and stores them in the correct format.
The first action in the macro is Prompt for User Input. This is the action that builds the window for selecting the date.
Whilst the macro doesn’t carry out any validation of the date (e.g. you could enter the 31st of February), it does constrain the available ranges.
Days are limited to the numeric values of 1 through 31 (as two digits) and it defaults to the current day of the month. It does this by using a Keyboard Maestro date variable - ICUDateTime. This variable is formatted to just return the day portion (using ‘dd’). This (prefixed by a pipe (‘|’)) is put at the start of a pipe separated list of all of the days. When the day is substituted in rather than adding a duplicate entry the first pipe character tells Keyboard Maestro to simply find it in the list and default to it.
The day variable capture therefore loks like this.
The months are presented in a similar way to the days. This time of course there are only twelve, but I decided that I’d rather display them as the full month name rather than the numeric digits for the month. Keyboard Maestro allows you to use a double underscore (‘__’) to separate pair values and so each month in the list is represented by it’s two digit numeric value followed by two underscores and the name of the month. For example, March is represented as ‘03__March’. This means that when “March” is selected in the window’s drop down list, the value “03” is stored in the Month variable.
Again we’re using some date formatting to prefix the current date time variable to provide the long month name.
The final element is the year. This time we don’t have any pre-determined ranges. Instead everything is calculated from the current year.
The “%ICUDateTime%yyyy%” that begins the list sets the default to be the current year. The list is then constructed from six years.
The list starts at the year previous to the current year by using a special Keyboard Maestro variable construct that can subtract from the current date time. In this case we’re subtracting one year. The result is then formatted to be a four digit year.
This is followed by the current year as a four digit year and then a similar construct for outputting one, two, three and four years beyond the current year sequentially. A six year range has been good enough for my use cases thus far, but it would be straight forward to extend this to suit your own needs.
The user is presented with an OK and a Cancel button when the window is displayed. If the user selects Cancel the macro terminates. If the user selects OK the second action (Set Variable) sets the DateStamp variable to the specified year (4 digits), followed by a hyphen, followed by the specified month (2 digits), followed by a hyphen and finally the specified day (2 digits).
If you don’t fancy creating the macro yourself you can of course download a copy.
Quite a neat macro in just two actions, but very useful. You can use it for specifying a prefix on files (yyyy-MM-dd is great for sorting files by date) or simply capturing a date in a standard format that is unlikely to be confusing when used internationally (01.02.2016 … is read as the 2nd of January in some places and as the 1st of February in others).
In an upcoming post I’ll explain where I’ve been using this to help me with TextExpander snippets … and yes you probably did read that correctly!