Why does $1 work inside some tokens but not others?

Could someone explain to me why this doesn't do what it clearly is intended to do. Here's the simplest piece of code to demonstrate the problem:

image

It is intended to replace the leading digits of each line in some text or variable with a formatted version of the date corresponding to the leading number in each line.

The problem seems to be that it's not placing the value of $1 inside the ICUDateTimeFor token. It seems to be returning an empty string for $1 when used inside this token. I can place variables inside that token, and I can place $1 outside that token, but I can't place $1 inside that token.

The following page says that $1 and \1 can be used inside the %Calculate% token, so shouldn't they also be usable inside other tokens like %ICUDateTimeFor%? Why do some tokens work with $1 but others not?

https://wiki.keyboardmaestro.com/action/Search_and_Replace

I just had to hunt this down last night, and the answer is "it's designed to work that way." Here's Peter explaining why:

Basically, as I understand it, it's a decision made due to not being able to fully decide what $1 should be in a given expansion.

-rob.

I think that's a slightly different case -- a capture group inside a variable -- and the behaviour is documented on the Wiki.

I wonder if, for this case, the answer is on the "ICUDateTime Tokens" page:

Using Variables with These Date/Time Tokens

You can use any calculation in the numeric parameters, like the [amount of time] and [unit of time] fields.

@Airy -- try this:

image

Using

%ICUDateTimeFor%CALCULATE($1)%yyyy-MM-dd-hh-mm%,$2

...to coerce from text to numeric.

2 Likes

Ah, sorry, my bad.

-rob.

I'm absolutely gobsmacked. You did it. I had read that wiki page several times but clearly I didn't understand parts of it. You have exceeded even my own expectations for your own genius, which is already very high.

As @Nige_S notes, the issue is that that section of the token is a numeric field, rather than a text field.

On the todo list is:

  • MATCH function to return regex group matches as numeric results.

Which, if implemented, would allow %ICUDateTime%MATCH(1)%…%. But as it is not implemented (yet), CALCULATE($1) is the correct solution.

2 Likes