How to set date format as plain text inside Set Variable?

Yes, I think we could have for example the option to add quotes around a token and that would "print" the token itself as text and not what the token represents, you know?
Like this
New Folder - "%Variable%VarName%"
would actually create a new folder with that name:
New Folder - %Variable%VarName%

I'm using quotes as an example, not sure if this would be the right symbol, but you get the point :slight_smile: That way we would have 3 options available.

This is exactly my point :slight_smile:
If it's a literal string then the New Folder action should interpret that the same way as when we manually type %ICUDateTime%yyyy-MM-d, hh.mm.ss a% into the field (when we type, we are typing a literal string as well, right? It's plain text).

So the action itself should get that information from the variable and replace the string with the current date when creating the folder.

With the current way Keyboard Maestro works I would suggest you do this:

  1. Make a Subroutine Macro that creates the date in the form you would like.
  2. In each Macro that you want to use that date call the Subroutine.
  3. If you ever want to change the date format, you only have to do that in the Subroutine. All the calling Macros would be given the new date format without having to make any changes to them.

I'm not familiar with Subroutines yet, but I will research.
What I'm getting from it is that it's just a macro with a different name... am I missing something special about it...?

I was able to make it work last night and it's here:

I just need to run the macro with the variable. I believe it's the same as the subroutine?
Reading the documentation, didn't really clarify how different it is from running a macro
https://wiki.keyboardmaestro.com:8443/Subroutines

Hi @iamdannywyatt - after a good night's sleep and a bit of catching up with this thread I now have a better perspective on what you're after.

If I understand correctly now, what you want is the ability to save a "formula" in a variable and then evaluate that formula later by using a reference to the variable. Something like:

set TimeStamp to "%ICUDateTime%yyyy-MM-d, hh.mm.ss a%"
...
...
...
create a folder with name "BetterTouchTool Backup, EVALUATE(TimeStamp)"
...
...

In this example the name of the folder would be evaluated to something like this:

BetterTouchTool Backup, 2022-05-13, 10.21.56 am

In other words, you're using the contents of the variable TimeStamp as a kind of template.

