How to Create a Template of a Date Format in a Variable

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

KM had a glitch moment and it's now working using @tiffle method as well

@noisneil ok, now I'm getting worried and really think this is a conspiracy against me hahaha
You just used the method I shared 5 hours ago...

Well we've kinda been going around in circles, so...

2 Likes

Thanks @Zabobon - but I think the brilliance lies with KM.

The Filter action has many powerful talents but they're not all necessarily obvious!!

1 Like

Ok thanks.
Yeah I was just wondering if in this case having them grouped would behave differently than separate. Almost like the way an IF statement is a group of IF, THEN, you know? :wink:

When you grouped, you just used Engroup > Group?

Exactly.

1 Like

Haha true...
But since I've shared that reply a few times, I thought you guys had seen it and maybe there was something wrong with that approach, because no one seemed to comment on that.

Glad we finally came to a few options. It seems that @tiffle's option also brought some light to other users about this process, which is great!

1 Like

Awesome!!!
Again, thank you so much for taking the time to discuss this and bringing your input. Super valuable! :slight_smile:

1 Like

OK, so this has been done to death -- but that's never stopped me before, so here goes nothing!

If I can restate the problem: You'd like to ensure consistent date/time formatting across many macros by storing the format in a "constant" to be used in the ICUDateTime token.

That means processing a token "inside" a token, which I was having trouble with. Then Google led me to this post from @peternlewis, and a possible solution.

Simply put -- double-encode the "inner variable", then double-decode with filters.

Date Test.kmmacros (3.9 KB)

Summary

I've used different variables to make it easier to follow in the debugger, but it works just as well filtering to "source". I've only made the format string a "constant", which makes this usable with the other ICUDateTime variants -- Peter's post shows how to use variables to set the other parameters needed for those.

It works, it does what OP wanted -- but I don't think this is the best method!

It needs multiple actions in each macro that uses it, which is redundant. Much better to use the trick above in a subroutine -- indeed, a good subroutine using just one parameter would allow you get a date/time slug in any number of formats in any macro with just one action. You could even include the "For", Plus", and "Minus" variants in the same subroutine, adding parameters to suit.

Now there's a project for someone...

2 Likes