Back to list

Using input fields for counters

The article "Input fields with default values" describes how you can preset input fields with default values. But there is much more about this feature. This tip describes how you can create a counter that you can overwrite with every expansion.

The trick is to combine input fields with variables and calculations. You can use a calculation in the form

{{expCounter=#expCounter+1}}

to increase the value of expCounter whenever the expansion takes place. You can then use the {{expCounter}} marker to insert the counter in this expansion (or in any other expansion). All this happens automatically, without user intervention.

But how can you override and reset the counter? For example, you may want to count certain events monthly and reset the counter to 1 when a new month starts. You can achieve this in the following way:

{{expCounter=?Monthly Count<{{#expCounter+1}}>}}

This may look weird and complicated, but it is actually rather straightforward when you dissect this into parts: {{expCounter=?Monthly Count}} asks for "Monthly Count" and assigns the entered value to the variable expCounter. {{#expCounter+1}} is a calculation that adds 1 to the previous value of expCounter. And inserting the calculation in angle brackets <> after "Monthly Count" makes Typinator suggest the result of the calculation as the default value. Note that this marker assigns the entered input variable to the variable expCounter, but it does not produce a visible result. To actually use the value in multiple places in the expansion, use the notation {{expCounter}}. Whenever you use this expansion, Typinator takes the previous value of the counter, increments it by one and suggests that in the input field: TYP-PT_InputFieldCounter.png You can then simply confirm the value or overwrite it with 1 at the beginning of the next month. And when you click the Cancel button, no assignment will take place, and the expCounter variable retains its previous value. Note that this tip requires Typinator 5.7 or newer. Oder versions of Typinator do not support default values of input fields.