While there is no direct equivalent in-line function in KM to the EVALUATE() function I've written above, you can achieve a similar effect by using KM's Filter action like so in this demonstration macro (which you'll need to tweak to suit your needs):

Test Indirect Evaluation.kmmacros (4.3 KB)

Click to see macro

The Filter with Process Tokens action takes what is in the variable and evaluates it thus allowing you to set up a global variable as a "template" for your timestamp. I've grouped this with the New Folder action to emphasise that they are both needed together for it to work as (I hope) you want it to.

After all the discussion I wonder if I'm on the right track? What do you think?

2 Likes

@iamdannywyatt, as @Zabobon so eloquently explained, we do understand what you're after, but it's not how KM works. In simplistic terms, the folder action processes what you write in the box once.

variable > data

What you're asking it to do is selectively process part of it twice.

variable > token > data

@tiffle has provided a nifty solution for changing the behaviour of the variable globally, but the trouble is that you need to trigger that process, so I still think the quickest option, practically speaking, is to recall a favourite that sets the variable to a preferred date format. That's one move. Inserting a subroutine and select its target is two moves.

Thanks for taking the time to go back to this thread and coming up with another solution :slight_smile:

My goal, though, is to make everything less confusing and faster to set up. Also, with the advantage of using global variables. In your example, it seems that each macro would have to have those 2 complex actions, which is something I would like to avoid.

I found a workaround that I detailed here:

Basically, before the New Folder action, I just run the variable's macro. That removes a lot of the extra actions and complexity. Even if the date is not accurate to the millisecond or second, in this particular scenario that's not relevant, as long as it finds the approximate current date so that I can run the same macro twice and it creates 2 distinct folders.

Basically, since running the variable's macro updates it to the current date, running the New Folder action right after it, will use the current date (add a few ms or sec, but not a big deal).

As I mentioned before, this would cause more problems, should I decide to change the format later on, because now all formats are "hard-coded" into each action. Let's say you used that in 100 actions... you will have to go back to each one of them and update the format manually. Having a variable, makes everything easier.

I shared my method of just running the variable right before the New Folder action, but it seems that everyone here is skipping that as a "valid" approach hahaha. Am I missing something here? Because that seems to be the most simple and fast approach, and also the easier to update should I decide to make changes to the format.

If I'm missing something about that approach, please let me know, as I'm still learning KM :slight_smile:

Nope.

I'm sorry but I assumed you had more experience with KM than perhaps you have. My demo macro is exactly that and just illustrates the principle involved and, for testing, I always use local variables so I don't get cluttered up with variables I'll never use again.

So - the first action coloured orange would be in a separate macro and would, in your case, assign the literal string to a global variable that you want to be your template for a timestamp. Like this:

The (teal-coloured) group of actions would be in your other macro(s) where you want to actually generate the timestamp from the template. You would set the green Filter action to refer to your global variable template. To illustrate, I've ungrouped the actions to make it more obvious what's going on:

KM 1 2022-05-13_12-40-51

In terms of complication, you've made it more complicated by having 2 macros since you can assign that ICUDate string directly to a variable using KM's variable inspector in the KM preferences

So instead of using the simple Filter action you choose to execute a separate macro.

Well, you're just replacing one action (Filter) with another (Execute Macro) and that macro contains a Set Variable action. So it looks like you're swapping my one action for your 2 actions!

The beauty of KM is that it provides enough options to satisfy everyone - so good luck and have fun!

2 Likes

No problem. Yes, I'm still learning :slight_smile:

Ok. Yes, in this case, I want it to be global, because I have a lot of apps that I need to create backups from on a regular basis.

Not really. I have 3 actions, just like you do:
1 - Set a Variable inside a macro, something I will not have to change at all.
2 - The action to run the variable
3 - The action to create the folder

You have 3 actions as well: orange, green, gray.

Now the difference I see on my approach, is that I don't have to worry much about filling out 2 fields ("Filter" and "To") or remember what those are supposed to be. All I need to do is go to the Execute Macro dropdown menu > "Select Macro by Name" > type "Current Date" and that's it. I'm good to go.

Your approach is also useful at least for me to learn another new thing that could be useful in another scenario. In this case, it seems that my approach (unless there's something I'm missing in terms of running into issues) is faster and easy to remember.

So true and it's through seeing other people's approach that we end up learning new ways of achieving the same results, but sometimes in a better way. Been learning a lot from reading other people's threads

Brilliant @tiffle, you have done what I was sure could not be done! That filter Action is very powerful.

1 Like

Ok, so just for testing purposes I tried the file you shared, but no luck. All it did was create a folder with the name:
BetterTouchTool Backup, %Variable%LocalCurrentDate%

Can you please confirm that on your end it created the folder with the current date and that running it 2 or more times, keeps creating new folders with the current dates?

Have you tried the macro @tiffle shared? If so, were you able to create multiple folders by running it multiple times and each with the current dates?
On my end, no luck... It keeps creating a single folder with the name:
BetterTouchTool Backup, %Variable%LocalCurrentDate%

@Zabobon and @tiffle
My bad! When I copied the text from the New Folder field and then pasted it again, it added extra % symbols so I ended up with this:
BetterTouchTool Backup, %%Variable%%LocalCurrentDate%%
Go figure...

@tiffle do you mind explaining what the filter is actually doing, since I'm not familiar with the technicalities...? Thanks!

Here is what I get after running it several times:

1 Like

I saw this, but it's a bit complex for me to truly understand what's happening under the hood. If it's too complex to explain based on what the action is actually doing (for example the Process Tokens section), it's ok. You've already done a lot :slight_smile:

So my question is: can I have those 2 actions separate (but inside the same macro) or do I have to group them as well for it to always work as expected?

Yes, after I fixed by "bug" it's now working as expected! :slight_smile:

You remember that to set up your initial variable to the ICUDate string literally that you had to check the "Process Nothing" item in the gear menu?

The Filter action is just doing the same job as if you had checked the "Process Text Normally" option. So the literal ICUDate string (held in the global variable) is now being interpreted by the Filter action and the result is placed into the second variable specified in the Filter action.

1 Like

@iamdannywyatt, I think it may be that you're not setting it up correctly.

Download this group and double click it.

TimeStamp.kmmacros (38.7 KB)

The first macro uses the Timestamp created in the second, by calling it as a submacro.

Screenshot

If you insert the Execute a Submacro action into all your macros that use time and date, they will all use the same format. If you want to change that format, you just adjust it in the submacro.

Screenshot

NB: I changed the folder creation path so I could test it. It works. Change the path to whatever you need it to be.

@tiffle, apologies if this muddies the water at all, but it seems like this method solves the problem without needing to filter, (which you've already mentioned).

Of course you can. Grouping actions the way I did allowed me to name the group "Create New Folder with Timestamp" in order to illustrate what those two actions were doing together. Creating a group in this way does not affect the execution of the enclosed actions in any way. It's a bit like having a box labelled "Enterprise" and the box contains all the Lego blocks to build that model while I might have another box labelled "Sydney Opera House" and I think you can get what's in that box!

1 Like